@xyo-network/archivist-indexeddb 2.87.2 → 2.88.0
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/browser/Archivist.d.cts +14 -2
- package/dist/browser/Archivist.d.cts.map +1 -1
- package/dist/browser/Archivist.d.mts +14 -2
- package/dist/browser/Archivist.d.mts.map +1 -1
- package/dist/browser/Archivist.d.ts +14 -2
- package/dist/browser/Archivist.d.ts.map +1 -1
- package/dist/browser/index.cjs +88 -45
- package/dist/browser/index.cjs.map +1 -1
- package/dist/browser/index.js +88 -45
- package/dist/browser/index.js.map +1 -1
- package/dist/node/Archivist.d.cts +14 -2
- package/dist/node/Archivist.d.cts.map +1 -1
- package/dist/node/Archivist.d.mts +14 -2
- package/dist/node/Archivist.d.mts.map +1 -1
- package/dist/node/Archivist.d.ts +14 -2
- package/dist/node/Archivist.d.ts.map +1 -1
- package/dist/node/index.cjs +88 -45
- package/dist/node/index.cjs.map +1 -1
- package/dist/node/index.js +88 -45
- package/dist/node/index.js.map +1 -1
- package/package.json +10 -10
- package/src/Archivist.ts +113 -45
|
@@ -14,7 +14,6 @@ export declare class IndexedDbArchivist<TParams extends IndexedDbArchivistParams
|
|
|
14
14
|
private static readonly schemaIndex;
|
|
15
15
|
static readonly hashIndexName: string;
|
|
16
16
|
static readonly schemaIndexName: string;
|
|
17
|
-
private _db;
|
|
18
17
|
/**
|
|
19
18
|
* The database name. If not supplied via config, it defaults
|
|
20
19
|
* to the module name (not guaranteed to be unique) and if module
|
|
@@ -33,7 +32,9 @@ export declare class IndexedDbArchivist<TParams extends IndexedDbArchivistParams
|
|
|
33
32
|
* to `payloads`.
|
|
34
33
|
*/
|
|
35
34
|
get storeName(): string;
|
|
36
|
-
|
|
35
|
+
/**
|
|
36
|
+
* The indexes to create on the store
|
|
37
|
+
*/
|
|
37
38
|
private get indexes();
|
|
38
39
|
protected allHandler(): Promise<Payload[]>;
|
|
39
40
|
protected clearHandler(): Promise<void>;
|
|
@@ -41,5 +42,16 @@ export declare class IndexedDbArchivist<TParams extends IndexedDbArchivistParams
|
|
|
41
42
|
protected getHandler(hashes: string[]): Promise<Payload[]>;
|
|
42
43
|
protected insertHandler(payloads: Payload[]): Promise<Payload[]>;
|
|
43
44
|
protected startHandler(): Promise<boolean>;
|
|
45
|
+
/**
|
|
46
|
+
* Returns that the desired DB/Store initialized to the correct version
|
|
47
|
+
* @returns The initialized DB
|
|
48
|
+
*/
|
|
49
|
+
private getInitializedDb;
|
|
50
|
+
/**
|
|
51
|
+
* Executes a callback with the initialized DB and then closes the db
|
|
52
|
+
* @param callback The method to execute with the initialized DB
|
|
53
|
+
* @returns
|
|
54
|
+
*/
|
|
55
|
+
private useDb;
|
|
44
56
|
}
|
|
45
57
|
//# sourceMappingURL=Archivist.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Archivist.d.ts","sourceRoot":"","sources":["../../src/Archivist.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"Archivist.d.ts","sourceRoot":"","sources":["../../src/Archivist.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,MAAM,iCAAiC,CAAA;AACnE,OAAO,EAKL,wBAAwB,EAGzB,MAAM,8BAA8B,CAAA;AAGrC,OAAO,EAAE,OAAO,EAAE,MAAM,4BAA4B,CAAA;AAIpD,OAAO,EAAE,wBAAwB,EAAE,MAAM,UAAU,CAAA;AAEnD,MAAM,WAAW,YAAY;IAC3B,CAAC,CAAC,EAAE,MAAM,GAAG,OAAO,CAAA;CACrB;AAED,qBACa,kBAAkB,CAC7B,OAAO,SAAS,wBAAwB,GAAG,wBAAwB,EACnE,UAAU,SAAS,wBAAwB,GAAG,wBAAwB,CACtE,SAAQ,iBAAiB,CAAC,OAAO,EAAE,UAAU,CAAC;IAC9C,OAAgB,aAAa,6CAAmC;IAChE,MAAM,CAAC,QAAQ,CAAC,aAAa,eAAc;IAC3C,MAAM,CAAC,QAAQ,CAAC,gBAAgB,KAAI;IACpC,MAAM,CAAC,QAAQ,CAAC,gBAAgB,cAAa;IAC7C,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,SAAS,CAA2E;IAC5G,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,WAAW,CAA6E;IAEhH,MAAM,CAAC,QAAQ,CAAC,aAAa,SAAuD;IAEpF,MAAM,CAAC,QAAQ,CAAC,eAAe,SAAyD;IAExF;;;;;;OAMG;IACH,IAAI,MAAM,WAET;IAED;;OAEG;IACH,IAAI,SAAS,WAEZ;IAED,IAAa,OAAO,aAEnB;IAED;;;OAGG;IACH,IAAI,SAAS,WAEZ;IAED;;OAEG;IACH,OAAO,KAAK,OAAO,GAElB;cAEwB,UAAU,IAAI,OAAO,CAAC,OAAO,EAAE,CAAC;cAOhC,YAAY,IAAI,OAAO,CAAC,IAAI,CAAC;cAI7B,aAAa,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;cAsBlD,UAAU,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,OAAO,EAAE,CAAC;cAOhD,aAAa,CAAC,QAAQ,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC,OAAO,EAAE,CAAC;cAmCtD,YAAY;IAQrC;;;OAGG;YACW,gBAAgB;IAiD9B;;;;OAIG;YACW,KAAK;CAWpB"}
|
|
@@ -14,7 +14,6 @@ export declare class IndexedDbArchivist<TParams extends IndexedDbArchivistParams
|
|
|
14
14
|
private static readonly schemaIndex;
|
|
15
15
|
static readonly hashIndexName: string;
|
|
16
16
|
static readonly schemaIndexName: string;
|
|
17
|
-
private _db;
|
|
18
17
|
/**
|
|
19
18
|
* The database name. If not supplied via config, it defaults
|
|
20
19
|
* to the module name (not guaranteed to be unique) and if module
|
|
@@ -33,7 +32,9 @@ export declare class IndexedDbArchivist<TParams extends IndexedDbArchivistParams
|
|
|
33
32
|
* to `payloads`.
|
|
34
33
|
*/
|
|
35
34
|
get storeName(): string;
|
|
36
|
-
|
|
35
|
+
/**
|
|
36
|
+
* The indexes to create on the store
|
|
37
|
+
*/
|
|
37
38
|
private get indexes();
|
|
38
39
|
protected allHandler(): Promise<Payload[]>;
|
|
39
40
|
protected clearHandler(): Promise<void>;
|
|
@@ -41,5 +42,16 @@ export declare class IndexedDbArchivist<TParams extends IndexedDbArchivistParams
|
|
|
41
42
|
protected getHandler(hashes: string[]): Promise<Payload[]>;
|
|
42
43
|
protected insertHandler(payloads: Payload[]): Promise<Payload[]>;
|
|
43
44
|
protected startHandler(): Promise<boolean>;
|
|
45
|
+
/**
|
|
46
|
+
* Returns that the desired DB/Store initialized to the correct version
|
|
47
|
+
* @returns The initialized DB
|
|
48
|
+
*/
|
|
49
|
+
private getInitializedDb;
|
|
50
|
+
/**
|
|
51
|
+
* Executes a callback with the initialized DB and then closes the db
|
|
52
|
+
* @param callback The method to execute with the initialized DB
|
|
53
|
+
* @returns
|
|
54
|
+
*/
|
|
55
|
+
private useDb;
|
|
44
56
|
}
|
|
45
57
|
//# sourceMappingURL=Archivist.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Archivist.d.ts","sourceRoot":"","sources":["../../src/Archivist.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"Archivist.d.ts","sourceRoot":"","sources":["../../src/Archivist.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,MAAM,iCAAiC,CAAA;AACnE,OAAO,EAKL,wBAAwB,EAGzB,MAAM,8BAA8B,CAAA;AAGrC,OAAO,EAAE,OAAO,EAAE,MAAM,4BAA4B,CAAA;AAIpD,OAAO,EAAE,wBAAwB,EAAE,MAAM,UAAU,CAAA;AAEnD,MAAM,WAAW,YAAY;IAC3B,CAAC,CAAC,EAAE,MAAM,GAAG,OAAO,CAAA;CACrB;AAED,qBACa,kBAAkB,CAC7B,OAAO,SAAS,wBAAwB,GAAG,wBAAwB,EACnE,UAAU,SAAS,wBAAwB,GAAG,wBAAwB,CACtE,SAAQ,iBAAiB,CAAC,OAAO,EAAE,UAAU,CAAC;IAC9C,OAAgB,aAAa,6CAAmC;IAChE,MAAM,CAAC,QAAQ,CAAC,aAAa,eAAc;IAC3C,MAAM,CAAC,QAAQ,CAAC,gBAAgB,KAAI;IACpC,MAAM,CAAC,QAAQ,CAAC,gBAAgB,cAAa;IAC7C,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,SAAS,CAA2E;IAC5G,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,WAAW,CAA6E;IAEhH,MAAM,CAAC,QAAQ,CAAC,aAAa,SAAuD;IAEpF,MAAM,CAAC,QAAQ,CAAC,eAAe,SAAyD;IAExF;;;;;;OAMG;IACH,IAAI,MAAM,WAET;IAED;;OAEG;IACH,IAAI,SAAS,WAEZ;IAED,IAAa,OAAO,aAEnB;IAED;;;OAGG;IACH,IAAI,SAAS,WAEZ;IAED;;OAEG;IACH,OAAO,KAAK,OAAO,GAElB;cAEwB,UAAU,IAAI,OAAO,CAAC,OAAO,EAAE,CAAC;cAOhC,YAAY,IAAI,OAAO,CAAC,IAAI,CAAC;cAI7B,aAAa,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;cAsBlD,UAAU,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,OAAO,EAAE,CAAC;cAOhD,aAAa,CAAC,QAAQ,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC,OAAO,EAAE,CAAC;cAmCtD,YAAY;IAQrC;;;OAGG;YACW,gBAAgB;IAiD9B;;;;OAIG;YACW,KAAK;CAWpB"}
|
|
@@ -14,7 +14,6 @@ export declare class IndexedDbArchivist<TParams extends IndexedDbArchivistParams
|
|
|
14
14
|
private static readonly schemaIndex;
|
|
15
15
|
static readonly hashIndexName: string;
|
|
16
16
|
static readonly schemaIndexName: string;
|
|
17
|
-
private _db;
|
|
18
17
|
/**
|
|
19
18
|
* The database name. If not supplied via config, it defaults
|
|
20
19
|
* to the module name (not guaranteed to be unique) and if module
|
|
@@ -33,7 +32,9 @@ export declare class IndexedDbArchivist<TParams extends IndexedDbArchivistParams
|
|
|
33
32
|
* to `payloads`.
|
|
34
33
|
*/
|
|
35
34
|
get storeName(): string;
|
|
36
|
-
|
|
35
|
+
/**
|
|
36
|
+
* The indexes to create on the store
|
|
37
|
+
*/
|
|
37
38
|
private get indexes();
|
|
38
39
|
protected allHandler(): Promise<Payload[]>;
|
|
39
40
|
protected clearHandler(): Promise<void>;
|
|
@@ -41,5 +42,16 @@ export declare class IndexedDbArchivist<TParams extends IndexedDbArchivistParams
|
|
|
41
42
|
protected getHandler(hashes: string[]): Promise<Payload[]>;
|
|
42
43
|
protected insertHandler(payloads: Payload[]): Promise<Payload[]>;
|
|
43
44
|
protected startHandler(): Promise<boolean>;
|
|
45
|
+
/**
|
|
46
|
+
* Returns that the desired DB/Store initialized to the correct version
|
|
47
|
+
* @returns The initialized DB
|
|
48
|
+
*/
|
|
49
|
+
private getInitializedDb;
|
|
50
|
+
/**
|
|
51
|
+
* Executes a callback with the initialized DB and then closes the db
|
|
52
|
+
* @param callback The method to execute with the initialized DB
|
|
53
|
+
* @returns
|
|
54
|
+
*/
|
|
55
|
+
private useDb;
|
|
44
56
|
}
|
|
45
57
|
//# sourceMappingURL=Archivist.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Archivist.d.ts","sourceRoot":"","sources":["../../src/Archivist.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"Archivist.d.ts","sourceRoot":"","sources":["../../src/Archivist.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,MAAM,iCAAiC,CAAA;AACnE,OAAO,EAKL,wBAAwB,EAGzB,MAAM,8BAA8B,CAAA;AAGrC,OAAO,EAAE,OAAO,EAAE,MAAM,4BAA4B,CAAA;AAIpD,OAAO,EAAE,wBAAw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ist/browser/index.cjs
CHANGED
|
@@ -28,7 +28,6 @@ __export(src_exports, {
|
|
|
28
28
|
module.exports = __toCommonJS(src_exports);
|
|
29
29
|
|
|
30
30
|
// src/Archivist.ts
|
|
31
|
-
var import_assert = require("@xylabs/assert");
|
|
32
31
|
var import_exists = require("@xylabs/exists");
|
|
33
32
|
var import_archivist_abstract = require("@xyo-network/archivist-abstract");
|
|
34
33
|
var import_archivist_model = require("@xyo-network/archivist-model");
|
|
@@ -82,7 +81,6 @@ var IndexedDbArchivist = class _IndexedDbArchivist extends import_archivist_abst
|
|
|
82
81
|
static hashIndexName = (0, import_archivist_model.buildStandardIndexName)(_IndexedDbArchivist.hashIndex);
|
|
83
82
|
// eslint-disable-next-line @typescript-eslint/member-ordering
|
|
84
83
|
static schemaIndexName = (0, import_archivist_model.buildStandardIndexName)(_IndexedDbArchivist.schemaIndex);
|
|
85
|
-
_db;
|
|
86
84
|
/**
|
|
87
85
|
* The database name. If not supplied via config, it defaults
|
|
88
86
|
* to the module name (not guaranteed to be unique) and if module
|
|
@@ -115,75 +113,107 @@ var IndexedDbArchivist = class _IndexedDbArchivist extends import_archivist_abst
|
|
|
115
113
|
get storeName() {
|
|
116
114
|
return this.config?.storeName ?? _IndexedDbArchivist.defaultStoreName;
|
|
117
115
|
}
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
116
|
+
/**
|
|
117
|
+
* The indexes to create on the store
|
|
118
|
+
*/
|
|
121
119
|
get indexes() {
|
|
122
|
-
return
|
|
120
|
+
return [
|
|
121
|
+
_IndexedDbArchivist.hashIndex,
|
|
122
|
+
_IndexedDbArchivist.schemaIndex,
|
|
123
|
+
...this.config?.storage?.indexes ?? []
|
|
124
|
+
];
|
|
123
125
|
}
|
|
124
126
|
async allHandler() {
|
|
125
|
-
const payloads = await this.db.getAll(this.storeName);
|
|
127
|
+
const payloads = await this.useDb((db) => db.getAll(this.storeName));
|
|
126
128
|
return payloads.map((payload) => import_hash.PayloadHasher.jsonPayload(payload));
|
|
127
129
|
}
|
|
128
130
|
async clearHandler() {
|
|
129
|
-
await this.db.clear(this.storeName);
|
|
131
|
+
await this.useDb((db) => db.clear(this.storeName));
|
|
130
132
|
}
|
|
131
133
|
async deleteHandler(hashes) {
|
|
132
134
|
const distinctHashes = [
|
|
133
135
|
...new Set(hashes)
|
|
134
136
|
];
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
137
|
+
return await this.useDb(async (db) => {
|
|
138
|
+
const found = await Promise.all(distinctHashes.map(async (hash) => {
|
|
139
|
+
const existing = await db.getKeyFromIndex(this.storeName, _IndexedDbArchivist.hashIndexName, hash);
|
|
140
|
+
if (existing) {
|
|
141
|
+
await db.delete(this.storeName, existing);
|
|
142
|
+
return hash;
|
|
143
|
+
}
|
|
144
|
+
}));
|
|
145
|
+
return found.filter(import_exists.exists);
|
|
146
|
+
});
|
|
145
147
|
}
|
|
146
148
|
async getHandler(hashes) {
|
|
147
|
-
const payloads = await Promise.all(hashes.map((hash) =>
|
|
149
|
+
const payloads = await this.useDb((db) => Promise.all(hashes.map((hash) => db.getFromIndex(this.storeName, _IndexedDbArchivist.hashIndexName, hash))));
|
|
148
150
|
return payloads.filter(import_exists.exists);
|
|
149
151
|
}
|
|
150
152
|
async insertHandler(payloads) {
|
|
151
153
|
const pairs = await import_hash.PayloadHasher.hashPairs(payloads);
|
|
152
|
-
const
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
const
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
await store.
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
154
|
+
const db = await this.getInitializedDb();
|
|
155
|
+
try {
|
|
156
|
+
const inserted = await Promise.all(pairs.map(async ([payload, _hash]) => {
|
|
157
|
+
const tx = db.transaction(this.storeName, "readwrite");
|
|
158
|
+
try {
|
|
159
|
+
const store = tx.objectStore(this.storeName);
|
|
160
|
+
const existing = await store.index(_IndexedDbArchivist.hashIndexName).get(_hash);
|
|
161
|
+
if (!existing) {
|
|
162
|
+
await store.put({
|
|
163
|
+
...payload,
|
|
164
|
+
_hash
|
|
165
|
+
});
|
|
166
|
+
return payload;
|
|
167
|
+
}
|
|
168
|
+
} finally {
|
|
169
|
+
await tx.done;
|
|
163
170
|
}
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
171
|
+
}));
|
|
172
|
+
return inserted.filter(import_exists.exists);
|
|
173
|
+
} finally {
|
|
174
|
+
db.close();
|
|
175
|
+
}
|
|
169
176
|
}
|
|
170
177
|
async startHandler() {
|
|
171
178
|
await super.startHandler();
|
|
179
|
+
await this.useDb(() => {
|
|
180
|
+
});
|
|
181
|
+
return true;
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Returns that the desired DB/Store initialized to the correct version
|
|
185
|
+
* @returns The initialized DB
|
|
186
|
+
*/
|
|
187
|
+
async getInitializedDb() {
|
|
172
188
|
const { dbName, dbVersion, indexes, storeName } = this;
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
189
|
+
const db = await (0, import_idb.openDB)(dbName, dbVersion, {
|
|
190
|
+
blocked(currentVersion, blockedVersion, event) {
|
|
191
|
+
console.warn(`IndexedDbArchivist: Blocked from upgrading from ${currentVersion} to ${blockedVersion}`, event);
|
|
192
|
+
},
|
|
193
|
+
blocking(currentVersion, blockedVersion, event) {
|
|
194
|
+
console.warn(`IndexedDbArchivist: Blocking upgrade from ${currentVersion} to ${blockedVersion}`, event);
|
|
195
|
+
},
|
|
196
|
+
terminated() {
|
|
197
|
+
console.log("IndexedDbArchivist: Terminated");
|
|
198
|
+
},
|
|
199
|
+
upgrade(database, oldVersion, newVersion, transaction) {
|
|
200
|
+
if (oldVersion !== newVersion) {
|
|
201
|
+
console.log(`IndexedDbArchivist: Upgrading from ${oldVersion} to ${newVersion}`);
|
|
202
|
+
const objectStores = transaction.objectStoreNames;
|
|
203
|
+
for (const name of objectStores) {
|
|
204
|
+
try {
|
|
205
|
+
database.deleteObjectStore(name);
|
|
206
|
+
} catch {
|
|
207
|
+
console.log(`IndexedDbArchivist: Failed to delete existing object store ${name}`);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}
|
|
176
211
|
const store = database.createObjectStore(storeName, {
|
|
177
212
|
// If it isn't explicitly set, create a value by auto incrementing.
|
|
178
213
|
autoIncrement: true
|
|
179
214
|
});
|
|
180
215
|
store.name = storeName;
|
|
181
|
-
const
|
|
182
|
-
_IndexedDbArchivist.hashIndex,
|
|
183
|
-
_IndexedDbArchivist.schemaIndex,
|
|
184
|
-
...indexes
|
|
185
|
-
];
|
|
186
|
-
for (const { key, multiEntry, unique } of indexesToCreate) {
|
|
216
|
+
for (const { key, multiEntry, unique } of indexes) {
|
|
187
217
|
const indexKeys = Object.keys(key);
|
|
188
218
|
const keys = indexKeys.length === 1 ? indexKeys[0] : indexKeys;
|
|
189
219
|
const indexName = (0, import_archivist_model.buildStandardIndexName)({
|
|
@@ -197,7 +227,20 @@ var IndexedDbArchivist = class _IndexedDbArchivist extends import_archivist_abst
|
|
|
197
227
|
}
|
|
198
228
|
}
|
|
199
229
|
});
|
|
200
|
-
return
|
|
230
|
+
return db;
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* Executes a callback with the initialized DB and then closes the db
|
|
234
|
+
* @param callback The method to execute with the initialized DB
|
|
235
|
+
* @returns
|
|
236
|
+
*/
|
|
237
|
+
async useDb(callback) {
|
|
238
|
+
const db = await this.getInitializedDb();
|
|
239
|
+
try {
|
|
240
|
+
return await callback(db);
|
|
241
|
+
} finally {
|
|
242
|
+
db.close();
|
|
243
|
+
}
|
|
201
244
|
}
|
|
202
245
|
};
|
|
203
246
|
IndexedDbArchivist = _ts_decorate([
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/index.ts","../../src/Archivist.ts","../../src/Schema.ts","../../src/Config.ts"],"sourcesContent":["export * from './Archivist'\nexport * from './Config'\nexport * from './Params'\nexport * from './Schema'\n","import { assertEx } from '@xylabs/assert'\nimport { exists } from '@xylabs/exists'\nimport { AbstractArchivist } from '@xyo-network/archivist-abstract'\nimport {\n ArchivistAllQuerySchema,\n ArchivistClearQuerySchema,\n ArchivistDeleteQuerySchema,\n ArchivistInsertQuerySchema,\n ArchivistModuleEventData,\n buildStandardIndexName,\n IndexDescription,\n} from '@xyo-network/archivist-model'\nimport { PayloadHasher } from '@xyo-network/hash'\nimport { creatableModule } from '@xyo-network/module-model'\nimport { Payload } from '@xyo-network/payload-model'\nimport { IDBPDatabase, openDB } from 'idb'\n\nimport { IndexedDbArchivistConfigSchema } from './Config'\nimport { IndexedDbArchivistParams } from './Params'\n\nexport interface PayloadStore {\n [s: string]: Payload\n}\n\n@creatableModule()\nexport class IndexedDbArchivist<\n TParams extends IndexedDbArchivistParams = IndexedDbArchivistParams,\n TEventData extends ArchivistModuleEventData = ArchivistModuleEventData,\n> extends AbstractArchivist<TParams, TEventData> {\n static override configSchemas = [IndexedDbArchivistConfigSchema]\n static readonly defaultDbName = 'archivist'\n static readonly defaultDbVersion = 1\n static readonly defaultStoreName = 'payloads'\n private static readonly hashIndex: IndexDescription = { key: { _hash: 1 }, multiEntry: false, unique: true }\n private static readonly schemaIndex: IndexDescription = { key: { schema: 1 }, multiEntry: false, unique: false }\n // eslint-disable-next-line @typescript-eslint/member-ordering\n static readonly hashIndexName = buildStandardIndexName(IndexedDbArchivist.hashIndex)\n // eslint-disable-next-line @typescript-eslint/member-ordering\n static readonly schemaIndexName = buildStandardIndexName(IndexedDbArchivist.schemaIndex)\n\n private _db: IDBPDatabase<PayloadStore> | undefined\n\n /**\n * The database name. If not supplied via config, it defaults\n * to the module name (not guaranteed to be unique) and if module\n * name is not supplied, it defaults to `archivist`. This behavior\n * biases towards a single, isolated DB per archivist which seems to\n * make the most sense for 99% of use cases.\n */\n get dbName() {\n return this.config?.dbName ?? this.config?.name ?? IndexedDbArchivist.defaultDbName\n }\n\n /**\n * The database version. If not supplied via config, it defaults to 1.\n */\n get dbVersion() {\n return this.config?.dbVersion ?? IndexedDbArchivist.defaultDbVersion\n }\n\n override get queries() {\n return [ArchivistAllQuerySchema, ArchivistClearQuerySchema, ArchivistDeleteQuerySchema, ArchivistInsertQuerySchema, ...super.queries]\n }\n\n /**\n * The name of the object store. If not supplied via config, it defaults\n * to `payloads`.\n */\n get storeName() {\n return this.config?.storeName ?? IndexedDbArchivist.defaultStoreName\n }\n\n private get db(): IDBPDatabase<PayloadStore> {\n return assertEx(this._db, 'DB not initialized')\n }\n\n private get indexes() {\n return this.config?.storage?.indexes ?? []\n }\n\n protected override async allHandler(): Promise<Payload[]> {\n // Get all payloads from the store\n const payloads = await this.db.getAll(this.storeName)\n // Remove any metadata before returning to the client\n return payloads.map((payload) => PayloadHasher.jsonPayload(payload))\n }\n\n protected override async clearHandler(): Promise<void> {\n await this.db.clear(this.storeName)\n }\n\n protected override async deleteHandler(hashes: string[]): Promise<string[]> {\n const distinctHashes = [...new Set(hashes)]\n const found = await Promise.all(\n distinctHashes.map(async (hash) => {\n let existing: IDBValidKey | undefined\n do {\n existing = await this.db.getKeyFromIndex(this.storeName, IndexedDbArchivist.hashIndexName, hash)\n if (existing) await this.db.delete(this.storeName, existing)\n } while (!existing)\n return hash\n }),\n )\n // Return hashes removed\n return found.filter(exists)\n }\n\n protected override async getHandler(hashes: string[]): Promise<Payload[]> {\n const payloads = await Promise.all(hashes.map((hash) => this.db.getFromIndex(this.storeName, IndexedDbArchivist.hashIndexName, hash)))\n return payloads.filter(exists)\n }\n\n protected override async insertHandler(payloads: Payload[]): Promise<Payload[]> {\n const pairs = await PayloadHasher.hashPairs(payloads)\n // Only return the payloads that were successfully inserted\n const inserted = await Promise.all(\n pairs.map(async ([payload, _hash]) => {\n const tx = this.db.transaction(this.storeName, 'readwrite')\n try {\n const store = tx.objectStore(this.storeName)\n const existing = await store.index(IndexedDbArchivist.hashIndexName).get(_hash)\n if (!existing) {\n await store.put({ ...payload, _hash })\n return payload\n }\n } finally {\n await tx.done\n }\n }),\n )\n return inserted.filter(exists)\n }\n\n protected override async startHandler() {\n await super.startHandler()\n // NOTE: We could defer this creation to first access but we\n // want to fail fast here in case something is wrong\n const { dbName, dbVersion, indexes, storeName } = this\n this._db = await openDB<PayloadStore>(dbName, dbVersion, {\n async upgrade(database) {\n await Promise.resolve() // Async to match spec\n // Create the store\n const store = database.createObjectStore(storeName, {\n // If it isn't explicitly set, create a value by auto incrementing.\n autoIncrement: true,\n })\n // Name the store\n store.name = storeName\n // Create an index on the hash\n const indexesToCreate = [IndexedDbArchivist.hashIndex, IndexedDbArchivist.schemaIndex, ...indexes]\n for (const { key, multiEntry, unique } of indexesToCreate) {\n const indexKeys = Object.keys(key)\n const keys = indexKeys.length === 1 ? indexKeys[0] : indexKeys\n const indexName = buildStandardIndexName({ key, unique })\n store.createIndex(indexName, keys, { multiEntry, unique })\n }\n },\n })\n return true\n }\n}\n","export type IndexedDbArchivistSchema = 'network.xyo.archivist.indexeddb'\nexport const IndexedDbArchivistSchema: IndexedDbArchivistSchema = 'network.xyo.archivist.indexeddb'\n","import { ArchivistConfig, IndexDescription } from '@xyo-network/archivist-model'\n\nimport { IndexedDbArchivistSchema } from './Schema'\n\nexport type IndexedDbArchivistConfigSchema = `${IndexedDbArchivistSchema}.config`\nexport const IndexedDbArchivistConfigSchema: IndexedDbArchivistConfigSchema = `${IndexedDbArchivistSchema}.config`\n\nexport type IndexedDbArchivistConfig = ArchivistConfig<{\n /**\n * The database name\n */\n dbName?: string\n /**\n * The version of the DB, defaults to 1\n */\n dbVersion?: number\n schema: IndexedDbArchivistConfigSchema\n /**\n * The storage configuration\n * // TODO: Hoist to main archivist config\n */\n storage?: {\n /**\n * The indexes to create on the object store\n */\n indexes?: IndexDescription[]\n }\n /**\n * The name of the object store\n */\n storeName?: string\n}>\n"],"mappings":";;;;;;;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;YAAS3B;UAAM,CAAA;AACpC,iBAAO2B;QACT;MACF,UAAA;AACE,cAAMwB,GAAGO;MACX;IACF,CAAA,CAAA;AAEF,WAAOR,SAASP,OAAOC,oBAAAA;EACzB;EAEA,MAAyBe,eAAe;AACtC,UAAM,MAAMA,aAAAA;AAGZ,UAAM,EAAElD,QAAQG,WAAWS,SAASH,UAAS,IAAK;AAClD,SAAKV,MAAM,UAAMoD,mBAAqBnD,QAAQG,WAAW;MACvD,MAAMiD,QAAQC,UAAQ;AACpB,cAAMzB,QAAQ0B,QAAO;AAErB,cAAMV,QAAQS,SAASE,kBAAkB9C,WAAW;;UAElD+C,eAAe;QACjB,CAAA;AAEAZ,cAAM1C,OAAOO;AAEb,cAAMgD,kBAAkB;UAAC3E,oBAAmBO;UAAWP,oBAAmBY;aAAgBkB;;AAC1F,mBAAW,EAAEtB,KAAKE,YAAYC,OAAM,KAAMgE,iBAAiB;AACzD,gBAAMC,YAAYC,OAAOC,KAAKtE,GAAAA;AAC9B,gBAAMsE,OAAOF,UAAUG,WAAW,IAAIH,UAAU,CAAA,IAAKA;AACrD,gBAAMI,gBAAYjE,+CAAuB;YAAEP;YAAKG;UAAO,CAAA;AACvDmD,gBAAMmB,YAAYD,WAAWF,MAAM;YAAEpE;YAAYC;UAAO,CAAA;QAC1D;MACF;IACF,CAAA;AACA,WAAO;EACT;AACF;AAvIaX,qBAAAA,aAAAA;MADZkF,qCAAAA;GACYlF,kBAAAA;","names":["IndexedDbArchivistSchema","IndexedDbArchivistConfigSchema","IndexedDbArchivistSchema","IndexedDbArchivist","AbstractArchivist","configSchemas","IndexedDbArchivistConfigSchema","defaultDbName","defaultDbVersion","defaultStoreName","hashIndex","key","_hash","multiEntry","unique","schemaIndex","schema","hashIndexName","buildStandardIndexName","schemaIndexName","_db","dbName","config","name","dbVersion","queries","ArchivistAllQuerySchema","ArchivistClearQuerySchema","ArchivistDeleteQuerySchema","ArchivistInsertQuerySchema","storeName","db","assertEx","indexes","storage","allHandler","payloads","getAll","map","payload","PayloadHasher","jsonPayload","clearHandler","clear","deleteHandler","hashes","distinctHashes","Set","found","Promise","all","hash","existing","getKeyFromIndex","delete","filter","exists","getHandler","getFromIndex","insertHandler","pairs","hashPairs","inserted","tx","transaction","store","objectStore","index","get","put","done","startHandler","openDB","upgrade","database","resolve","createObjectStore","autoIncrement","indexesToCreate","indexKeys","Object","keys","length","indexName","createIndex","creatableModule"]}
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts","../../src/Archivist.ts","../../src/Schema.ts","../../src/Config.ts"],"sourcesContent":["export * from './Archivist'\nexport * from './Config'\nexport * from './Params'\nexport * from './Schema'\n","import { exists } from '@xylabs/exists'\nimport { AbstractArchivist } from '@xyo-network/archivist-abstract'\nimport {\n ArchivistAllQuerySchema,\n ArchivistClearQuerySchema,\n ArchivistDeleteQuerySchema,\n ArchivistInsertQuerySchema,\n ArchivistModuleEventData,\n buildStandardIndexName,\n IndexDescription,\n} from '@xyo-network/archivist-model'\nimport { PayloadHasher } from '@xyo-network/hash'\nimport { creatableModule } from '@xyo-network/module-model'\nimport { Payload } from '@xyo-network/payload-model'\nimport { IDBPDatabase, openDB } from 'idb'\n\nimport { IndexedDbArchivistConfigSchema } from './Config'\nimport { IndexedDbArchivistParams } from './Params'\n\nexport interface PayloadStore {\n [s: string]: Payload\n}\n\n@creatableModule()\nexport class IndexedDbArchivist<\n TParams extends IndexedDbArchivistParams = IndexedDbArchivistParams,\n TEventData extends ArchivistModuleEventData = ArchivistModuleEventData,\n> extends AbstractArchivist<TParams, TEventData> {\n static override configSchemas = [IndexedDbArchivistConfigSchema]\n static readonly defaultDbName = 'archivist'\n static readonly defaultDbVersion = 1\n static readonly defaultStoreName = 'payloads'\n private static readonly hashIndex: IndexDescription = { key: { _hash: 1 }, multiEntry: false, unique: true }\n private static readonly schemaIndex: IndexDescription = { key: { schema: 1 }, multiEntry: false, unique: false }\n // eslint-disable-next-line @typescript-eslint/member-ordering\n static readonly hashIndexName = buildStandardIndexName(IndexedDbArchivist.hashIndex)\n // eslint-disable-next-line @typescript-eslint/member-ordering\n static readonly schemaIndexName = buildStandardIndexName(IndexedDbArchivist.schemaIndex)\n\n /**\n * The database name. If not supplied via config, it defaults\n * to the module name (not guaranteed to be unique) and if module\n * name is not supplied, it defaults to `archivist`. This behavior\n * biases towards a single, isolated DB per archivist which seems to\n * make the most sense for 99% of use cases.\n */\n get dbName() {\n return this.config?.dbName ?? this.config?.name ?? IndexedDbArchivist.defaultDbName\n }\n\n /**\n * The database version. If not supplied via config, it defaults to 1.\n */\n get dbVersion() {\n return this.config?.dbVersion ?? IndexedDbArchivist.defaultDbVersion\n }\n\n override get queries() {\n return [ArchivistAllQuerySchema, ArchivistClearQuerySchema, ArchivistDeleteQuerySchema, ArchivistInsertQuerySchema, ...super.queries]\n }\n\n /**\n * The name of the object store. If not supplied via config, it defaults\n * to `payloads`.\n */\n get storeName() {\n return this.config?.storeName ?? IndexedDbArchivist.defaultStoreName\n }\n\n /**\n * The indexes to create on the store\n */\n private get indexes() {\n return [IndexedDbArchivist.hashIndex, IndexedDbArchivist.schemaIndex, ...(this.config?.storage?.indexes ?? [])]\n }\n\n protected override async allHandler(): Promise<Payload[]> {\n // Get all payloads from the store\n const payloads = await this.useDb((db) => db.getAll(this.storeName))\n // Remove any metadata before returning to the client\n return payloads.map((payload) => PayloadHasher.jsonPayload(payload))\n }\n\n protected override async clearHandler(): Promise<void> {\n await this.useDb((db) => db.clear(this.storeName))\n }\n\n protected override async deleteHandler(hashes: string[]): Promise<string[]> {\n // Remove any duplicates\n const distinctHashes = [...new Set(hashes)]\n return await this.useDb(async (db) => {\n // Only return hashes that were successfully deleted\n const found = await Promise.all(\n distinctHashes.map(async (hash) => {\n // Check if the hash exists\n const existing = await db.getKeyFromIndex(this.storeName, IndexedDbArchivist.hashIndexName, hash)\n // If it does exist\n if (existing) {\n // Delete it\n await db.delete(this.storeName, existing)\n // Return the hash so it gets added to the list of deleted hashes\n return hash\n }\n }),\n )\n return found.filter(exists)\n })\n }\n\n protected override async getHandler(hashes: string[]): Promise<Payload[]> {\n const payloads = await this.useDb((db) =>\n Promise.all(hashes.map((hash) => db.getFromIndex(this.storeName, IndexedDbArchivist.hashIndexName, hash))),\n )\n return payloads.filter(exists)\n }\n\n protected override async insertHandler(payloads: Payload[]): Promise<Payload[]> {\n const pairs = await PayloadHasher.hashPairs(payloads)\n const db = await this.getInitializedDb()\n try {\n // Only return the payloads that were successfully inserted\n const inserted = await Promise.all(\n pairs.map(async ([payload, _hash]) => {\n // Perform each insert via a transaction to ensure it is atomic\n // with respect to checking for the pre-existence of the hash.\n // This is done to preserve iteration via insertion order.\n const tx = db.transaction(this.storeName, 'readwrite')\n try {\n // Get the object store\n const store = tx.objectStore(this.storeName)\n // Check if the hash already exists\n const existing = await store.index(IndexedDbArchivist.hashIndexName).get(_hash)\n // If it does not already exist\n if (!existing) {\n // Insert the payload\n await store.put({ ...payload, _hash })\n // Return it so it gets added to the list of inserted payloads\n return payload\n }\n } finally {\n // Close the transaction\n await tx.done\n }\n }),\n )\n return inserted.filter(exists)\n } finally {\n db.close()\n }\n }\n\n protected override async startHandler() {\n await super.startHandler()\n // NOTE: We could defer this creation to first access but we\n // want to fail fast here in case something is wrong\n await this.useDb(() => {})\n return true\n }\n\n /**\n * Returns that the desired DB/Store initialized to the correct version\n * @returns The initialized DB\n */\n private async getInitializedDb(): Promise<IDBPDatabase<PayloadStore>> {\n const { dbName, dbVersion, indexes, storeName } = this\n const db = await openDB<PayloadStore>(dbName, dbVersion, {\n blocked(currentVersion, blockedVersion, event) {\n console.warn(`IndexedDbArchivist: Blocked from upgrading from ${currentVersion} to ${blockedVersion}`, event)\n },\n blocking(currentVersion, blockedVersion, event) {\n console.warn(`IndexedDbArchivist: Blocking upgrade from ${currentVersion} to ${blockedVersion}`, event)\n },\n terminated() {\n console.log('IndexedDbArchivist: Terminated')\n },\n upgrade(database, oldVersion, newVersion, transaction) {\n // NOTE: This is called whenever the DB is created/updated. We could simply ensure the desired end\n // state but, out of an abundance of caution, we will just delete (so we know where we are starting\n // from a known good point) and recreate the desired state. This prioritizes resilience over data\n // retention but we can revisit that tradeoff when it becomes limiting. Because distributed browser\n // state is extremely hard to debug, this seems like fair tradeoff for now.\n if (oldVersion !== newVersion) {\n console.log(`IndexedDbArchivist: Upgrading from ${oldVersion} to ${newVersion}`)\n // Delete any existing databases that are not the current version\n const objectStores = transaction.objectStoreNames\n for (const name of objectStores) {\n try {\n database.deleteObjectStore(name)\n } catch {\n console.log(`IndexedDbArchivist: Failed to delete existing object store ${name}`)\n }\n }\n }\n // Create the store\n const store = database.createObjectStore(storeName, {\n // If it isn't explicitly set, create a value by auto incrementing.\n autoIncrement: true,\n })\n // Name the store\n store.name = storeName\n // Create an index on the hash\n for (const { key, multiEntry, unique } of indexes) {\n const indexKeys = Object.keys(key)\n const keys = indexKeys.length === 1 ? indexKeys[0] : indexKeys\n const indexName = buildStandardIndexName({ key, unique })\n store.createIndex(indexName, keys, { multiEntry, unique })\n }\n },\n })\n return db\n }\n\n /**\n * Executes a callback with the initialized DB and then closes the db\n * @param callback The method to execute with the initialized DB\n * @returns\n */\n private async useDb<T>(callback: (db: IDBPDatabase<PayloadStore>) => Promise<T> | T): Promise<T> {\n // Get the initialized DB\n const db = await this.getInitializedDb()\n try {\n // Perform the callback\n return await callback(db)\n } finally {\n // Close the DB\n db.close()\n }\n }\n}\n","export type IndexedDbArchivistSchema = 'network.xyo.archivist.indexeddb'\nexport const IndexedDbArchivistSchema: IndexedDbArchivistSchema = 'network.xyo.archivist.indexeddb'\n","import { ArchivistConfig, IndexDescription } from '@xyo-network/archivist-model'\n\nimport { IndexedDbArchivistSchema } from './Schema'\n\nexport type IndexedDbArchivistConfigSchema = `${IndexedDbArchivistSchema}.config`\nexport const IndexedDbArchivistConfigSchema: IndexedDbArchivistConfigSchema = `${IndexedDbArchivistSchema}.config`\n\nexport type IndexedDbArchivistConfig = ArchivistConfig<{\n /**\n * The database name\n */\n dbName?: string\n /**\n * The version of the DB, defaults to 1\n */\n dbVersion?: number\n schema: IndexedDbArchivistConfigSchema\n /**\n * The storage configuration\n * // TODO: Hoist to main archivist config\n */\n storage?: {\n /**\n * The indexes to create on the object store\n */\n indexes?: IndexDescription[]\n }\n /**\n * The name of the object store\n */\n storeName?: string\n}>\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;ACAA,oBAAuB;AACvB,gCAAkC;AAClC,6BAQO;AACP,kBAA8B;AAC9B,0BAAgC;AAEhC,iBAAqC;;;ACb9B,IAAMA,2BAAqD;;;ACI3D,IAAMC,iCAAiE,GAAGC,wBAAAA;;;;;;;;;;;;;;AFmB1E,IAAMC,qBAAN,MAAMA,4BAGHC,4CAAAA;SAAAA;;;EACR,OAAgBC,gBAAgB;IAACC;;EACjC,OAAgBC,gBAAgB;EAChC,OAAgBC,mBAAmB;EACnC,OAAgBC,mBAAmB;EACnC,OAAwBC,YAA8B;IAAEC,KAAK;MAAEC,OAAO;IAAE;IAAGC,YAAY;IAAOC,QAAQ;EAAK;EAC3G,OAAwBC,cAAgC;IAAEJ,KAAK;MAAEK,QAAQ;IAAE;IAAGH,YAAY;IAAOC,QAAQ;EAAM;;EAE/G,OAAgBG,oBAAgBC,+CAAuBf,oBAAmBO,SAAS;;EAEnF,OAAgBS,sBAAkBD,+CAAuBf,oBAAmBY,WAAW;;;;;;;;EASvF,IAAIK,SAAS;AACX,WAAO,KAAKC,QAAQD,UAAU,KAAKC,QAAQC,QAAQnB,oBAAmBI;EACxE;;;;EAKA,IAAIgB,YAAY;AACd,WAAO,KAAKF,QAAQE,aAAapB,oBAAmBK;EACtD;EAEA,IAAagB,UAAU;AACrB,WAAO;MAACC;MAAyBC;MAA2BC;MAA4BC;SAA+B,MAAMJ;;EAC/H;;;;;EAMA,IAAIK,YAAY;AACd,WAAO,KAAKR,QAAQQ,aAAa1B,oBAAmBM;EACtD;;;;EAKA,IAAYqB,UAAU;AACpB,WAAO;MAAC3B,oBAAmBO;M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kBAAkBnE,IAAAA;YAC7B,QAAQ;AACNwD,sBAAQI,IAAI,8DAA8D5D,IAAAA,EAAM;YAClF;UACF;QACF;AAEA,cAAM2C,QAAQmB,SAASM,kBAAkB7D,WAAW;;UAElD8D,eAAe;QACjB,CAAA;AAEA1B,cAAM3C,OAAOO;AAEb,mBAAW,EAAElB,KAAKE,YAAYC,OAAM,KAAMgB,SAAS;AACjD,gBAAM8D,YAAYC,OAAOC,KAAKnF,GAAAA;AAC9B,gBAAMmF,OAAOF,UAAUG,WAAW,IAAIH,UAAU,CAAA,IAAKA;AACrD,gBAAMI,gBAAY9E,+CAAuB;YAAEP;YAAKG;UAAO,CAAA;AACvDmD,gBAAMgC,YAAYD,WAAWF,MAAM;YAAEjF;YAAYC;UAAO,CAAA;QAC1D;MACF;IACF,CAAA;AACA,WAAOqB;EACT;;;;;;EAOA,MAAcD,MAASgE,UAA0E;AAE/F,UAAM/D,KAAK,MAAM,KAAK0B,iBAAgB;AACtC,QAAI;AAEF,aAAO,MAAMqC,SAAS/D,EAAAA;IACxB,UAAA;AAEEA,SAAGoC,MAAK;IACV;EACF;AACF;AA5MapE,qBAAAA,aAAAA;MADZgG,qCAAAA;GACYhG,kBAAAA;","names":["IndexedDbArchivistSchema","IndexedDbArchivistConfigSchema","IndexedDbArchivistSchema","IndexedDbArchivist","AbstractArchivist","configSchemas","IndexedDbArchivistConfigSchema","defaultDbName","defaultDbVersion","defaultStoreName","hashIndex","key","_hash","multiEntry","unique","schemaIndex","schema","hashIndexName","buildStandardIndexName","schemaIndexName","dbName","config","name","dbVersion","queries","ArchivistAllQuerySchema","ArchivistClearQuerySchema","ArchivistDeleteQuerySchema","ArchivistInsertQuerySchema","storeName","indexes","storage","allHandler","payloads","useDb","db","getAll","map","payload","PayloadHasher","jsonPayload","clearHandler","clear","deleteHandler","hashes","distinctHashes","Set","found","Promise","all","hash","existing","getKeyFromIndex","delete","filter","exists","getHandler","getFromIndex","insertHandler","pairs","hashPairs","getInitializedDb","inserted","tx","transaction","store","objectStore","index","get","put","done","close","startHandler","openDB","blocked","currentVersion","blockedVersion","event","console","warn","blocking","terminated","log","upgrade","database","oldVersion","newVersion","objectStores","objectStoreNames","deleteObjectStore","createObjectStore","autoIncrement","indexKeys","Object","keys","length","indexName","createIndex","callback","creatableModule"]}
|
package/dist/browser/index.js
CHANGED
|
@@ -2,7 +2,6 @@ var __defProp = Object.defineProperty;
|
|
|
2
2
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
3
|
|
|
4
4
|
// src/Archivist.ts
|
|
5
|
-
import { assertEx } from "@xylabs/assert";
|
|
6
5
|
import { exists } from "@xylabs/exists";
|
|
7
6
|
import { AbstractArchivist } from "@xyo-network/archivist-abstract";
|
|
8
7
|
import { ArchivistAllQuerySchema, ArchivistClearQuerySchema, ArchivistDeleteQuerySchema, ArchivistInsertQuerySchema, buildStandardIndexName } from "@xyo-network/archivist-model";
|
|
@@ -56,7 +55,6 @@ var IndexedDbArchivist = class _IndexedDbArchivist extends AbstractArchivist {
|
|
|
56
55
|
static hashIndexName = buildStandardIndexName(_IndexedDbArchivist.hashIndex);
|
|
57
56
|
// eslint-disable-next-line @typescript-eslint/member-ordering
|
|
58
57
|
static schemaIndexName = buildStandardIndexName(_IndexedDbArchivist.schemaIndex);
|
|
59
|
-
_db;
|
|
60
58
|
/**
|
|
61
59
|
* The database name. If not supplied via config, it defaults
|
|
62
60
|
* to the module name (not guaranteed to be unique) and if module
|
|
@@ -89,75 +87,107 @@ var IndexedDbArchivist = class _IndexedDbArchivist extends AbstractArchivist {
|
|
|
89
87
|
get storeName() {
|
|
90
88
|
return this.config?.storeName ?? _IndexedDbArchivist.defaultStoreName;
|
|
91
89
|
}
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
90
|
+
/**
|
|
91
|
+
* The indexes to create on the store
|
|
92
|
+
*/
|
|
95
93
|
get indexes() {
|
|
96
|
-
return
|
|
94
|
+
return [
|
|
95
|
+
_IndexedDbArchivist.hashIndex,
|
|
96
|
+
_IndexedDbArchivist.schemaIndex,
|
|
97
|
+
...this.config?.storage?.indexes ?? []
|
|
98
|
+
];
|
|
97
99
|
}
|
|
98
100
|
async allHandler() {
|
|
99
|
-
const payloads = await this.db.getAll(this.storeName);
|
|
101
|
+
const payloads = await this.useDb((db) => db.getAll(this.storeName));
|
|
100
102
|
return payloads.map((payload) => PayloadHasher.jsonPayload(payload));
|
|
101
103
|
}
|
|
102
104
|
async clearHandler() {
|
|
103
|
-
await this.db.clear(this.storeName);
|
|
105
|
+
await this.useDb((db) => db.clear(this.storeName));
|
|
104
106
|
}
|
|
105
107
|
async deleteHandler(hashes) {
|
|
106
108
|
const distinctHashes = [
|
|
107
109
|
...new Set(hashes)
|
|
108
110
|
];
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
111
|
+
return await this.useDb(async (db) => {
|
|
112
|
+
const found = await Promise.all(distinctHashes.map(async (hash) => {
|
|
113
|
+
const existing = await db.getKeyFromIndex(this.storeName, _IndexedDbArchivist.hashIndexName, hash);
|
|
114
|
+
if (existing) {
|
|
115
|
+
await db.delete(this.storeName, existing);
|
|
116
|
+
return hash;
|
|
117
|
+
}
|
|
118
|
+
}));
|
|
119
|
+
return found.filter(exists);
|
|
120
|
+
});
|
|
119
121
|
}
|
|
120
122
|
async getHandler(hashes) {
|
|
121
|
-
const payloads = await Promise.all(hashes.map((hash) =>
|
|
123
|
+
const payloads = await this.useDb((db) => Promise.all(hashes.map((hash) => db.getFromIndex(this.storeName, _IndexedDbArchivist.hashIndexName, hash))));
|
|
122
124
|
return payloads.filter(exists);
|
|
123
125
|
}
|
|
124
126
|
async insertHandler(payloads) {
|
|
125
127
|
const pairs = await PayloadHasher.hashPairs(payloads);
|
|
126
|
-
const
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
const
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
await store.
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
128
|
+
const db = await this.getInitializedDb();
|
|
129
|
+
try {
|
|
130
|
+
const inserted = await Promise.all(pairs.map(async ([payload, _hash]) => {
|
|
131
|
+
const tx = db.transaction(this.storeName, "readwrite");
|
|
132
|
+
try {
|
|
133
|
+
const store = tx.objectStore(this.storeName);
|
|
134
|
+
const existing = await store.index(_IndexedDbArchivist.hashIndexName).get(_hash);
|
|
135
|
+
if (!existing) {
|
|
136
|
+
await store.put({
|
|
137
|
+
...payload,
|
|
138
|
+
_hash
|
|
139
|
+
});
|
|
140
|
+
return payload;
|
|
141
|
+
}
|
|
142
|
+
} finally {
|
|
143
|
+
await tx.done;
|
|
137
144
|
}
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
145
|
+
}));
|
|
146
|
+
return inserted.filter(exists);
|
|
147
|
+
} finally {
|
|
148
|
+
db.close();
|
|
149
|
+
}
|
|
143
150
|
}
|
|
144
151
|
async startHandler() {
|
|
145
152
|
await super.startHandler();
|
|
153
|
+
await this.useDb(() => {
|
|
154
|
+
});
|
|
155
|
+
return true;
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Returns that the desired DB/Store initialized to the correct version
|
|
159
|
+
* @returns The initialized DB
|
|
160
|
+
*/
|
|
161
|
+
async getInitializedDb() {
|
|
146
162
|
const { dbName, dbVersion, indexes, storeName } = this;
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
163
|
+
const db = await openDB(dbName, dbVersion, {
|
|
164
|
+
blocked(currentVersion, blockedVersion, event) {
|
|
165
|
+
console.warn(`IndexedDbArchivist: Blocked from upgrading from ${currentVersion} to ${blockedVersion}`, event);
|
|
166
|
+
},
|
|
167
|
+
blocking(currentVersion, blockedVersion, event) {
|
|
168
|
+
console.warn(`IndexedDbArchivist: Blocking upgrade from ${currentVersion} to ${blockedVersion}`, event);
|
|
169
|
+
},
|
|
170
|
+
terminated() {
|
|
171
|
+
console.log("IndexedDbArchivist: Terminated");
|
|
172
|
+
},
|
|
173
|
+
upgrade(database, oldVersion, newVersion, transaction) {
|
|
174
|
+
if (oldVersion !== newVersion) {
|
|
175
|
+
console.log(`IndexedDbArchivist: Upgrading from ${oldVersion} to ${newVersion}`);
|
|
176
|
+
const objectStores = transaction.objectStoreNames;
|
|
177
|
+
for (const name of objectStores) {
|
|
178
|
+
try {
|
|
179
|
+
database.deleteObjectStore(name);
|
|
180
|
+
} catch {
|
|
181
|
+
console.log(`IndexedDbArchivist: Failed to delete existing object store ${name}`);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
150
185
|
const store = database.createObjectStore(storeName, {
|
|
151
186
|
// If it isn't explicitly set, create a value by auto incrementing.
|
|
152
187
|
autoIncrement: true
|
|
153
188
|
});
|
|
154
189
|
store.name = storeName;
|
|
155
|
-
const
|
|
156
|
-
_IndexedDbArchivist.hashIndex,
|
|
157
|
-
_IndexedDbArchivist.schemaIndex,
|
|
158
|
-
...indexes
|
|
159
|
-
];
|
|
160
|
-
for (const { key, multiEntry, unique } of indexesToCreate) {
|
|
190
|
+
for (const { key, multiEntry, unique } of indexes) {
|
|
161
191
|
const indexKeys = Object.keys(key);
|
|
162
192
|
const keys = indexKeys.length === 1 ? indexKeys[0] : indexKeys;
|
|
163
193
|
const indexName = buildStandardIndexName({
|
|
@@ -171,7 +201,20 @@ var IndexedDbArchivist = class _IndexedDbArchivist extends AbstractArchivist {
|
|
|
171
201
|
}
|
|
172
202
|
}
|
|
173
203
|
});
|
|
174
|
-
return
|
|
204
|
+
return db;
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Executes a callback with the initialized DB and then closes the db
|
|
208
|
+
* @param callback The method to execute with the initialized DB
|
|
209
|
+
* @returns
|
|
210
|
+
*/
|
|
211
|
+
async useDb(callback) {
|
|
212
|
+
const db = await this.getInitializedDb();
|
|
213
|
+
try {
|
|
214
|
+
return await callback(db);
|
|
215
|
+
} finally {
|
|
216
|
+
db.close();
|
|
217
|
+
}
|
|
175
218
|
}
|
|
176
219
|
};
|
|
177
220
|
IndexedDbArchivist = _ts_decorate([
|