Skip to content

Commit

Permalink
Add short ID Column for loading, add label to sqlite shapes
Browse files Browse the repository at this point in the history
  • Loading branch information
LocalNewsTV authored and plasticviking committed Dec 12, 2024
1 parent 5649782 commit 557d10c
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions app/src/utils/record-cache/sqlite-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ const RECORD_CACHE_DB_MIGRATIONS_1 = [

const RECORD_CACHE_DB_MIGRATIONS_2 = [
`ALTER TABLE CACHED_RECORDS
ADD COLUMN GEOJSON TEXT;`
ADD COLUMN GEOJSON TEXT;`,
`ALTER TABLE CACHED_RECORDS
ADD COLUMN SHORT_ID TEXT;`
];

class SQLiteRecordCacheService extends RecordCacheService {
Expand Down Expand Up @@ -219,14 +221,15 @@ class SQLiteRecordCacheService extends RecordCacheService {
throw new Error('cache not available');
}
const stringified = JSON.stringify(data);
const short_id = (data as Record<PropertyKey, Feature[]>)?.short_id;
const geometry = (data as Record<PropertyKey, Feature[]>)?.geometry;
const geojson = JSON.stringify(geometry) ?? null;
await this.cacheDB.query(
//language=SQLite
`INSERT INTO CACHED_RECORDS(ID, DATA, GEOJSON)
VALUES (?, ?, ?)
`INSERT INTO CACHED_RECORDS(ID, DATA, GEOJSON, SHORT_ID)
VALUES (?, ?, ?, ?)
ON CONFLICT (ID) DO UPDATE SET DATA=?;`,
[id, stringified, geojson, stringified]
[id, stringified, geojson, short_id, stringified]
);
}
async loadRecordsetSourceMetadata(ids: string[]): Promise<RecordSetSourceMetadata> {
Expand All @@ -237,7 +240,7 @@ class SQLiteRecordCacheService extends RecordCacheService {
const geoJsonArr: any[] = [];
const results = await this.cacheDB?.query(
// language=SQLite
`SELECT GEOJSON, ID
`SELECT GEOJSON, SHORT_ID
FROM CACHED_RECORDS
WHERE ID IN (${ids.map(() => '?').join(', ')})
AND GEOJSON NOT NULL`,
Expand All @@ -246,9 +249,11 @@ class SQLiteRecordCacheService extends RecordCacheService {

results?.values?.forEach((item) => {
try {
JSON.parse(item['GEOJSON'])?.forEach((shape: Feature) => {
centroidArr.push(centroid(shape));
geoJsonArr.push(shape);
const label = item['SHORT_ID'];
JSON.parse(item['GEOJSON'])?.forEach((feature: Feature) => {
feature.properties = { name: label };
centroidArr.push(centroid(feature));
geoJsonArr.push(feature);
});
} catch (e) {
console.error('Error parsing record:', e);
Expand Down

0 comments on commit 557d10c

Please sign in to comment.