Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions packages/auth-client/src/db.ts
Original file line number Diff line number Diff line change
@@ -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<unknown>;
type IDBValidKey = string | number | Date | BufferSource | IDBValidKey[];
Expand Down Expand Up @@ -69,7 +69,7 @@ export class IdbKeyVal {
* @constructs an {@link IdbKeyVal}
*/
public static async create(options?: DBCreateOptions): Promise<IdbKeyVal> {
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);
}
Expand Down
21 changes: 19 additions & 2 deletions packages/auth-client/src/storage.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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<IdbKeyVal> {
Expand All @@ -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);
});
Expand Down