@xyo-network/archivist-indexeddb 2.60.3 → 2.60.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/IndexedDbArchivist.js +51 -13
- package/dist/cjs/IndexedDbArchivist.js.map +1 -1
- package/dist/docs.json +1971 -1802
- package/dist/esm/IndexedDbArchivist.js +46 -9
- package/dist/esm/IndexedDbArchivist.js.map +1 -1
- package/dist/types/IndexedDbArchivist.d.ts +35 -9
- package/dist/types/IndexedDbArchivist.d.ts.map +1 -1
- package/package.json +14 -13
- package/src/IndexedDbArchivist.ts +57 -17
|
@@ -1,28 +1,59 @@
|
|
|
1
|
+
var IndexedDbArchivist_1;
|
|
1
2
|
import { __decorate } from "tslib";
|
|
3
|
+
import { assertEx } from '@xylabs/assert';
|
|
2
4
|
import { AbstractArchivist } from '@xyo-network/abstract-archivist';
|
|
3
5
|
import { ArchivistAllQuerySchema, ArchivistClearQuerySchema, ArchivistDeleteQuerySchema, ArchivistInsertQuerySchema, } from '@xyo-network/archivist-model';
|
|
4
6
|
import { PayloadHasher } from '@xyo-network/core';
|
|
5
7
|
import { creatableModule } from '@xyo-network/module';
|
|
6
|
-
import { clear, delMany, entries, getMany, setMany } from 'idb-keyval';
|
|
7
|
-
export const IndexedDbArchivistConfigSchema = 'network.xyo.module.config.archivist.
|
|
8
|
-
let IndexedDbArchivist = class IndexedDbArchivist extends AbstractArchivist {
|
|
8
|
+
import { clear, createStore, delMany, entries, getMany, setMany } from 'idb-keyval';
|
|
9
|
+
export const IndexedDbArchivistConfigSchema = 'network.xyo.module.config.archivist.indexeddb';
|
|
10
|
+
let IndexedDbArchivist = IndexedDbArchivist_1 = class IndexedDbArchivist extends AbstractArchivist {
|
|
9
11
|
static configSchema = IndexedDbArchivistConfigSchema;
|
|
12
|
+
static defaultDbName = 'archivist';
|
|
13
|
+
static defaultStoreName = 'payloads';
|
|
14
|
+
_db;
|
|
15
|
+
/**
|
|
16
|
+
* The database name. If not supplied via config, it defaults
|
|
17
|
+
* to the module name (not guaranteed to be unique) and if module
|
|
18
|
+
* name is not supplied, it defaults to `archivist`. This behavior
|
|
19
|
+
* biases towards a single, isolated DB per archivist which seems to
|
|
20
|
+
* make the most sense for 99% of use cases.
|
|
21
|
+
*/
|
|
22
|
+
get dbName() {
|
|
23
|
+
return this.config?.dbName ?? this.config?.name ?? IndexedDbArchivist_1.defaultDbName;
|
|
24
|
+
}
|
|
10
25
|
get queries() {
|
|
11
26
|
return [ArchivistAllQuerySchema, ArchivistClearQuerySchema, ArchivistDeleteQuerySchema, ArchivistInsertQuerySchema, ...super.queries];
|
|
12
27
|
}
|
|
28
|
+
/**
|
|
29
|
+
* The name of the object store. If not supplied via config, it defaults
|
|
30
|
+
* to `payloads`. The limitation of the current IndexedDB wrapper we're
|
|
31
|
+
* using is that it only supports a single object store per DB. See here:
|
|
32
|
+
* https://github.com/jakearchibald/idb-keyval/blob/main/custom-stores.md#defining-a-custom-database--store-name
|
|
33
|
+
* If this becomes a problem or we need migrations/transactions, we can
|
|
34
|
+
* move to this more-flexible library, which they recommend (and who
|
|
35
|
+
* recommends them for our simple use case of key-value storage):
|
|
36
|
+
* https://www.npmjs.com/package/idb
|
|
37
|
+
*/
|
|
38
|
+
get storeName() {
|
|
39
|
+
return this.config?.storeName ?? IndexedDbArchivist_1.defaultStoreName;
|
|
40
|
+
}
|
|
41
|
+
get db() {
|
|
42
|
+
return assertEx(this._db, 'DB not initialized');
|
|
43
|
+
}
|
|
13
44
|
async all() {
|
|
14
|
-
const result = await entries(this.
|
|
45
|
+
const result = await entries(this.db);
|
|
15
46
|
return result.map(([_hash, payload]) => payload);
|
|
16
47
|
}
|
|
17
48
|
async clear() {
|
|
18
|
-
await clear(this.
|
|
49
|
+
await clear(this.db);
|
|
19
50
|
}
|
|
20
51
|
async delete(hashes) {
|
|
21
|
-
await delMany(hashes, this.
|
|
52
|
+
await delMany(hashes, this.db);
|
|
22
53
|
return hashes.map((_) => true);
|
|
23
54
|
}
|
|
24
55
|
async get(hashes) {
|
|
25
|
-
const result = await getMany(hashes, this.
|
|
56
|
+
const result = await getMany(hashes, this.db);
|
|
26
57
|
return result;
|
|
27
58
|
}
|
|
28
59
|
async insert(payloads) {
|
|
@@ -30,12 +61,18 @@ let IndexedDbArchivist = class IndexedDbArchivist extends AbstractArchivist {
|
|
|
30
61
|
const hash = await PayloadHasher.hashAsync(payload);
|
|
31
62
|
return [hash, payload];
|
|
32
63
|
}));
|
|
33
|
-
await setMany(entries, this.
|
|
64
|
+
await setMany(entries, this.db);
|
|
34
65
|
const result = await this.bindQueryResult({ payloads, schema: ArchivistInsertQuerySchema }, payloads);
|
|
35
66
|
return [result[0]];
|
|
36
67
|
}
|
|
68
|
+
async start() {
|
|
69
|
+
await super.start();
|
|
70
|
+
// NOTE: We could defer this creation to first access but we
|
|
71
|
+
// want to fail fast here in case something is wrong
|
|
72
|
+
this._db = createStore(this.dbName, this.storeName);
|
|
73
|
+
}
|
|
37
74
|
};
|
|
38
|
-
IndexedDbArchivist = __decorate([
|
|
75
|
+
IndexedDbArchivist = IndexedDbArchivist_1 = __decorate([
|
|
39
76
|
creatableModule()
|
|
40
77
|
], IndexedDbArchivist);
|
|
41
78
|
export { IndexedDbArchivist };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"IndexedDbArchivist.js","sourceRoot":"","sources":["../../src/IndexedDbArchivist.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"IndexedDbArchivist.js","sourceRoot":"","sources":["../../src/IndexedDbArchivist.ts"],"names":[],"mappings":";;AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAA;AACzC,OAAO,EAAE,iBAAiB,EAAE,MAAM,iCAAiC,CAAA;AACnE,OAAO,EACL,uBAAuB,EACvB,yBAAyB,EAEzB,0BAA0B,EAC1B,0BAA0B,GAG3B,MAAM,8BAA8B,CAAA;AAErC,OAAO,EAAE,aAAa,EAAE,MAAM,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kBAAkB"}
|
|
@@ -3,25 +3,51 @@ import { ArchivistConfig, ArchivistModuleEventData, ArchivistParams } from '@xyo
|
|
|
3
3
|
import { BoundWitness } from '@xyo-network/boundwitness-model';
|
|
4
4
|
import { AnyConfigSchema } from '@xyo-network/module';
|
|
5
5
|
import { Payload } from '@xyo-network/payload-model';
|
|
6
|
-
|
|
7
|
-
export type IndexedDbArchivistConfigSchema = 'network.xyo.module.config.archivist.storage';
|
|
6
|
+
export type IndexedDbArchivistConfigSchema = 'network.xyo.module.config.archivist.indexeddb';
|
|
8
7
|
export declare const IndexedDbArchivistConfigSchema: IndexedDbArchivistConfigSchema;
|
|
9
8
|
export type IndexedDbArchivistConfig = ArchivistConfig<{
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
9
|
+
/**
|
|
10
|
+
* The database name
|
|
11
|
+
*/
|
|
12
|
+
dbName?: string;
|
|
13
13
|
schema: IndexedDbArchivistConfigSchema;
|
|
14
|
+
/**
|
|
15
|
+
* The name of the object store
|
|
16
|
+
*/
|
|
17
|
+
storeName?: string;
|
|
14
18
|
}>;
|
|
15
|
-
export type IndexedDbArchivistParams = ArchivistParams<AnyConfigSchema<IndexedDbArchivistConfig
|
|
16
|
-
indexedDB?: UseStore;
|
|
17
|
-
}>;
|
|
19
|
+
export type IndexedDbArchivistParams = ArchivistParams<AnyConfigSchema<IndexedDbArchivistConfig>>;
|
|
18
20
|
export declare class IndexedDbArchivist<TParams extends IndexedDbArchivistParams = IndexedDbArchivistParams, TEventData extends ArchivistModuleEventData = ArchivistModuleEventData> extends AbstractArchivist<TParams, TEventData> {
|
|
19
|
-
static configSchema: "network.xyo.module.config.archivist.
|
|
21
|
+
static configSchema: "network.xyo.module.config.archivist.indexeddb";
|
|
22
|
+
static defaultDbName: string;
|
|
23
|
+
static defaultStoreName: string;
|
|
24
|
+
private _db;
|
|
25
|
+
/**
|
|
26
|
+
* The database name. If not supplied via config, it defaults
|
|
27
|
+
* to the module name (not guaranteed to be unique) and if module
|
|
28
|
+
* name is not supplied, it defaults to `archivist`. This behavior
|
|
29
|
+
* biases towards a single, isolated DB per archivist which seems to
|
|
30
|
+
* make the most sense for 99% of use cases.
|
|
31
|
+
*/
|
|
32
|
+
get dbName(): string;
|
|
20
33
|
get queries(): string[];
|
|
34
|
+
/**
|
|
35
|
+
* The name of the object store. If not supplied via config, it defaults
|
|
36
|
+
* to `payloads`. The limitation of the current IndexedDB wrapper we're
|
|
37
|
+
* using is that it only supports a single object store per DB. See here:
|
|
38
|
+
* https://github.com/jakearchibald/idb-keyval/blob/main/custom-stores.md#defining-a-custom-database--store-name
|
|
39
|
+
* If this becomes a problem or we need migrations/transactions, we can
|
|
40
|
+
* move to this more-flexible library, which they recommend (and who
|
|
41
|
+
* recommends them for our simple use case of key-value storage):
|
|
42
|
+
* https://www.npmjs.com/package/idb
|
|
43
|
+
*/
|
|
44
|
+
get storeName(): string;
|
|
45
|
+
private get db();
|
|
21
46
|
all(): Promise<Payload[]>;
|
|
22
47
|
clear(): Promise<void>;
|
|
23
48
|
delete(hashes: string[]): Promise<boolean[]>;
|
|
24
49
|
get(hashes: string[]): Promise<Payload[]>;
|
|
25
50
|
insert(payloads: Payload[]): Promise<BoundWitness[]>;
|
|
51
|
+
start(): Promise<void>;
|
|
26
52
|
}
|
|
27
53
|
//# sourceMappingURL=IndexedDbArchivist.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"IndexedDbArchivist.d.ts","sourceRoot":"","sources":["../../src/IndexedDbArchivist.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"IndexedDbArchivist.d.ts","sourceRoot":"","sources":["../../src/IndexedDbArchivist.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,MAAM,iCAAiC,CAAA;AACnE,OAAO,EAGL,eAAe,EAGf,wBAAwB,EACxB,eAAe,EAChB,MAAM,8BAA8B,CAAA;AACrC,OAAO,EAAE,YAAY,EAAE,MAAM,iCAAiC,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3C,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;CAMtC"}
|
package/package.json
CHANGED
|
@@ -10,23 +10,24 @@
|
|
|
10
10
|
"url": "https://github.com/XYOracleNetwork/sdk-xyo-client-js/issues"
|
|
11
11
|
},
|
|
12
12
|
"dependencies": {
|
|
13
|
-
"@
|
|
14
|
-
"@xyo-network/archivist
|
|
15
|
-
"@xyo-network/
|
|
16
|
-
"@xyo-network/
|
|
17
|
-
"@xyo-network/
|
|
18
|
-
"@xyo-network/
|
|
19
|
-
"@xyo-network/payload-
|
|
20
|
-
"@xyo-network/
|
|
13
|
+
"@xylabs/assert": "^2.9.1",
|
|
14
|
+
"@xyo-network/abstract-archivist": "^2.60.5",
|
|
15
|
+
"@xyo-network/archivist-model": "^2.60.5",
|
|
16
|
+
"@xyo-network/boundwitness-model": "^2.60.5",
|
|
17
|
+
"@xyo-network/core": "^2.60.5",
|
|
18
|
+
"@xyo-network/module": "^2.60.5",
|
|
19
|
+
"@xyo-network/payload-model": "^2.60.5",
|
|
20
|
+
"@xyo-network/payload-wrapper": "^2.60.5",
|
|
21
|
+
"@xyo-network/promise": "^2.60.5",
|
|
21
22
|
"idb-keyval": "^6.2.1"
|
|
22
23
|
},
|
|
23
24
|
"devDependencies": {
|
|
24
|
-
"@xylabs/delay": "^2.
|
|
25
|
+
"@xylabs/delay": "^2.9.1",
|
|
25
26
|
"@xylabs/ts-scripts-yarn3": "^2.17.13",
|
|
26
27
|
"@xylabs/tsconfig": "^2.17.13",
|
|
27
|
-
"@xyo-network/archivist-wrapper": "^2.60.
|
|
28
|
-
"@xyo-network/boundwitness-wrapper": "^2.60.
|
|
29
|
-
"@xyo-network/plugins": "^2.60.
|
|
28
|
+
"@xyo-network/archivist-wrapper": "^2.60.5",
|
|
29
|
+
"@xyo-network/boundwitness-wrapper": "^2.60.5",
|
|
30
|
+
"@xyo-network/plugins": "^2.60.5",
|
|
30
31
|
"fake-indexeddb": "^4.0.1",
|
|
31
32
|
"typescript": "^5.1.3"
|
|
32
33
|
},
|
|
@@ -63,5 +64,5 @@
|
|
|
63
64
|
},
|
|
64
65
|
"sideEffects": false,
|
|
65
66
|
"types": "dist/types/index.d.ts",
|
|
66
|
-
"version": "2.60.
|
|
67
|
+
"version": "2.60.5"
|
|
67
68
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { assertEx } from '@xylabs/assert'
|
|
1
2
|
import { AbstractArchivist } from '@xyo-network/abstract-archivist'
|
|
2
3
|
import {
|
|
3
4
|
ArchivistAllQuerySchema,
|
|
@@ -12,24 +13,24 @@ import { BoundWitness } from '@xyo-network/boundwitness-model'
|
|
|
12
13
|
import { PayloadHasher } from '@xyo-network/core'
|
|
13
14
|
import { AnyConfigSchema, creatableModule } from '@xyo-network/module'
|
|
14
15
|
import { Payload } from '@xyo-network/payload-model'
|
|
15
|
-
import { clear, delMany, entries, getMany, setMany, UseStore } from 'idb-keyval'
|
|
16
|
+
import { clear, createStore, delMany, entries, getMany, setMany, UseStore } from 'idb-keyval'
|
|
16
17
|
|
|
17
|
-
export type IndexedDbArchivistConfigSchema = 'network.xyo.module.config.archivist.
|
|
18
|
-
export const IndexedDbArchivistConfigSchema: IndexedDbArchivistConfigSchema = 'network.xyo.module.config.archivist.
|
|
18
|
+
export type IndexedDbArchivistConfigSchema = 'network.xyo.module.config.archivist.indexeddb'
|
|
19
|
+
export const IndexedDbArchivistConfigSchema: IndexedDbArchivistConfigSchema = 'network.xyo.module.config.archivist.indexeddb'
|
|
19
20
|
|
|
20
21
|
export type IndexedDbArchivistConfig = ArchivistConfig<{
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
22
|
+
/**
|
|
23
|
+
* The database name
|
|
24
|
+
*/
|
|
25
|
+
dbName?: string
|
|
24
26
|
schema: IndexedDbArchivistConfigSchema
|
|
27
|
+
/**
|
|
28
|
+
* The name of the object store
|
|
29
|
+
*/
|
|
30
|
+
storeName?: string
|
|
25
31
|
}>
|
|
26
32
|
|
|
27
|
-
export type IndexedDbArchivistParams = ArchivistParams<
|
|
28
|
-
AnyConfigSchema<IndexedDbArchivistConfig>,
|
|
29
|
-
{
|
|
30
|
-
indexedDB?: UseStore
|
|
31
|
-
}
|
|
32
|
-
>
|
|
33
|
+
export type IndexedDbArchivistParams = ArchivistParams<AnyConfigSchema<IndexedDbArchivistConfig>>
|
|
33
34
|
|
|
34
35
|
@creatableModule()
|
|
35
36
|
export class IndexedDbArchivist<
|
|
@@ -37,27 +38,59 @@ export class IndexedDbArchivist<
|
|
|
37
38
|
TEventData extends ArchivistModuleEventData = ArchivistModuleEventData,
|
|
38
39
|
> extends AbstractArchivist<TParams, TEventData> {
|
|
39
40
|
static override configSchema = IndexedDbArchivistConfigSchema
|
|
41
|
+
static defaultDbName = 'archivist'
|
|
42
|
+
static defaultStoreName = 'payloads'
|
|
43
|
+
|
|
44
|
+
private _db: UseStore | undefined
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* The database name. If not supplied via config, it defaults
|
|
48
|
+
* to the module name (not guaranteed to be unique) and if module
|
|
49
|
+
* name is not supplied, it defaults to `archivist`. This behavior
|
|
50
|
+
* biases towards a single, isolated DB per archivist which seems to
|
|
51
|
+
* make the most sense for 99% of use cases.
|
|
52
|
+
*/
|
|
53
|
+
get dbName() {
|
|
54
|
+
return this.config?.dbName ?? this.config?.name ?? IndexedDbArchivist.defaultDbName
|
|
55
|
+
}
|
|
40
56
|
|
|
41
57
|
override get queries() {
|
|
42
58
|
return [ArchivistAllQuerySchema, ArchivistClearQuerySchema, ArchivistDeleteQuerySchema, ArchivistInsertQuerySchema, ...super.queries]
|
|
43
59
|
}
|
|
60
|
+
/**
|
|
61
|
+
* The name of the object store. If not supplied via config, it defaults
|
|
62
|
+
* to `payloads`. The limitation of the current IndexedDB wrapper we're
|
|
63
|
+
* using is that it only supports a single object store per DB. See here:
|
|
64
|
+
* https://github.com/jakearchibald/idb-keyval/blob/main/custom-stores.md#defining-a-custom-database--store-name
|
|
65
|
+
* If this becomes a problem or we need migrations/transactions, we can
|
|
66
|
+
* move to this more-flexible library, which they recommend (and who
|
|
67
|
+
* recommends them for our simple use case of key-value storage):
|
|
68
|
+
* https://www.npmjs.com/package/idb
|
|
69
|
+
*/
|
|
70
|
+
get storeName() {
|
|
71
|
+
return this.config?.storeName ?? IndexedDbArchivist.defaultStoreName
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
private get db(): UseStore {
|
|
75
|
+
return assertEx(this._db, 'DB not initialized')
|
|
76
|
+
}
|
|
44
77
|
|
|
45
78
|
override async all(): Promise<Payload[]> {
|
|
46
|
-
const result = await entries<string, Payload>(this.
|
|
79
|
+
const result = await entries<string, Payload>(this.db)
|
|
47
80
|
return result.map<Payload>(([_hash, payload]) => payload)
|
|
48
81
|
}
|
|
49
82
|
|
|
50
83
|
override async clear(): Promise<void> {
|
|
51
|
-
await clear(this.
|
|
84
|
+
await clear(this.db)
|
|
52
85
|
}
|
|
53
86
|
|
|
54
87
|
override async delete(hashes: string[]): Promise<boolean[]> {
|
|
55
|
-
await delMany(hashes, this.
|
|
88
|
+
await delMany(hashes, this.db)
|
|
56
89
|
return hashes.map((_) => true)
|
|
57
90
|
}
|
|
58
91
|
|
|
59
92
|
override async get(hashes: string[]): Promise<Payload[]> {
|
|
60
|
-
const result = await getMany<Payload>(hashes, this.
|
|
93
|
+
const result = await getMany<Payload>(hashes, this.db)
|
|
61
94
|
return result
|
|
62
95
|
}
|
|
63
96
|
|
|
@@ -68,8 +101,15 @@ export class IndexedDbArchivist<
|
|
|
68
101
|
return [hash, payload]
|
|
69
102
|
}),
|
|
70
103
|
)
|
|
71
|
-
await setMany(entries, this.
|
|
104
|
+
await setMany(entries, this.db)
|
|
72
105
|
const result = await this.bindQueryResult({ payloads, schema: ArchivistInsertQuerySchema }, payloads)
|
|
73
106
|
return [result[0]]
|
|
74
107
|
}
|
|
108
|
+
|
|
109
|
+
override async start(): Promise<void> {
|
|
110
|
+
await super.start()
|
|
111
|
+
// NOTE: We could defer this creation to first access but we
|
|
112
|
+
// want to fail fast here in case something is wrong
|
|
113
|
+
this._db = createStore(this.dbName, this.storeName)
|
|
114
|
+
}
|
|
75
115
|
}
|