diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 4eaed9c50..df95e6e86 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -2,6 +2,10 @@ ## [Unreleased] +### Changed + +* feat: allow passing `DBCreateOptions` to `IdbStorage` constructor + ## [1.1.1] - 2024-03-19 * fix: work around `PublicKeyCredential` not being enumerable diff --git a/packages/auth-client/src/db.ts b/packages/auth-client/src/db.ts index a090ff86a..1642b0559 100644 --- a/packages/auth-client/src/db.ts +++ b/packages/auth-client/src/db.ts @@ -1,5 +1,5 @@ import { openDB, IDBPDatabase } from 'idb'; -import { isBrowser, KEY_STORAGE_DELEGATION, KEY_STORAGE_KEY } from './storage'; +import { DB_VERSION, isBrowser, KEY_STORAGE_DELEGATION, KEY_STORAGE_KEY } from './storage'; type Database = IDBPDatabase; type IDBValidKey = string | number | Date | BufferSource | IDBValidKey[]; @@ -69,7 +69,7 @@ export class IdbKeyVal { * @constructs an {@link IdbKeyVal} */ public static async create(options?: DBCreateOptions): Promise { - const { dbName = AUTH_DB_NAME, storeName = OBJECT_STORE_NAME, version = 1 } = options ?? {}; + const { dbName = AUTH_DB_NAME, storeName = OBJECT_STORE_NAME, version = DB_VERSION } = options ?? {}; const db = await _openDbStore(dbName, storeName, version); return new IdbKeyVal(db, storeName); } diff --git a/packages/auth-client/src/storage.ts b/packages/auth-client/src/storage.ts index 9812b3e77..aa9694d30 100644 --- a/packages/auth-client/src/storage.ts +++ b/packages/auth-client/src/storage.ts @@ -1,4 +1,4 @@ -import { IdbKeyVal } from './db'; +import { DBCreateOptions, IdbKeyVal } from './db'; export const KEY_STORAGE_KEY = 'identity'; export const KEY_STORAGE_DELEGATION = 'delegation'; @@ -70,6 +70,23 @@ export class LocalStorage implements AuthClientStorage { * @see implements {@link AuthClientStorage} */ export class IdbStorage implements AuthClientStorage { + #options: DBCreateOptions; + + /** + * @param options - DBCreateOptions + * @param options.dbName - name for the indexeddb database + * @param options.storeName - name for the indexeddb Data Store + * @param options.version - version of the database. Increment to safely upgrade + * @constructs an {@link IdbStorage} + * @example + * ```typescript + * const storage = new IdbStorage({ dbName: 'my-db', storeName: 'my-store', version: 2 }); + * ``` + */ + constructor(options?: DBCreateOptions) { + this.#options = options ?? {}; + } + // Initializes a KeyVal on first request private initializedDb: IdbKeyVal | undefined; get _db(): Promise { @@ -78,7 +95,7 @@ export class IdbStorage implements AuthClientStorage { resolve(this.initializedDb); return; } - IdbKeyVal.create({ version: DB_VERSION }).then(db => { + IdbKeyVal.create(this.#options).then(db => { this.initializedDb = db; resolve(db); });