From fe0de66ef1d68f1ceff226091133f9cfd3adcd64 Mon Sep 17 00:00:00 2001 From: LocalNewsTV <62873746+LocalNewsTV@users.noreply.github.com> Date: Tue, 31 Dec 2024 09:33:33 -0800 Subject: [PATCH] add implement deleteCachedRecordsFromIds for SQLite --- app/src/utils/record-cache/index.ts | 2 +- .../utils/record-cache/localforage-cache.ts | 3 ++- app/src/utils/record-cache/sqlite-cache.ts | 18 +++++++++++++++++- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/app/src/utils/record-cache/index.ts b/app/src/utils/record-cache/index.ts index 4896c0396..b76e2df4e 100644 --- a/app/src/utils/record-cache/index.ts +++ b/app/src/utils/record-cache/index.ts @@ -54,7 +54,7 @@ abstract class RecordCacheService { abstract saveActivity(id: string, data: unknown): Promise; abstract saveIapp(id: string, iappRecord: unknown, iappTableRow: unknown): Promise; - + abstract deleteCachedRecordsFromIds(idsToDelete: string[], recordSetType: RecordSetType): Promise; abstract loadActivity(id: string): Promise; abstract loadIapp(id: string, type: IappRecordMode): Promise; diff --git a/app/src/utils/record-cache/localforage-cache.ts b/app/src/utils/record-cache/localforage-cache.ts index c32c5f7b4..34f43be5d 100644 --- a/app/src/utils/record-cache/localforage-cache.ts +++ b/app/src/utils/record-cache/localforage-cache.ts @@ -6,6 +6,7 @@ import { Feature } from '@turf/helpers'; import { GeoJSONSourceSpecification } from 'maplibre-gl'; import IappRecord from 'interfaces/IappRecord'; import IappTableRow from 'interfaces/IappTableRecord'; +import { RecordSetType } from 'interfaces/UserRecordSet'; class LocalForageRecordCacheService extends RecordCacheService { private static _instance: LocalForageRecordCacheService; @@ -168,7 +169,7 @@ class LocalForageRecordCacheService extends RecordCacheService { return { cachedCentroid, cachedGeoJson }; } - async deleteCachedRecordsFromIds(idsToDelete: string[]): Promise { + async deleteCachedRecordsFromIds(idsToDelete: string[], recordSetType: RecordSetType): Promise { if (this.store == null) { throw new Error('cache not available'); } diff --git a/app/src/utils/record-cache/sqlite-cache.ts b/app/src/utils/record-cache/sqlite-cache.ts index f65a9d14b..49a334406 100644 --- a/app/src/utils/record-cache/sqlite-cache.ts +++ b/app/src/utils/record-cache/sqlite-cache.ts @@ -4,6 +4,7 @@ import { Feature } from '@turf/helpers'; import IappRecord from 'interfaces/IappRecord'; import IappTableRow from 'interfaces/IappTableRecord'; import UserRecord from 'interfaces/UserRecord'; +import { RecordSetType } from 'interfaces/UserRecordSet'; import { GeoJSONSourceSpecification } from 'maplibre-gl'; import { IappRecordMode, RecordCacheService, RecordSetSourceMetadata } from 'utils/record-cache/index'; import { sqlite } from 'utils/sharedSQLiteInstance'; @@ -282,7 +283,22 @@ class SQLiteRecordCacheService extends RecordCacheService { }; return { cachedCentroid, cachedGeoJson }; } - + async deleteCachedRecordsFromIds(idsToDelete: string[], recordSetType: RecordSetType): Promise { + if (this.cacheDB == null) { + throw new Error(CACHE_UNAVAILABLE); + } + const RecordsToTable = { + [RecordSetType.Activity]: 'CACHED_RECORDS', + [RecordSetType.IAPP]: 'CACHED_IAPP_RECORDS' + }; + const RECORD_TABLE = RecordsToTable[recordSetType]; + await this.cacheDB.query( + // language=SQLite + `DELETE FROM ${RECORD_TABLE} + WHERE ID IN (${idsToDelete.map(() => '?').join(', ')})`, + [...idsToDelete] + ); + } private async initializeRecordCache(sqlite: SQLiteConnection) { // Hold Migrations as named variable so we can use length to update the Db version automagically // Note: toVersion must be an integer.