diff --git a/libs/ngrx-toolkit/src/lib/storageSync/internal/storage.service.ts b/libs/ngrx-toolkit/src/lib/storageSync/internal/storage.service.ts index fb88c04..4de8119 100644 --- a/libs/ngrx-toolkit/src/lib/storageSync/internal/storage.service.ts +++ b/libs/ngrx-toolkit/src/lib/storageSync/internal/storage.service.ts @@ -1,13 +1,26 @@ import { inject, Injectable } from '@angular/core'; import { IndexedDBService } from './indexeddb.service'; +// export type Config = { +// storage: 'localStorage' | 'sessionStorage' | 'indexedDB'; +// key?: string; +// dbName?: string; +// storeName?: string; +// }; + export type IndexedDBConfig = { - storage: 'localStorage' | 'sessionStorage' | 'indexedDB'; - key?: string; - dbName?: string; - storeName?: string; + storage: 'indexedDB'; + dbName: string; + storeName: string; +}; + +export type StorageConfig = { + storage: 'localStorage' | 'sessionStorage'; + key: string; }; +export type Config = IndexedDBConfig | StorageConfig; + @Injectable({ providedIn: 'root', }) @@ -21,41 +34,27 @@ export class StorageService { } // get item from storage(localStorage, sessionStorage, indexedDB) - async getItem(config: IndexedDBConfig): Promise; + async getItem(config: Config): Promise; - async getItem(config: IndexedDBConfig): Promise { + async getItem(config: Config): Promise { if (config.storage === 'indexedDB') { const { dbName, storeName } = config; - if (dbName === undefined || storeName === undefined) { - throw new Error('dbName and storeName must be set'); - } - return await this.indexedDB.read(dbName, storeName); } if (this.storage === null) { throw new Error('Storage not set'); } - - if (config.key === undefined) { - throw new Error('key is undefined'); - } - return this.storage.getItem(config.key); } // set item in storage(localStorage, sessionStorage, indexedDB) - async setItem(config: IndexedDBConfig, value: string): Promise; + async setItem(config: Config, value: string): Promise; - async setItem(config: IndexedDBConfig, value: string): Promise { + async setItem(config: Config, value: string): Promise { if (config.storage === 'indexedDB') { const { dbName, storeName } = config; - - if (dbName === undefined || storeName === undefined) { - throw new Error('dbName and storeName must be set'); - } - return await this.indexedDB.write(dbName, storeName, value); } @@ -63,24 +62,16 @@ export class StorageService { throw new Error('Storage not set'); } - if (config.key === undefined) { - throw new Error('key is undefined'); - } - return this.storage.setItem(config.key, value); } - // - // // remove item from storage(localStorage, sessionStorage, indexedDB) - async removeItem(config: IndexedDBConfig): Promise; - async removeItem(config: IndexedDBConfig): Promise { + // remove item from storage(localStorage, sessionStorage, indexedDB) + async removeItem(config: Config): Promise; + + async removeItem(config: Config): Promise { if (config.storage === 'indexedDB') { const { dbName, storeName } = config; - if (dbName === undefined || storeName === undefined) { - throw new Error('dbName and storeName must be set'); - } - return await this.indexedDB.clear(dbName, storeName); } @@ -88,10 +79,6 @@ export class StorageService { throw new Error('Storage not set'); } - if (config.key === undefined) { - throw new Error('key is undefined'); - } - return this.storage.removeItem(config.key); } } diff --git a/libs/ngrx-toolkit/src/lib/storageSync/with-storage-sync.ts b/libs/ngrx-toolkit/src/lib/storageSync/with-storage-sync.ts index d1024ee..c8709f9 100644 --- a/libs/ngrx-toolkit/src/lib/storageSync/with-storage-sync.ts +++ b/libs/ngrx-toolkit/src/lib/storageSync/with-storage-sync.ts @@ -37,6 +37,24 @@ const StorageSyncStub: Pick< writeToStorage: NOOP, }; +export type IndexedDBSyncConfig = { + /** + * Allows selection between localStorage, sessionStorage, and indexedDB + * + */ + storage: 'indexedDB'; + + /** + * The name of the indexedDB database + */ + dbName: string; + + /** + * The store name in indexedDB (equivalent to a table name in SQL) + */ + storeName: string; +}; + export type SyncConfig = { /** * The key which is used to access the storage. @@ -71,17 +89,17 @@ export type SyncConfig = { * * Defaults to `localStorage` */ - storage: 'localStorage' | 'sessionStorage' | 'indexedDB'; + storage?: 'localStorage' | 'sessionStorage' | 'indexedDB'; /** * The name of the indexedDB database */ - dbName: string; + dbName?: string; /** * The store name in indexedDB (equivalent to a table name in SQL) */ - storeName: string; + storeName?: string; }; /** @@ -107,9 +125,9 @@ export function withStorageSync< select = (state: State) => state, parse = JSON.parse, stringify = JSON.stringify, - storage: storage = 'localStorage', - dbName, - storeName, + storage = 'localStorage', + dbName = '', + storeName = '', } = typeof configOrKey === 'string' ? { key: configOrKey } : configOrKey; return signalStoreFeature(