Skip to content

Commit 919fc2d

Browse files
committed
[persisters] IndexedDbPersister
1 parent b74b50f commit 919fc2d

File tree

4 files changed

+57
-9
lines changed

4 files changed

+57
-9
lines changed

src/persisters/persister-indexed-db.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import {IdObj, objHas, objMap, objNew} from '../common/obj';
2-
import {Persister, PersisterListener} from '../types/persisters';
2+
import {
3+
IndexedDbPersister,
4+
createIndexedDbPersister as createIndexedDbPersisterDecl,
5+
} from '../types/persisters/persister-indexed-db';
36
import {Store, Table, Tables, Values} from '../types/store';
47
import {T, V} from '../common/strings';
58
import {arrayMap, arrayPush} from '../common/array';
@@ -10,8 +13,8 @@ import {
1013
stopInterval,
1114
} from '../common/other';
1215
import {Id} from '../types/common';
16+
import {PersisterListener} from '../types/persisters';
1317
import {createCustomPersister} from '../persisters';
14-
import {createIndexedDbPersister as createIndexedDbPersisterDecl} from '../types/persisters/persister-indexed-db';
1518

1619
const WINDOW = globalThis.window;
1720
const OBJECT_STORE_NAMES = [T, V];
@@ -48,7 +51,7 @@ export const createIndexedDbPersister = ((
4851
dbName: string,
4952
autoLoadIntervalSeconds = 1,
5053
onIgnoredError?: (error: any) => void,
51-
): Persister => {
54+
): IndexedDbPersister => {
5255
const forObjectStores = async (
5356
forObjectStore: (objectStore: IDBObjectStore, arg: any) => Promise<any>,
5457
args: any[] = [],
@@ -122,5 +125,5 @@ export const createIndexedDbPersister = ((
122125
delPersisterListener,
123126
onIgnoredError,
124127
['getDbName', dbName],
125-
);
128+
) as IndexedDbPersister;
126129
}) as typeof createIndexedDbPersisterDecl;

src/types/docs/persisters/persister-indexed-db.js

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,38 @@
66
* @module persister-indexed-db
77
*/
88
/// persister-indexed-db
9+
/**
10+
* The IndexedDbPersister interface is a minor extension to the Persister
11+
* interface.
12+
*
13+
* It simply provides an extra getDbName method for accessing the unique key of
14+
* the IndexedDB the Store is being persisted to.
15+
* @since v4.3.14
16+
*/
17+
/// IndexedDbPersister
18+
{
19+
/**
20+
* The getDbName method returns the unique key of the IndexedDB the Store is
21+
* being persisted to.
22+
* @returns The unique key of the IndexedDB.
23+
* @example
24+
* This example creates a Persister object against a newly-created Store and
25+
* then gets the unique key of the IndexedDB back out again.
26+
*
27+
* ```js
28+
* const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
29+
* const persister = createIndexedDbPersister(store, 'petStore');
30+
*
31+
* console.log(persister.getDbName());
32+
* // -> 'petStore'
33+
*
34+
* persister.destroy();
35+
* ```
36+
* @category Getter
37+
* @since v4.3.14
38+
*/
39+
/// IndexedDbPersister.getDbName
40+
}
941
/**
1042
* The createIndexedDbPersister function creates a Persister object that can
1143
* persist the Store to the browser's IndexedDB storage.
@@ -31,10 +63,10 @@
3163
* @param onIgnoredError An optional handler for the errors that the Persister
3264
* would otherwise ignore when trying to save or load data. This is suitable for
3365
* debugging persistence issues in a development environment.
34-
* @returns A reference to the new Persister object.
66+
* @returns A reference to the new IndexedDbPersister object.
3567
* @example
36-
* This example creates a Persister object and persists the Store to the
37-
* browser's IndexedDB storage.
68+
* This example creates a IndexedDbPersister object and persists the Store to
69+
* the browser's IndexedDB storage.
3870
*
3971
* ```js
4072
* const store = createStore()

src/types/persisters/persister-indexed-db.d.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,16 @@
33
import {Persister} from '../persisters';
44
import {Store} from '../store';
55

6+
/// IndexedDbPersister
7+
export interface IndexedDbPersister extends Persister {
8+
/// IndexedDbPersister.getDbName
9+
getDbName: () => string;
10+
}
11+
612
/// createIndexedDbPersister
713
export function createIndexedDbPersister(
814
store: Store,
915
dbName: string,
1016
autoLoadIntervalSeconds?: number,
1117
onIgnoredError?: (error: any) => void,
12-
): Persister;
18+
): IndexedDbPersister;

src/types/with-schemas/persisters/persister-indexed-db.d.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,17 @@
33
import {OptionalSchemas, Store} from '../store';
44
import {Persister} from '../persisters';
55

6+
/// IndexedDbPersister
7+
export interface IndexedDbPersister<Schemas extends OptionalSchemas>
8+
extends Persister<Schemas> {
9+
/// IndexedDbPersister.getDbName
10+
getDbName: () => string;
11+
}
12+
613
/// createIndexedDbPersister
714
export function createIndexedDbPersister<Schemas extends OptionalSchemas>(
815
store: Store<Schemas>,
916
dbName: string,
1017
autoLoadIntervalSeconds?: number,
1118
onIgnoredError?: (error: any) => void,
12-
): Persister<Schemas>;
19+
): IndexedDbPersister<Schemas>;

0 commit comments

Comments
 (0)