xt-idb-store 0.4.3 → 0.4.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -207,10 +207,10 @@ class IndexedDbStorageService extends AbstractXtStoreProvider {
|
|
|
207
207
|
}
|
|
208
208
|
return ret;
|
|
209
209
|
}
|
|
210
|
-
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.
|
|
211
|
-
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.
|
|
210
|
+
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.10", ngImport: i0, type: IndexedDbStorageService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
|
|
211
|
+
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.10", ngImport: i0, type: IndexedDbStorageService, providedIn: 'root' }); }
|
|
212
212
|
}
|
|
213
|
-
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.
|
|
213
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.10", ngImport: i0, type: IndexedDbStorageService, decorators: [{
|
|
214
214
|
type: Injectable,
|
|
215
215
|
args: [{
|
|
216
216
|
providedIn: 'root'
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"xt-idb-store.mjs","sources":["../../../projects/idb-store/src/indexeddb/indexed-db-storage.service.ts","../../../projects/idb-store/src/public-api.ts","../../../projects/idb-store/src/xt-idb-store.ts"],"sourcesContent":["/**\n * Allow storing of entities in the browser local database\n */\nimport { from, Observable, Subscription } from 'rxjs';\nimport Dexie, { Table } from 'dexie';\nimport { Injectable, OnDestroy } from '@angular/core';\nimport { AbstractXtStoreProvider, UploadedDocumentInfo, XtStoreCriteria, XtStoreProviderHelper } from 'xt-store';\n\n\n@Injectable({\n providedIn: 'root'\n})\nexport class IndexedDbStorageService<T=never> extends AbstractXtStoreProvider<T> implements OnDestroy {\n\n protected static globalDb: Dexie|null;\n\n protected db: Dexie|null=null;\n\n protected dbName = \"Dont-code Sandbox Lib\";\n\n protected subscriptions = new Subscription ();\n\n /**\n * Enable test code to close a database between tests\n */\n public static forceCloseDatabase () {\n // eslint-disable-next-line no-restricted-syntax\n //console.debug(\"IndexedDB: In forceCloseDatabase\");\n if (this.globalDb!=null) {\n // eslint-disable-next-line no-restricted-syntax\n //console.debug(\"IndexedDB: GlobalDB Exist\");\n if (this.globalDb.isOpen()) {\n // eslint-disable-next-line no-restricted-syntax\n //console.debug(\"IndexedDB: Closing GlobalDB\");\n this.globalDb.close();\n // eslint-disable-next-line no-restricted-syntax\n //console.debug(\"IndexedDB: GlobalDB is closed\");\n }\n }\n }\n\n public static forceDeleteDatabase (dbName:string):Promise<void> {\n // eslint-disable-next-line no-restricted-syntax\n //console.debug(\"IndexedDB: In forceDeleteDatabase\");\n return Dexie.delete(dbName).then(() => {\n // eslint-disable-next-line no-restricted-syntax\n //console.debug(\"IndexedDB: Database \"+dbName+\" deleted\");\n });\n }\n\n constructor(/*protected values: ValueService,\n protected configService: CommonConfigService,\n @Optional () modelMgr?:DontCodeModelManager*/\n ) {\n super(/*modelMgr*/);\n /*this.updateConfig (configService.getConfig());\n this.subscriptions.add (configService.getUpdates().pipe (map ((newConfig) => {\n this.updateConfig(newConfig);\n })).subscribe());*/\n // Let unit tests close or delete the database between tests if needed\n if ((self as any)._indexedDbStorageServiceForceClose == null) {\n (self as any)._indexedDbStorageServiceForceClose = () => IndexedDbStorageService.forceCloseDatabase();\n }\n if ((self as any)._indexedDbStorageServiceForceDelete == null) {\n (self as any)._indexedDbStorageServiceForceDelete = (dbName:string) => IndexedDbStorageService.forceDeleteDatabase(dbName);\n }\n\n}\n\n deleteEntity(name: string, key: any): Promise<boolean> {\n return this.ensurePositionCanBeStored(name, false).then(table => {\n return table.delete(key).then(() => {\n return true;\n });\n });\n\n }\n\n loadEntity(name: string, key: any): Promise<T|undefined> {\n return this.ensurePositionCanBeStored(name, false).then (table => {\n return table.get(key);\n }).catch(reason => {\n console.warn(\"IndexedDB: Cannot load entity \"+key+\" : \"+reason);\n return undefined;\n });\n }\n\n override searchEntities(name: string, ...criteria: XtStoreCriteria[]): Observable<Array<T>> {\n return from (\n this.ensurePositionCanBeStored(name, false).then(table => {\n return table.toArray().then(list => {\n return XtStoreProviderHelper.applyFilters(list, ...criteria);\n });\n }).catch(reason => {\n // Probably table not found, just returns empty values\n console.warn(\"IndexedDB: Cannot search entity: \"+reason);\n return [];\n })\n );\n }\n\n canStoreDocument(): boolean {\n return false;\n }\n storeDocuments(toStore: File[]): Observable<UploadedDocumentInfo> {\n throw new Error(\"Impossible to store documents in IndexedDB.\");\n }\n\n storeEntity(name: string, entity: any): Promise<T> {\n return this.ensurePositionCanBeStored(name, true).then(table => {\n return table.put(entity).then(key => {\n if ((entity._id) && (entity._id!==key)) {\n return Promise.reject(\"Stored entity with id \"+key+\" different from \"+entity._id);\n } else {\n return entity;\n }\n\n });\n });\n }\n\n ensureEntityCanBeStored (description: any, create?:boolean):Promise<Table<T>> {\n if (description)\n return this.ensurePositionCanBeStored(description.name, create);\n else{\n return Promise.reject(\"Error called with null description\");\n }\n }\n\n ensurePositionCanBeStored (name: string, create?:boolean):Promise<Table<T>> {\n return this.withDatabase().then (db => {\n // We have to make sure the database is open before we can get the list of tables\n let table;\n try {\n table = db.table(name);\n } catch (error) {\n // Just ignore table not found\n }\n if (table != null) return Promise.resolve(table);\n\n if (create) {\n const tableDescription: { [key: string]: string } = {};\n tableDescription[name] = '++_id';\n return this.changeSchema(db, tableDescription).then(db => {\n return db.table(name);\n });\n } else {\n return Promise.reject(name + ' table not found');\n }\n });\n }\n\n protected changeSchema(db : Dexie, schemaChanges:any): Promise<Dexie> {\n //console.log(\"IndexedDB: Closing DB\");\n db.close();\n/* const newDb = new Dexie(db.name,{allowEmptyDB:true, autoOpen:false});\n\n newDb.on('blocked', ()=>false); // Silence console warning of blocked event.\n\n // Workaround: If DB is empty from tables, it needs to be recreated\n if (db.tables.length === 0) {\n return db.delete().then (value => {\n newDb.version(1.5).stores(schemaChanges);\n return newDb.open();\n })\n }\n\n // Extract current schema in dexie format:\n const currentSchema = db.tables.reduce((result:{[key:string]:any},{name, schema}) => {\n result[name] = [\n schema.primKey.src,\n ...schema.indexes.map(idx => idx.src)\n ].join(',');\n return result;\n }, {});\n*/\n //console.log(\"Version: \" + db.verno);\n //console.log(\"Current Schema: \", currentSchema);\n\n // Tell Dexie about current schema:\n // newDb.version(db.verno).stores(currentSchema);\n // Tell Dexie about next schema:\n //console.log(\"IndexedDB: Versioning DB to \"+(db.verno + 1)+ \" from tables \"+this.allTables(db));\n db.version(db.verno + 1).stores(schemaChanges);\n // Upgrade it:\n //console.log(\"IndexedDB: Upgrading DB\");\n return db.open().then (database => {\n //console.log(\"IndexedDB: Upgraded DB v\"+database.verno+\" to tables \"+this.allTables(database));\n return database;\n });\n }\n\n /*updateConfig (newConfig:CommonLibConfig) {\n if ((newConfig.indexedDbName!=null) && (newConfig.indexedDbName.length > 0)) {\n if( newConfig.indexedDbName!=this.dbName) {\n this.dbName=newConfig.indexedDbName;\n if (this.db?.isOpen ()) {\n console.warn (\"Changing the name of an Open IndexedDB database to \"+newConfig.indexedDbName);\n this.db.close();\n this.db = null; // Force reopen of db next time\n }\n IndexedDbStorageService.forceCloseDatabase();\n }\n }\n }*/\n\n withDatabase (): Promise<Dexie> {\n if (this.db==null) {\n\n //console.log(\"IndexedDB: Checking GlobalDB \"+this.dbName);\n if(IndexedDbStorageService.globalDb==null) {\n IndexedDbStorageService.globalDb = new Dexie(this.dbName, {allowEmptyDB:true, autoOpen:false});\n //console.log(\"IndexedDB: GlobalDB \"+this.dbName+\" created\");\n }\n this.db=IndexedDbStorageService.globalDb;\n if( !this.db.isOpen()) {\n //console.log(\"IndexedDB: Opening DB \"+this.dbName);\n return this.db.open().then(database => {\n //console.log (\"IndexedDB: DB \"+this.dbName+\" v\"+database.verno+\" opened with tables \"+this.allTables(database));\n return database;\n });\n }\n }\n return Promise.resolve(this.db);\n }\n\n ngOnDestroy () {\n //console.log(\"IndexedDB: ngOnDestroy called\");\n this.subscriptions.unsubscribe();\n IndexedDbStorageService.forceCloseDatabase();\n }\n\n allTables (db:Dexie): string {\n let ret=\"\";\n for (const table of db.tables) {\n ret=ret+\", \"+table.name;\n }\n return ret;\n }\n}\n","/*\n * Public API Surface of store\n */\n\nexport * from './indexeddb/indexed-db-storage.service';\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './public-api';\n"],"names":[],"mappings":";;;;;;AAAA;;AAEG;AAUG,MAAO,uBAAiC,SAAQ,uBAA0B,CAAA;AAU9E;;AAEG;AACI,IAAA,OAAO,kBAAkB,GAAA;;;AAG9B,QAAA,IAAI,IAAI,CAAC,QAAQ,IAAE,IAAI,EAAE;;;AAGvB,YAAA,IAAI,IAAI,CAAC,QAAQ,CAAC,MAAM,EAAE,EAAE;;;AAG1B,gBAAA,IAAI,CAAC,QAAQ,CAAC,KAAK,EAAE;;;;;;IAOpB,OAAO,mBAAmB,CAAE,MAAa,EAAA;;;QAG9C,OAAO,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,MAAK;;;AAGtC,SAAC,CAAC;;AAGJ,IAAA,WAAA,GAAA;QAIE,KAAK,eAAc;QAtCX,IAAE,CAAA,EAAA,GAAa,IAAI;QAEnB,IAAM,CAAA,MAAA,GAAG,uBAAuB;AAEhC,QAAA,IAAA,CAAA,aAAa,GAAG,IAAI,YAAY,EAAG;AAmC3C;;;AAGmB;;AAEnB,QAAA,IAAK,IAAY,CAAC,kCAAkC,IAAI,IAAI,EAAE;YAC3D,IAAY,CAAC,kCAAkC,GAAG,MAAM,uBAAuB,CAAC,kBAAkB,EAAE;;AAEvG,QAAA,IAAK,IAAY,CAAC,mCAAmC,IAAI,IAAI,EAAE;AAC5D,YAAA,IAAY,CAAC,mCAAmC,GA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AAA;;AAET,QAAA,IAAI,CAAC,aAAa,CAAC,WAAW,EAAE;QAChC,uBAAuB,CAAC,kBAAkB,EAAE;;AAG9C,IAAA,SAAS,CAAE,EAAQ,EAAA;QACjB,IAAI,GAAG,GAAC,EAAE;AACV,QAAA,KAAK,MAAM,KAAK,IAAI,EAAE,CAAC,MAAM,EAAE;YAC7B,GAAG,GAAC,GAAG,GAAC,IAAI,GAAC,KAAK,CAAC,IAAI;;AAEzB,QAAA,OAAO,GAAG;;8GAjOD,uBAAuB,EAAA,IAAA,EAAA,EAAA,EAAA,MAAA,EAAA,EAAA,CAAA,eAAA,CAAA,UAAA,EAAA,CAAA,CAAA;AAAvB,IAAA,SAAA,IAAA,CAAA,KAAA,GAAA,EAAA,CAAA,qBAAA,CAAA,EAAA,UAAA,EAAA,QAAA,EAAA,OAAA,EAAA,QAAA,EAAA,QAAA,EAAA,EAAA,EAAA,IAAA,EAAA,uBAAuB,cAFtB,MAAM,EAAA,CAAA,CAAA;;2FAEP,uBAAuB,EAAA,UAAA,EAAA,CAAA;kBAHnC,UAAU;AAAC,YAAA,IAAA,EAAA,CAAA;AACV,oBAAA,UAAU,EAAE;AACb,iBAAA;;;ACXD;;AAEG;;ACFH;;AAEG;;;;"}
|
|
1
|
+
{"version":3,"file":"xt-idb-store.mjs","sources":["../../../projects/idb-store/src/indexeddb/indexed-db-storage.service.ts","../../../projects/idb-store/src/public-api.ts","../../../projects/idb-store/src/xt-idb-store.ts"],"sourcesContent":["/**\n * Allow storing of entities in the browser local database\n */\nimport { from, Observable, Subscription } from 'rxjs';\nimport Dexie, { Table } from 'dexie';\nimport { Injectable, OnDestroy } from '@angular/core';\nimport { AbstractXtStoreProvider, UploadedDocumentInfo, XtStoreCriteria, XtStoreProviderHelper } from 'xt-store';\nimport { ManagedData } from 'xt-type';\n\n\n@Injectable({\n providedIn: 'root'\n})\nexport class IndexedDbStorageService<T extends ManagedData = ManagedData> extends AbstractXtStoreProvider<T> implements OnDestroy {\n\n protected static globalDb: Dexie|null;\n\n protected db: Dexie|null=null;\n\n protected dbName = \"Dont-code Sandbox Lib\";\n\n protected subscriptions = new Subscription ();\n\n /**\n * Enable test code to close a database between tests\n */\n public static forceCloseDatabase () {\n // eslint-disable-next-line no-restricted-syntax\n //console.debug(\"IndexedDB: In forceCloseDatabase\");\n if (this.globalDb!=null) {\n // eslint-disable-next-line no-restricted-syntax\n //console.debug(\"IndexedDB: GlobalDB Exist\");\n if (this.globalDb.isOpen()) {\n // eslint-disable-next-line no-restricted-syntax\n //console.debug(\"IndexedDB: Closing GlobalDB\");\n this.globalDb.close();\n // eslint-disable-next-line no-restricted-syntax\n //console.debug(\"IndexedDB: GlobalDB is closed\");\n }\n }\n }\n\n public static forceDeleteDatabase (dbName:string):Promise<void> {\n // eslint-disable-next-line no-restricted-syntax\n //console.debug(\"IndexedDB: In forceDeleteDatabase\");\n return Dexie.delete(dbName).then(() => {\n // eslint-disable-next-line no-restricted-syntax\n //console.debug(\"IndexedDB: Database \"+dbName+\" deleted\");\n });\n }\n\n constructor(/*protected values: ValueService,\n protected configService: CommonConfigService,\n @Optional () modelMgr?:DontCodeModelManager*/\n ) {\n super(/*modelMgr*/);\n /*this.updateConfig (configService.getConfig());\n this.subscriptions.add (configService.getUpdates().pipe (map ((newConfig) => {\n this.updateConfig(newConfig);\n })).subscribe());*/\n // Let unit tests close or delete the database between tests if needed\n if ((self as any)._indexedDbStorageServiceForceClose == null) {\n (self as any)._indexedDbStorageServiceForceClose = () => IndexedDbStorageService.forceCloseDatabase();\n }\n if ((self as any)._indexedDbStorageServiceForceDelete == null) {\n (self as any)._indexedDbStorageServiceForceDelete = (dbName:string) => IndexedDbStorageService.forceDeleteDatabase(dbName);\n }\n\n}\n\n deleteEntity(name: string, key: any): Promise<boolean> {\n return this.ensurePositionCanBeStored(name, false).then(table => {\n return table.delete(key).then(() => {\n return true;\n });\n });\n\n }\n\n loadEntity(name: string, key: any): Promise<T|undefined> {\n return this.ensurePositionCanBeStored(name, false).then (table => {\n return table.get(key);\n }).catch(reason => {\n console.warn(\"IndexedDB: Cannot load entity \"+key+\" : \"+reason);\n return undefined;\n });\n }\n\n override searchEntities(name: string, ...criteria: XtStoreCriteria[]): Observable<Array<T>> {\n return from (\n this.ensurePositionCanBeStored(name, false).then(table => {\n return table.toArray().then(list => {\n return XtStoreProviderHelper.applyFilters(list, ...criteria);\n });\n }).catch(reason => {\n // Probably table not found, just returns empty values\n console.warn(\"IndexedDB: Cannot search entity: \"+reason);\n return [];\n })\n );\n }\n\n canStoreDocument(): boolean {\n return false;\n }\n storeDocuments(toStore: File[]): Observable<UploadedDocumentInfo> {\n throw new Error(\"Impossible to store documents in IndexedDB.\");\n }\n\n storeEntity(name: string, entity: any): Promise<T> {\n return this.ensurePositionCanBeStored(name, true).then(table => {\n return table.put(entity).then(key => {\n if ((entity._id) && (entity._id!==key)) {\n return Promise.reject(\"Stored entity with id \"+key+\" different from \"+entity._id);\n } else {\n return entity;\n }\n\n });\n });\n }\n\n ensureEntityCanBeStored (description: any, create?:boolean):Promise<Table<T>> {\n if (description)\n return this.ensurePositionCanBeStored(description.name, create);\n else{\n return Promise.reject(\"Error called with null description\");\n }\n }\n\n ensurePositionCanBeStored (name: string, create?:boolean):Promise<Table<T>> {\n return this.withDatabase().then (db => {\n // We have to make sure the database is open before we can get the list of tables\n let table;\n try {\n table = db.table(name);\n } catch (error) {\n // Just ignore table not found\n }\n if (table != null) return Promise.resolve(table);\n\n if (create) {\n const tableDescription: { [key: string]: string } = {};\n tableDescription[name] = '++_id';\n return this.changeSchema(db, tableDescription).then(db => {\n return db.table(name);\n });\n } else {\n return Promise.reject(name + ' table not found');\n }\n });\n }\n\n protected changeSchema(db : Dexie, schemaChanges:any): Promise<Dexie> {\n //console.log(\"IndexedDB: Closing DB\");\n db.close();\n/* const newDb = new Dexie(db.name,{allowEmptyDB:true, autoOpen:false});\n\n newDb.on('blocked', ()=>false); // Silence console warning of blocked event.\n\n // Workaround: If DB is empty from tables, it needs to be recreated\n if (db.tables.length === 0) {\n return db.delete().then (value => {\n newDb.version(1.5).stores(schemaChanges);\n return newDb.open();\n })\n }\n\n // Extract current schema in dexie format:\n const currentSchema = db.tables.reduce((result:{[key:string]:any},{name, schema}) => {\n result[name] = [\n schema.primKey.src,\n ...schema.indexes.map(idx => idx.src)\n ].join(',');\n return result;\n }, {});\n*/\n //console.log(\"Version: \" + db.verno);\n //console.log(\"Current Schema: \", currentSchema);\n\n // Tell Dexie about current schema:\n // newDb.version(db.verno).stores(currentSchema);\n // Tell Dexie about next schema:\n //console.log(\"IndexedDB: Versioning DB to \"+(db.verno + 1)+ \" from tables \"+this.allTables(db));\n db.version(db.verno + 1).stores(schemaChanges);\n // Upgrade it:\n //console.log(\"IndexedDB: Upgrading DB\");\n return db.open().then (database => {\n //console.log(\"IndexedDB: Upgraded DB v\"+database.verno+\" to tables \"+this.allTables(database));\n return database;\n });\n }\n\n /*updateConfig (newConfig:CommonLibConfig) {\n if ((newConfig.indexedDbName!=null) && (newConfig.indexedDbName.length > 0)) {\n if( newConfig.indexedDbName!=this.dbName) {\n this.dbName=newConfig.indexedDbName;\n if (this.db?.isOpen ()) {\n console.warn (\"Changing the name of an Open IndexedDB database to \"+newConfig.indexedDbName);\n this.db.close();\n this.db = null; // Force reopen of db next time\n }\n IndexedDbStorageService.forceCloseDatabase();\n }\n }\n }*/\n\n withDatabase (): Promise<Dexie> {\n if (this.db==null) {\n\n //console.log(\"IndexedDB: Checking GlobalDB \"+this.dbName);\n if(IndexedDbStorageService.globalDb==null) {\n IndexedDbStorageService.globalDb = new Dexie(this.dbName, {allowEmptyDB:true, autoOpen:false});\n //console.log(\"IndexedDB: GlobalDB \"+this.dbName+\" created\");\n }\n this.db=IndexedDbStorageService.globalDb;\n if( !this.db.isOpen()) {\n //console.log(\"IndexedDB: Opening DB \"+this.dbName);\n return this.db.open().then(database => {\n //console.log (\"IndexedDB: DB \"+this.dbName+\" v\"+database.verno+\" opened with tables \"+this.allTables(database));\n return database;\n });\n }\n }\n return Promise.resolve(this.db);\n }\n\n ngOnDestroy () {\n //console.log(\"IndexedDB: ngOnDestroy called\");\n this.subscriptions.unsubscribe();\n IndexedDbStorageService.forceCloseDatabase();\n }\n\n allTables (db:Dexie): string {\n let ret=\"\";\n for (const table of db.tables) {\n ret=ret+\", \"+table.name;\n }\n return ret;\n }\n}\n","/*\n * Public API Surface of store\n */\n\nexport * from './indexeddb/indexed-db-storage.service';\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './public-api';\n"],"names":[],"mappings":";;;;;;AAAA;;AAEG;AAWG,MAAO,uBAA6D,SAAQ,uBAA0B,CAAA;AAU1G;;AAEG;AACI,IAAA,OAAO,kBAAkB,GAAA;;;AAG9B,QAAA,IAAI,IAAI,CAAC,QAAQ,IAAE,IAAI,EAAE;;;AAGvB,YAAA,IAAI,IAAI,CAAC,QAAQ,CAAC,MAAM,EAAE,EAAE;;;AAG1B,gBAAA,IAAI,CAAC,QAAQ,CAAC,KAAK,EAAE;;;;;;IAOpB,OAAO,mBAAmB,CAAE,MAAa,EAAA;;;QAG9C,OAAO,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,MAAK;;;AAGtC,SAAC,CAAC;;AAGJ,IAAA,WAAA,GAAA;QAIE,KAAK,eAAc;QAtCX,IAAE,CAAA,EAAA,GAAa,IAAI;QAEnB,IAAM,CAAA,MAAA,GAAG,uBAAuB;AAEhC,QAAA,IAAA,CAAA,aAAa,GAAG,IAAI,YAAY,EAAG;AAmC3C;;;AAGmB;;AAEnB,QAAA,IAAK,IAAY,CAAC,kCAAkC,IAAI,IAAI,EAAE;YAC3D,IAAY,CAAC,kCAAkC,GAAG,MAAM,uBAAuB,CAAC,kBAAkB,EAAE;;AAEvG,QAAA,IAAK,IAAY,CAAC,mCAAmC,IAAI,IAAI,EAAE;AAC5D,YAAA,IAAY,CAAC,mCAAmC,GAAG,CAAC,MAAa,KAAK,uBAAuB,CAAC,mBAAmB,CAAC,M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;QAChC,uBAAuB,CAAC,kBAAkB,EAAE;;AAG9C,IAAA,SAAS,CAAE,EAAQ,EAAA;QACjB,IAAI,GAAG,GAAC,EAAE;AACV,QAAA,KAAK,MAAM,KAAK,IAAI,EAAE,CAAC,MAAM,EAAE;YAC7B,GAAG,GAAC,GAAG,GAAC,IAAI,GAAC,KAAK,CAAC,IAAI;;AAEzB,QAAA,OAAO,GAAG;;+GAjOD,uBAAuB,EAAA,IAAA,EAAA,EAAA,EAAA,MAAA,EAAA,EAAA,CAAA,eAAA,CAAA,UAAA,EAAA,CAAA,CAAA;AAAvB,IAAA,SAAA,IAAA,CAAA,KAAA,GAAA,EAAA,CAAA,qBAAA,CAAA,EAAA,UAAA,EAAA,QAAA,EAAA,OAAA,EAAA,SAAA,EAAA,QAAA,EAAA,EAAA,EAAA,IAAA,EAAA,uBAAuB,cAFtB,MAAM,EAAA,CAAA,CAAA;;4FAEP,uBAAuB,EAAA,UAAA,EAAA,CAAA;kBAHnC,UAAU;AAAC,YAAA,IAAA,EAAA,CAAA;AACV,oBAAA,UAAU,EAAE;AACb,iBAAA;;;ACZD;;AAEG;;ACFH;;AAEG;;;;"}
|
|
@@ -5,8 +5,9 @@ import { Observable, Subscription } from 'rxjs';
|
|
|
5
5
|
import Dexie, { Table } from 'dexie';
|
|
6
6
|
import { OnDestroy } from '@angular/core';
|
|
7
7
|
import { AbstractXtStoreProvider, UploadedDocumentInfo, XtStoreCriteria } from 'xt-store';
|
|
8
|
+
import { ManagedData } from 'xt-type';
|
|
8
9
|
import * as i0 from "@angular/core";
|
|
9
|
-
export declare class IndexedDbStorageService<T =
|
|
10
|
+
export declare class IndexedDbStorageService<T extends ManagedData = ManagedData> extends AbstractXtStoreProvider<T> implements OnDestroy {
|
|
10
11
|
protected static globalDb: Dexie | null;
|
|
11
12
|
protected db: Dexie | null;
|
|
12
13
|
protected dbName: string;
|