Skip to content

Commit

Permalink
refactor(with-storage-sync): integrate with-indexeddb into with-stora…
Browse files Browse the repository at this point in the history
…ge-sync(wip)
  • Loading branch information
mzkmnk committed Jan 28, 2025
1 parent 607eb58 commit 6f980ca
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 44 deletions.
63 changes: 25 additions & 38 deletions libs/ngrx-toolkit/src/lib/storageSync/internal/storage.service.ts
Original file line number Diff line number Diff line change
@@ -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',
})
Expand All @@ -21,77 +34,51 @@ export class StorageService {
}

// get item from storage(localStorage, sessionStorage, indexedDB)
async getItem(config: IndexedDBConfig): Promise<string | null>;
async getItem(config: Config): Promise<string | null>;

async getItem(config: IndexedDBConfig): Promise<string | null> {
async getItem(config: Config): Promise<string | null> {
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<void>;
async setItem(config: Config, value: string): Promise<void>;

async setItem(config: IndexedDBConfig, value: string): Promise<void> {
async setItem(config: Config, value: string): Promise<void> {
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);
}

if (this.storage === null) {
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<void>;

async removeItem(config: IndexedDBConfig): Promise<void> {
// remove item from storage(localStorage, sessionStorage, indexedDB)
async removeItem(config: Config): Promise<void>;

async removeItem(config: Config): Promise<void> {
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);
}

if (this.storage === null) {
throw new Error('Storage not set');
}

if (config.key === undefined) {
throw new Error('key is undefined');
}

return this.storage.removeItem(config.key);
}
}
30 changes: 24 additions & 6 deletions libs/ngrx-toolkit/src/lib/storageSync/with-storage-sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<State> = {
/**
* The key which is used to access the storage.
Expand Down Expand Up @@ -71,17 +89,17 @@ export type SyncConfig<State> = {
*
* 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;
};

/**
Expand All @@ -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(
Expand Down

0 comments on commit 6f980ca

Please sign in to comment.