@xyo-network/archivist-indexeddb 3.6.9 → 3.6.11
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/Schema.d.ts +2 -2
- package/dist/browser/Schema.d.ts.map +1 -1
- package/dist/browser/index.mjs +129 -127
- package/dist/browser/index.mjs.map +1 -1
- package/package.json +18 -18
- package/src/Archivist.ts +1 -1
- package/src/Schema.ts +2 -2
package/dist/browser/Schema.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export
|
|
2
|
-
export
|
|
1
|
+
export declare const IndexedDbArchivistSchema: "network.xyo.archivist.indexeddb";
|
|
2
|
+
export type IndexedDbArchivistSchema = typeof IndexedDbArchivistSchema;
|
|
3
3
|
//# sourceMappingURL=Schema.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Schema.d.ts","sourceRoot":"","sources":["../../src/Schema.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"Schema.d.ts","sourceRoot":"","sources":["../../src/Schema.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,wBAAwB,EAAG,iCAA0C,CAAA;AAClF,MAAM,MAAM,wBAAwB,GAAG,OAAO,wBAAwB,CAAA"}
|
package/dist/browser/index.mjs
CHANGED
|
@@ -1,13 +1,37 @@
|
|
|
1
1
|
var __defProp = Object.defineProperty;
|
|
2
|
-
var
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
4
|
+
var __reflectGet = Reflect.get;
|
|
5
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
6
|
+
var __decorateClass = (decorators, target, key, kind) => {
|
|
7
|
+
var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
|
|
8
|
+
for (var i = decorators.length - 1, decorator; i >= 0; i--)
|
|
9
|
+
if (decorator = decorators[i])
|
|
10
|
+
result = (kind ? decorator(target, key, result) : decorator(result)) || result;
|
|
11
|
+
if (kind && result) __defProp(target, key, result);
|
|
12
|
+
return result;
|
|
13
|
+
};
|
|
14
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
15
|
+
var __superGet = (cls, obj, key) => __reflectGet(__getProtoOf(cls), key, obj);
|
|
3
16
|
|
|
4
17
|
// src/Archivist.ts
|
|
5
18
|
import { uniq } from "@xylabs/array";
|
|
6
19
|
import { assertEx } from "@xylabs/assert";
|
|
7
20
|
import { exists } from "@xylabs/exists";
|
|
8
|
-
import {
|
|
21
|
+
import {
|
|
22
|
+
withDb,
|
|
23
|
+
withReadOnlyStore,
|
|
24
|
+
withReadWriteStore
|
|
25
|
+
} from "@xylabs/indexed-db";
|
|
9
26
|
import { AbstractArchivist } from "@xyo-network/archivist-abstract";
|
|
10
|
-
import {
|
|
27
|
+
import {
|
|
28
|
+
ArchivistAllQuerySchema,
|
|
29
|
+
ArchivistClearQuerySchema,
|
|
30
|
+
ArchivistDeleteQuerySchema,
|
|
31
|
+
ArchivistInsertQuerySchema,
|
|
32
|
+
ArchivistNextQuerySchema,
|
|
33
|
+
buildStandardIndexName
|
|
34
|
+
} from "@xyo-network/archivist-model";
|
|
11
35
|
import { creatableModule } from "@xyo-network/module-model";
|
|
12
36
|
import { PayloadBuilder } from "@xyo-network/payload-builder";
|
|
13
37
|
|
|
@@ -18,71 +42,17 @@ var IndexedDbArchivistSchema = "network.xyo.archivist.indexeddb";
|
|
|
18
42
|
var IndexedDbArchivistConfigSchema = `${IndexedDbArchivistSchema}.config`;
|
|
19
43
|
|
|
20
44
|
// src/Archivist.ts
|
|
21
|
-
|
|
22
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
23
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
24
|
-
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
25
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
26
|
-
}
|
|
27
|
-
__name(_ts_decorate, "_ts_decorate");
|
|
28
|
-
var IndexedDbArchivist = class _IndexedDbArchivist extends AbstractArchivist {
|
|
29
|
-
static {
|
|
30
|
-
__name(this, "IndexedDbArchivist");
|
|
31
|
-
}
|
|
32
|
-
static configSchemas = [
|
|
33
|
-
...super.configSchemas,
|
|
34
|
-
IndexedDbArchivistConfigSchema
|
|
35
|
-
];
|
|
36
|
-
static defaultConfigSchema = IndexedDbArchivistConfigSchema;
|
|
37
|
-
static defaultDbName = "archivist";
|
|
38
|
-
static defaultDbVersion = 1;
|
|
39
|
-
static defaultStoreName = "payloads";
|
|
40
|
-
static dataHashIndex = {
|
|
41
|
-
key: {
|
|
42
|
-
_dataHash: 1
|
|
43
|
-
},
|
|
44
|
-
multiEntry: false,
|
|
45
|
-
unique: false
|
|
46
|
-
};
|
|
47
|
-
static hashIndex = {
|
|
48
|
-
key: {
|
|
49
|
-
_hash: 1
|
|
50
|
-
},
|
|
51
|
-
multiEntry: false,
|
|
52
|
-
unique: true
|
|
53
|
-
};
|
|
54
|
-
static schemaIndex = {
|
|
55
|
-
key: {
|
|
56
|
-
schema: 1
|
|
57
|
-
},
|
|
58
|
-
multiEntry: false,
|
|
59
|
-
unique: false
|
|
60
|
-
};
|
|
61
|
-
static sequenceIndex = {
|
|
62
|
-
key: {
|
|
63
|
-
_sequence: 1
|
|
64
|
-
},
|
|
65
|
-
multiEntry: false,
|
|
66
|
-
unique: true
|
|
67
|
-
};
|
|
68
|
-
// eslint-disable-next-line @typescript-eslint/member-ordering
|
|
69
|
-
static hashIndexName = buildStandardIndexName(_IndexedDbArchivist.hashIndex);
|
|
70
|
-
// eslint-disable-next-line @typescript-eslint/member-ordering
|
|
71
|
-
static dataHashIndexName = buildStandardIndexName(_IndexedDbArchivist.dataHashIndex);
|
|
72
|
-
// eslint-disable-next-line @typescript-eslint/member-ordering
|
|
73
|
-
static schemaIndexName = buildStandardIndexName(_IndexedDbArchivist.schemaIndex);
|
|
74
|
-
// eslint-disable-next-line @typescript-eslint/member-ordering
|
|
75
|
-
static sequenceIndexName = buildStandardIndexName(_IndexedDbArchivist.sequenceIndex);
|
|
45
|
+
var IndexedDbArchivist = class extends AbstractArchivist {
|
|
76
46
|
_dbName;
|
|
77
47
|
_dbVersion;
|
|
78
48
|
_storeName;
|
|
79
49
|
/**
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
50
|
+
* The database name. If not supplied via config, it defaults
|
|
51
|
+
* to the module name (not guaranteed to be unique) and if module
|
|
52
|
+
* name is not supplied, it defaults to `archivist`. This behavior
|
|
53
|
+
* biases towards a single, isolated DB per archivist which seems to
|
|
54
|
+
* make the most sense for 99% of use cases.
|
|
55
|
+
*/
|
|
86
56
|
get dbName() {
|
|
87
57
|
if (!this._dbName) {
|
|
88
58
|
if (this.config?.dbName) {
|
|
@@ -92,18 +62,18 @@ var IndexedDbArchivist = class _IndexedDbArchivist extends AbstractArchivist {
|
|
|
92
62
|
this.logger.warn("No dbName provided, using module name: ", this.config?.name);
|
|
93
63
|
this._dbName = this.config?.name;
|
|
94
64
|
} else {
|
|
95
|
-
this.logger.warn("No dbName provided, using default name: ",
|
|
96
|
-
this._dbName =
|
|
65
|
+
this.logger.warn("No dbName provided, using default name: ", IndexedDbArchivist.defaultDbName);
|
|
66
|
+
this._dbName = IndexedDbArchivist.defaultDbName;
|
|
97
67
|
}
|
|
98
68
|
}
|
|
99
69
|
}
|
|
100
70
|
return assertEx(this._dbName);
|
|
101
71
|
}
|
|
102
72
|
/**
|
|
103
|
-
|
|
104
|
-
|
|
73
|
+
* The database version. If not supplied via config, it defaults to 1.
|
|
74
|
+
*/
|
|
105
75
|
get dbVersion() {
|
|
106
|
-
this._dbVersion = this._dbVersion ?? this.config?.dbVersion ??
|
|
76
|
+
this._dbVersion = this._dbVersion ?? this.config?.dbVersion ?? IndexedDbArchivist.defaultDbVersion;
|
|
107
77
|
return this._dbVersion;
|
|
108
78
|
}
|
|
109
79
|
get queries() {
|
|
@@ -117,29 +87,29 @@ var IndexedDbArchivist = class _IndexedDbArchivist extends AbstractArchivist {
|
|
|
117
87
|
];
|
|
118
88
|
}
|
|
119
89
|
/**
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
90
|
+
* The name of the object store. If not supplied via config, it defaults
|
|
91
|
+
* to `payloads`.
|
|
92
|
+
*/
|
|
123
93
|
get storeName() {
|
|
124
94
|
if (!this._storeName) {
|
|
125
95
|
if (this.config?.storeName) {
|
|
126
96
|
this._storeName = this.config?.storeName;
|
|
127
97
|
} else {
|
|
128
|
-
this.logger.warn("No storeName provided, using default name: ",
|
|
129
|
-
this._storeName =
|
|
98
|
+
this.logger.warn("No storeName provided, using default name: ", IndexedDbArchivist.defaultStoreName);
|
|
99
|
+
this._storeName = IndexedDbArchivist.defaultStoreName;
|
|
130
100
|
}
|
|
131
101
|
}
|
|
132
102
|
return assertEx(this._storeName);
|
|
133
103
|
}
|
|
134
104
|
/**
|
|
135
|
-
|
|
136
|
-
|
|
105
|
+
* The indexes to create on the store
|
|
106
|
+
*/
|
|
137
107
|
get indexes() {
|
|
138
108
|
return [
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
109
|
+
IndexedDbArchivist.dataHashIndex,
|
|
110
|
+
IndexedDbArchivist.hashIndex,
|
|
111
|
+
IndexedDbArchivist.schemaIndex,
|
|
112
|
+
IndexedDbArchivist.sequenceIndex,
|
|
143
113
|
...this.config?.storage?.indexes ?? []
|
|
144
114
|
];
|
|
145
115
|
}
|
|
@@ -151,37 +121,35 @@ var IndexedDbArchivist = class _IndexedDbArchivist extends AbstractArchivist {
|
|
|
151
121
|
await this.useDb((db) => db.clear(this.storeName));
|
|
152
122
|
}
|
|
153
123
|
async deleteHandler(hashes) {
|
|
154
|
-
const uniqueHashes = [
|
|
155
|
-
...new Set(hashes)
|
|
156
|
-
];
|
|
124
|
+
const uniqueHashes = [...new Set(hashes)];
|
|
157
125
|
const pairs = await PayloadBuilder.hashPairs(await this.getHandler(uniqueHashes));
|
|
158
126
|
const hashesToDelete = (await Promise.all(pairs.map(async (pair) => {
|
|
159
127
|
const dataHash0 = await PayloadBuilder.dataHash(pair[0]);
|
|
160
|
-
return [
|
|
161
|
-
dataHash0,
|
|
162
|
-
pair[1]
|
|
163
|
-
];
|
|
128
|
+
return [dataHash0, pair[1]];
|
|
164
129
|
}))).flat();
|
|
165
|
-
const distinctHashes = [
|
|
166
|
-
...new Set(hashesToDelete)
|
|
167
|
-
];
|
|
130
|
+
const distinctHashes = [...new Set(hashesToDelete)];
|
|
168
131
|
return await this.useDb(async (db) => {
|
|
169
|
-
const found = await Promise.all(
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
132
|
+
const found = await Promise.all(
|
|
133
|
+
distinctHashes.map(async (hash) => {
|
|
134
|
+
const existing = await db.getKeyFromIndex(this.storeName, IndexedDbArchivist.hashIndexName, hash) ?? await db.getKeyFromIndex(this.storeName, IndexedDbArchivist.dataHashIndexName, hash);
|
|
135
|
+
if (existing) {
|
|
136
|
+
await db.delete(this.storeName, existing);
|
|
137
|
+
return hash;
|
|
138
|
+
}
|
|
139
|
+
})
|
|
140
|
+
);
|
|
176
141
|
return found.filter(exists).filter((hash) => uniqueHashes.includes(hash));
|
|
177
142
|
});
|
|
178
143
|
}
|
|
179
144
|
async getFromCursor(db, storeName, order = "asc", limit = 10, cursor) {
|
|
180
145
|
return await withReadOnlyStore(db, storeName, async (store) => {
|
|
181
|
-
const sequenceIndex = assertEx(store?.index(
|
|
146
|
+
const sequenceIndex = assertEx(store?.index(IndexedDbArchivist.sequenceIndexName), () => "Failed to get sequence index");
|
|
182
147
|
let sequenceCursor;
|
|
183
148
|
const parsedCursor = cursor ? order === "asc" ? IDBKeyRange.lowerBound(cursor, false) : IDBKeyRange.upperBound(cursor, false) : null;
|
|
184
|
-
sequenceCursor = await sequenceIndex.openCursor(
|
|
149
|
+
sequenceCursor = await sequenceIndex.openCursor(
|
|
150
|
+
parsedCursor,
|
|
151
|
+
order === "desc" ? "prev" : "next"
|
|
152
|
+
);
|
|
185
153
|
if (cursor) {
|
|
186
154
|
sequenceCursor = await sequenceCursor?.advance(1);
|
|
187
155
|
}
|
|
@@ -206,13 +174,13 @@ var IndexedDbArchivist = class _IndexedDbArchivist extends AbstractArchivist {
|
|
|
206
174
|
});
|
|
207
175
|
}
|
|
208
176
|
/**
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
177
|
+
* Uses an index to get a payload by the index value, but returns the value with the primary key (from the root store)
|
|
178
|
+
* @param db The db instance to use
|
|
179
|
+
* @param storeName The name of the store to use
|
|
180
|
+
* @param indexName The index to use
|
|
181
|
+
* @param key The key to get from the index
|
|
182
|
+
* @returns The primary key and the payload, or undefined if not found
|
|
183
|
+
*/
|
|
216
184
|
async getFromIndexWithPrimaryKey(db, storeName, indexName, key) {
|
|
217
185
|
return await withReadOnlyStore(db, storeName, async (store) => {
|
|
218
186
|
if (store) {
|
|
@@ -223,10 +191,7 @@ var IndexedDbArchivist = class _IndexedDbArchivist extends AbstractArchivist {
|
|
|
223
191
|
if (typeof cursor.primaryKey !== "number") {
|
|
224
192
|
throw new TypeError("primaryKey must be a number");
|
|
225
193
|
}
|
|
226
|
-
return [
|
|
227
|
-
cursor.primaryKey,
|
|
228
|
-
singleValue
|
|
229
|
-
];
|
|
194
|
+
return [cursor.primaryKey, singleValue];
|
|
230
195
|
}
|
|
231
196
|
}
|
|
232
197
|
});
|
|
@@ -235,9 +200,9 @@ var IndexedDbArchivist = class _IndexedDbArchivist extends AbstractArchivist {
|
|
|
235
200
|
const payloads = await this.useDb((db) => Promise.all(
|
|
236
201
|
// Filter duplicates to prevent unnecessary DB queries
|
|
237
202
|
uniq(hashes).map(async (hash) => {
|
|
238
|
-
const payload = await this.getFromIndexWithPrimaryKey(db, this.storeName,
|
|
203
|
+
const payload = await this.getFromIndexWithPrimaryKey(db, this.storeName, IndexedDbArchivist.hashIndexName, hash);
|
|
239
204
|
if (payload) return payload;
|
|
240
|
-
return this.getFromIndexWithPrimaryKey(db, this.storeName,
|
|
205
|
+
return this.getFromIndexWithPrimaryKey(db, this.storeName, IndexedDbArchivist.dataHashIndexName, hash);
|
|
241
206
|
})
|
|
242
207
|
));
|
|
243
208
|
const found = /* @__PURE__ */ new Set();
|
|
@@ -255,12 +220,14 @@ var IndexedDbArchivist = class _IndexedDbArchivist extends AbstractArchivist {
|
|
|
255
220
|
return await withReadWriteStore(db, this.storeName, async (store) => {
|
|
256
221
|
if (store) {
|
|
257
222
|
const inserted = [];
|
|
258
|
-
await Promise.all(
|
|
259
|
-
|
|
260
|
-
await store.
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
223
|
+
await Promise.all(
|
|
224
|
+
payloads.map(async (payload) => {
|
|
225
|
+
if (!await store.index(IndexedDbArchivist.hashIndexName).get(payload._hash)) {
|
|
226
|
+
await store.put(payload);
|
|
227
|
+
inserted.push(payload);
|
|
228
|
+
}
|
|
229
|
+
})
|
|
230
|
+
);
|
|
264
231
|
return inserted;
|
|
265
232
|
} else {
|
|
266
233
|
throw new Error("Failed to get store");
|
|
@@ -269,7 +236,11 @@ var IndexedDbArchivist = class _IndexedDbArchivist extends AbstractArchivist {
|
|
|
269
236
|
});
|
|
270
237
|
}
|
|
271
238
|
async nextHandler(options) {
|
|
272
|
-
const {
|
|
239
|
+
const {
|
|
240
|
+
limit,
|
|
241
|
+
cursor,
|
|
242
|
+
order
|
|
243
|
+
} = options ?? {};
|
|
273
244
|
return await this.useDb(async (db) => {
|
|
274
245
|
return await this.getFromCursor(db, this.storeName, order, limit ?? 10, cursor);
|
|
275
246
|
});
|
|
@@ -281,19 +252,50 @@ var IndexedDbArchivist = class _IndexedDbArchivist extends AbstractArchivist {
|
|
|
281
252
|
return true;
|
|
282
253
|
}
|
|
283
254
|
/**
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
255
|
+
* Executes a callback with the initialized DB and then closes the db
|
|
256
|
+
* @param callback The method to execute with the initialized DB
|
|
257
|
+
* @returns
|
|
258
|
+
*/
|
|
288
259
|
async useDb(callback) {
|
|
289
260
|
return await withDb(this.dbName, async (db) => {
|
|
290
261
|
return await callback(db);
|
|
291
|
-
}, {
|
|
292
|
-
[this.storeName]: this.indexes
|
|
293
|
-
}, this.logger);
|
|
262
|
+
}, { [this.storeName]: this.indexes }, this.logger);
|
|
294
263
|
}
|
|
295
264
|
};
|
|
296
|
-
IndexedDbArchivist
|
|
265
|
+
__publicField(IndexedDbArchivist, "configSchemas", [...__superGet(IndexedDbArchivist, IndexedDbArchivist, "configSchemas"), IndexedDbArchivistConfigSchema]);
|
|
266
|
+
__publicField(IndexedDbArchivist, "defaultConfigSchema", IndexedDbArchivistConfigSchema);
|
|
267
|
+
__publicField(IndexedDbArchivist, "defaultDbName", "archivist");
|
|
268
|
+
__publicField(IndexedDbArchivist, "defaultDbVersion", 1);
|
|
269
|
+
__publicField(IndexedDbArchivist, "defaultStoreName", "payloads");
|
|
270
|
+
__publicField(IndexedDbArchivist, "dataHashIndex", {
|
|
271
|
+
key: { _dataHash: 1 },
|
|
272
|
+
multiEntry: false,
|
|
273
|
+
unique: false
|
|
274
|
+
});
|
|
275
|
+
__publicField(IndexedDbArchivist, "hashIndex", {
|
|
276
|
+
key: { _hash: 1 },
|
|
277
|
+
multiEntry: false,
|
|
278
|
+
unique: true
|
|
279
|
+
});
|
|
280
|
+
__publicField(IndexedDbArchivist, "schemaIndex", {
|
|
281
|
+
key: { schema: 1 },
|
|
282
|
+
multiEntry: false,
|
|
283
|
+
unique: false
|
|
284
|
+
});
|
|
285
|
+
__publicField(IndexedDbArchivist, "sequenceIndex", {
|
|
286
|
+
key: { _sequence: 1 },
|
|
287
|
+
multiEntry: false,
|
|
288
|
+
unique: true
|
|
289
|
+
});
|
|
290
|
+
// eslint-disable-next-line @typescript-eslint/member-ordering
|
|
291
|
+
__publicField(IndexedDbArchivist, "hashIndexName", buildStandardIndexName(IndexedDbArchivist.hashIndex));
|
|
292
|
+
// eslint-disable-next-line @typescript-eslint/member-ordering
|
|
293
|
+
__publicField(IndexedDbArchivist, "dataHashIndexName", buildStandardIndexName(IndexedDbArchivist.dataHashIndex));
|
|
294
|
+
// eslint-disable-next-line @typescript-eslint/member-ordering
|
|
295
|
+
__publicField(IndexedDbArchivist, "schemaIndexName", buildStandardIndexName(IndexedDbArchivist.schemaIndex));
|
|
296
|
+
// eslint-disable-next-line @typescript-eslint/member-ordering
|
|
297
|
+
__publicField(IndexedDbArchivist, "sequenceIndexName", buildStandardIndexName(IndexedDbArchivist.sequenceIndex));
|
|
298
|
+
IndexedDbArchivist = __decorateClass([
|
|
297
299
|
creatableModule()
|
|
298
300
|
], IndexedDbArchivist);
|
|
299
301
|
export {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/Archivist.ts","../../src/Schema.ts","../../src/Config.ts"],"sourcesContent":["import { uniq } from '@xylabs/array'\nimport { assertEx } from '@xylabs/assert'\nimport { exists } from '@xylabs/exists'\nimport { Hash, Hex } from '@xylabs/hex'\nimport {\n ObjectStore,\n withDb,\n withReadOnlyStore, withReadWriteStore,\n} from '@xylabs/indexed-db'\nimport { AbstractArchivist } from '@xyo-network/archivist-abstract'\nimport {\n ArchivistAllQuerySchema,\n ArchivistClearQuerySchema,\n ArchivistDeleteQuerySchema,\n ArchivistInsertQuerySchema,\n ArchivistModuleEventData,\n ArchivistNextOptions,\n ArchivistNextQuerySchema,\n buildStandardIndexName,\n IndexDescription,\n} from '@xyo-network/archivist-model'\nimport { creatableModule } from '@xyo-network/module-model'\nimport { PayloadBuilder } from '@xyo-network/payload-builder'\nimport {\n Payload, Schema, WithStorageMeta,\n} from '@xyo-network/payload-model'\nimport { IDBPCursorWithValue, IDBPDatabase } from 'idb'\n\nimport { IndexedDbArchivistConfigSchema } from './Config.ts'\nimport { IndexedDbArchivistParams } from './Params.ts'\n\nexport interface PayloadStore {\n [s: string]: WithStorageMeta\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 readonly configSchemas: Schema[] = [...super.configSchemas, IndexedDbArchivistConfigSchema]\n static override readonly defaultConfigSchema: Schema = IndexedDbArchivistConfigSchema\n static readonly defaultDbName = 'archivist'\n static readonly defaultDbVersion = 1\n static readonly defaultStoreName = 'payloads'\n private static readonly dataHashIndex: IndexDescription = {\n key: { _dataHash: 1 }, multiEntry: false, unique: false,\n }\n\n private static readonly hashIndex: IndexDescription = {\n key: { _hash: 1 }, multiEntry: false, unique: true,\n }\n\n private static readonly schemaIndex: IndexDescription = {\n key: { schema: 1 }, multiEntry: false, unique: false,\n }\n\n private static readonly sequenceIndex: IndexDescription = {\n key: { _sequence: 1 }, multiEntry: false, unique: true,\n }\n\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 dataHashIndexName = buildStandardIndexName(IndexedDbArchivist.dataHashIndex)\n // eslint-disable-next-line @typescript-eslint/member-ordering\n static readonly schemaIndexName = buildStandardIndexName(IndexedDbArchivist.schemaIndex)\n // eslint-disable-next-line @typescript-eslint/member-ordering\n static readonly sequenceIndexName = buildStandardIndexName(IndexedDbArchivist.sequenceIndex)\n\n private _dbName?: string\n private _dbVersion?: number\n private _storeName?: string\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 if (!this._dbName) {\n if (this.config?.dbName) {\n this._dbName = this.config?.dbName\n } else {\n if (this.config?.name) {\n this.logger.warn('No dbName provided, using module name: ', this.config?.name)\n this._dbName = this.config?.name\n } else {\n this.logger.warn('No dbName provided, using default name: ', IndexedDbArchivist.defaultDbName)\n this._dbName = IndexedDbArchivist.defaultDbName\n }\n }\n }\n return assertEx(this._dbName)\n }\n\n /**\n * The database version. If not supplied via config, it defaults to 1.\n */\n get dbVersion() {\n this._dbVersion = this._dbVersion ?? this.config?.dbVersion ?? IndexedDbArchivist.defaultDbVersion\n return this._dbVersion\n }\n\n override get queries() {\n return [\n ArchivistNextQuerySchema,\n ArchivistAllQuerySchema,\n ArchivistClearQuerySchema,\n ArchivistDeleteQuerySchema,\n ArchivistInsertQuerySchema,\n ...super.queries,\n ]\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 if (!this._storeName) {\n if (this.config?.storeName) {\n this._storeName = this.config?.storeName\n } else {\n this.logger.warn('No storeName provided, using default name: ', IndexedDbArchivist.defaultStoreName)\n this._storeName = IndexedDbArchivist.defaultStoreName\n }\n }\n return assertEx(this._storeName)\n }\n\n /**\n * The indexes to create on the store\n */\n private get indexes() {\n return [\n IndexedDbArchivist.dataHashIndex,\n IndexedDbArchivist.hashIndex,\n IndexedDbArchivist.schemaIndex,\n IndexedDbArchivist.sequenceIndex,\n ...(this.config?.storage?.indexes ?? []),\n ]\n }\n\n protected override async allHandler(): Promise<WithStorageMeta<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\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: Hash[]): Promise<Hash[]> {\n // Filter duplicates to prevent unnecessary DB queries\n const uniqueHashes = [...new Set(hashes)]\n const pairs = await PayloadBuilder.hashPairs(await this.getHandler(uniqueHashes))\n const hashesToDelete = (await Promise.all(pairs.map(async (pair) => {\n const dataHash0 = await PayloadBuilder.dataHash(pair[0])\n return [dataHash0, pair[1]]\n }))).flat()\n // Remove any duplicates\n const distinctHashes = [...new Set(hashesToDelete)]\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\n = (await db.getKeyFromIndex(this.storeName, IndexedDbArchivist.hashIndexName, hash))\n ?? (await db.getKeyFromIndex(this.storeName, IndexedDbArchivist.dataHashIndexName, 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).filter(hash => uniqueHashes.includes(hash))\n })\n }\n\n protected async getFromCursor(\n db: IDBPDatabase<ObjectStore>,\n storeName: string,\n order: 'asc' | 'desc' = 'asc',\n limit: number = 10,\n cursor?: Hex,\n ): Promise<WithStorageMeta[]> {\n // TODO: We have to handle the case where the cursor is not found, and then find the correct cursor to start with (thunked cursor)\n\n return await withReadOnlyStore(db, storeName, async (store) => {\n const sequenceIndex = assertEx(store?.index(IndexedDbArchivist.sequenceIndexName), () => 'Failed to get sequence index')\n let sequenceCursor: IDBPCursorWithValue<ObjectStore, [string]> | null | undefined\n const parsedCursor = cursor\n ? order === 'asc'\n ? IDBKeyRange.lowerBound(cursor, false)\n : IDBKeyRange.upperBound(cursor, false)\n : null\n\n sequenceCursor = await sequenceIndex.openCursor(\n parsedCursor,\n order === 'desc' ? 'prev' : 'next',\n )\n\n if (cursor) {\n sequenceCursor = await sequenceCursor?.advance(1)\n }\n\n let remaining = limit\n const result: WithStorageMeta[] = []\n while (remaining) {\n const value = sequenceCursor?.value\n if (value) {\n result.push(value)\n }\n try {\n sequenceCursor = await sequenceCursor?.advance(1)\n } catch {\n break\n }\n if (sequenceCursor === null) {\n break\n }\n remaining--\n }\n return result\n })\n }\n\n /**\n * Uses an index to get a payload by the index value, but returns the value with the primary key (from the root store)\n * @param db The db instance to use\n * @param storeName The name of the store to use\n * @param indexName The index to use\n * @param key The key to get from the index\n * @returns The primary key and the payload, or undefined if not found\n */\n protected async getFromIndexWithPrimaryKey(\n db: IDBPDatabase<ObjectStore>,\n storeName: string,\n indexName: string,\n key: IDBValidKey,\n ): Promise<[number, WithStorageMeta] | undefined> {\n return await withReadOnlyStore(db, storeName, async (store) => {\n if (store) {\n const index = store.index(indexName)\n const cursor = await index.openCursor(key)\n if (cursor) {\n const singleValue = cursor.value\n // NOTE: It's known to be a number because we are using IndexedDB supplied auto-incrementing keys\n if (typeof cursor.primaryKey !== 'number') {\n throw new TypeError('primaryKey must be a number')\n }\n\n return [cursor.primaryKey, singleValue]\n }\n }\n })\n }\n\n protected override async getHandler(hashes: string[]): Promise<WithStorageMeta[]> {\n const payloads = await this.useDb(db =>\n Promise.all(\n // Filter duplicates to prevent unnecessary DB queries\n uniq(hashes).map(async (hash) => {\n // Find by hash\n const payload = await this.getFromIndexWithPrimaryKey(db, this.storeName, IndexedDbArchivist.hashIndexName, hash)\n // If found, return\n if (payload) return payload\n // Otherwise, find by data hash\n return this.getFromIndexWithPrimaryKey(db, this.storeName, IndexedDbArchivist.dataHashIndexName, hash)\n }),\n ))\n\n const found = new Set<string>()\n return (\n payloads\n // Filter out not found\n .filter(exists)\n // Sort by primary key\n .sort((a, b) => a![0] - b![0])\n // Filter out duplicates by hash\n .filter(([_key, payload]) => {\n if (found.has(payload._hash)) {\n return false\n } else {\n found.add(payload._hash)\n return true\n }\n })\n // Return just the payloads\n .map(([_key, payload]) => payload)\n )\n }\n\n protected override async insertHandler(payloads: WithStorageMeta<Payload>[]): Promise<WithStorageMeta<Payload>[]> {\n return await this.useDb(async (db) => {\n // Perform all inserts via a single transaction to ensure atomicity\n // with respect to checking for the pre-existence of the hash.\n // This is done to prevent duplicate root hashes due to race\n // conditions between checking vs insertion.\n return await withReadWriteStore(db, this.storeName, async (store) => {\n // Return only the payloads that were successfully inserted\n if (store) {\n const inserted: WithStorageMeta<Payload>[] = []\n await Promise.all(\n payloads.map(async (payload) => {\n // only insert if hash does not already exist\n if (!await store.index(IndexedDbArchivist.hashIndexName).get(payload._hash)) {\n // Insert the payload\n await store.put(payload)\n // Add it to the inserted list\n inserted.push(payload)\n }\n }),\n )\n return inserted\n } else {\n throw new Error('Failed to get store')\n }\n })\n })\n }\n\n protected override async nextHandler(options?: ArchivistNextOptions): Promise<WithStorageMeta<Payload>[]> {\n const {\n limit, cursor, order,\n } = options ?? {}\n return await this.useDb(async (db) => {\n return await this.getFromCursor(db, this.storeName, order, limit ?? 10, cursor)\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 * 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<ObjectStore>) => Promise<T> | T): Promise<T> {\n return await withDb<ObjectStore, T>(this.dbName, async (db) => {\n return await callback(db)\n }, { [this.storeName]: this.indexes }, this.logger)\n }\n}\n","export type IndexedDbArchivistSchema = 'network.xyo.archivist.indexeddb'\nexport const IndexedDbArchivistSchema: IndexedDbArchivistSchema = 'network.xyo.archivist.indexeddb'\n","import type { ArchivistConfig } from '@xyo-network/archivist-model'\n\nimport { IndexedDbArchivistSchema } from './Schema.ts'\n\nexport type IndexedDbArchivistConfigSchema = `${IndexedDbArchivistSchema}.config`\nexport const IndexedDbArchivistConfigSchema: IndexedDbArchivistConfigSchema = `${IndexedDbArchivistSchema}.config`\n\nexport type IndexedDbArchivistConfig<TStoreName extends string = string> = 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 name of the object store\n */\n storeName?: TStoreName\n}>\n"],"mappings":";;;;AAAA,SAASA,YAAY;AACrB,SAASC,gBAAgB;AACzB,SAASC,cAAc;AAEvB,SAEEC,QACAC,mBAAmBC,0BACd;AACP,SAASC,yBAAyB;AAClC,SACEC,yBACAC,2BACAC,4BACAC,4BAGAC,0BACAC,8BAEK;AACP,SAASC,uBAAuB;AAChC,SAASC,sBAAsB;;;ACrBxB,IAAMC,2BAAqD;;;ACI3D,IAAMC,iCAAiE,GAAGC,wBAAAA;;;;;;;;;;AF+B1E,IAAMC,qBAAN,MAAMA,4BAGHC,kBAAAA;SAAAA;;;EACR,OAAyBC,gBAA0B;OAAI,MAAMA;IAAeC;;EAC5E,OAAyBC,sBAA8BD;EACvD,OAAgBE,gBAAgB;EAChC,OAAgBC,mBAAmB;EACnC,OAAgBC,mBAAmB;EACnC,OAAwBC,gBAAkC;IACxDC,KAAK;MAAEC,WAAW;IAAE;IAAGC,YAAY;IAAOC,QAAQ;EACpD;EAEA,OAAwBC,YAA8B;IACpDJ,KAAK;MAAEK,OAAO;IAAE;IAAGH,YAAY;IAAOC,QAAQ;EAChD;EAEA,OAAwBG,cAAgC;IACtDN,KAAK;MAAEO,QAAQ;IAAE;IAAGL,YAAY;IAAOC,QAAQ;EACjD;EAEA,OAAwBK,gBAAkC;IACxDR,KAAK;MAAES,WAAW;IAAE;IAAGP,YAAY;IAAOC,QAAQ;EACpD;;EAGA,OAAgBO,gBAAgBC,uBAAuBpB,oBAAmBa,SAAS;;EAEnF,OAAgBQ,oBAAoBD,uBAAuBpB,oBAAmBQ,aAAa;;EAE3F,OAAgBc,kBAAkBF,uBAAuBpB,oBAAmBe,WAAW;;EAEvF,OAAgBQ,oBAAoBH,uBAAuBpB,oBAAmBiB,aAAa;EAEnFO;EACAC;EACAC;;;;;;;;EASR,IAAIC,SAAS;AAC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MAAMA,aAAAA;AAGZ,UAAM,KAAKrE,MAAM,MAAA;IAAO,CAAA;AACxB,WAAO;EACT;;;;;;EAOA,MAAcA,MAASsE,UAAyE;AAC9F,WAAO,MAAMC,OAAuB,KAAKzF,QAAQ,OAAOmB,OAAAA;AACtD,aAAO,MAAMqE,SAASrE,EAAAA;IACxB,GAAG;MAAE,CAAC,KAAKN,SAAS,GAAG,KAAKC;IAAQ,GAAG,KAAKX,MAAM;EACpD;AACF;;;;","names":["uniq","assertEx","exists","withDb","withReadOnlyStore","withReadWriteStore","AbstractArchivist","ArchivistAllQuerySchema","ArchivistClearQuerySchema","ArchivistDeleteQuerySchema","ArchivistInsertQuerySchema","ArchivistNextQuerySchema","buildStandardIndexName","creatableModule","PayloadBuilder","IndexedDbArchivistSchema","IndexedDbArchivistConfigSchema","IndexedDbArchivistSchema","IndexedDbArchivist","AbstractArchivist","configSchemas","IndexedDbArchivistConfigSchema","defaultConfigSchema","defaultDbName","defaultDbVersion","defaultStoreName","dataHashIndex","key","_dataHash","multiEntry","unique","hashIndex","_hash","schemaIndex","schema","sequenceIndex","_sequence","hashIndexName","buildStandardIndexName","dataHashIndexName","schemaIndexName","sequenceIndexName","_dbName","_dbVersion","_storeName","dbName","config","name","logger","warn","assertEx","dbVersion","queries","ArchivistNextQuerySchema","ArchivistAllQuerySchema","ArchivistClearQuerySchema","ArchivistDeleteQuerySchema","ArchivistInsertQuerySchema","storeName","indexes","storage","allHandler","payloads","useDb","db","getAll","clearHandler","clear","deleteHandler","hashes","uniqueHashes","Set","pairs","PayloadBuilder","hashPairs","getHandler","hashesToDelete","Promise","all","map","pair","dataHash0","dataHash","flat","distinctHashes","found","hash","existing","getKeyFromIndex","delete","filter","exists","includes","getFromCursor","order","limit","cursor","withReadOnlyStore","store","index","sequenceCursor","parsedCursor","IDBKeyRange","lowerBound","upperBound","openCursor","advance","remaining","result","value","push","getFromIndexWithPrimaryKey","indexName","singleValue","primaryKey","TypeError","uniq","payload","sort","a","b","_key","has","add","insertHandler","withReadWriteStore","inserted","get","put","Error","nextHandler","options","startHandler","callback","withDb"]}
|
|
1
|
+
{"version":3,"sources":["../../src/Archivist.ts","../../src/Schema.ts","../../src/Config.ts"],"sourcesContent":["import { uniq } from '@xylabs/array'\nimport { assertEx } from '@xylabs/assert'\nimport { exists } from '@xylabs/exists'\nimport { Hash, Hex } from '@xylabs/hex'\nimport {\n ObjectStore,\n withDb,\n withReadOnlyStore, withReadWriteStore,\n} from '@xylabs/indexed-db'\nimport { AbstractArchivist } from '@xyo-network/archivist-abstract'\nimport {\n ArchivistAllQuerySchema,\n ArchivistClearQuerySchema,\n ArchivistDeleteQuerySchema,\n ArchivistInsertQuerySchema,\n ArchivistModuleEventData,\n ArchivistNextOptions,\n ArchivistNextQuerySchema,\n buildStandardIndexName,\n IndexDescription,\n} from '@xyo-network/archivist-model'\nimport { creatableModule } from '@xyo-network/module-model'\nimport { PayloadBuilder } from '@xyo-network/payload-builder'\nimport {\n Payload, Schema, WithStorageMeta,\n} from '@xyo-network/payload-model'\nimport { IDBPCursorWithValue, IDBPDatabase } from 'idb'\n\nimport { IndexedDbArchivistConfigSchema } from './Config.ts'\nimport { IndexedDbArchivistParams } from './Params.ts'\n\nexport interface PayloadStore {\n [s: string]: WithStorageMeta\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 readonly configSchemas: Schema[] = [...super.configSchemas, IndexedDbArchivistConfigSchema]\n static override readonly defaultConfigSchema: Schema = IndexedDbArchivistConfigSchema\n static readonly defaultDbName = 'archivist'\n static readonly defaultDbVersion = 1\n static readonly defaultStoreName = 'payloads'\n private static readonly dataHashIndex: IndexDescription = {\n key: { _dataHash: 1 }, multiEntry: false, unique: false,\n }\n\n private static readonly hashIndex: IndexDescription = {\n key: { _hash: 1 }, multiEntry: false, unique: true,\n }\n\n private static readonly schemaIndex: IndexDescription = {\n key: { schema: 1 }, multiEntry: false, unique: false,\n }\n\n private static readonly sequenceIndex: IndexDescription = {\n key: { _sequence: 1 }, multiEntry: false, unique: true,\n }\n\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 dataHashIndexName = buildStandardIndexName(IndexedDbArchivist.dataHashIndex)\n // eslint-disable-next-line @typescript-eslint/member-ordering\n static readonly schemaIndexName = buildStandardIndexName(IndexedDbArchivist.schemaIndex)\n // eslint-disable-next-line @typescript-eslint/member-ordering\n static readonly sequenceIndexName = buildStandardIndexName(IndexedDbArchivist.sequenceIndex)\n\n private _dbName?: string\n private _dbVersion?: number\n private _storeName?: string\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 if (!this._dbName) {\n if (this.config?.dbName) {\n this._dbName = this.config?.dbName\n } else {\n if (this.config?.name) {\n this.logger.warn('No dbName provided, using module name: ', this.config?.name)\n this._dbName = this.config?.name\n } else {\n this.logger.warn('No dbName provided, using default name: ', IndexedDbArchivist.defaultDbName)\n this._dbName = IndexedDbArchivist.defaultDbName\n }\n }\n }\n return assertEx(this._dbName)\n }\n\n /**\n * The database version. If not supplied via config, it defaults to 1.\n */\n get dbVersion() {\n this._dbVersion = this._dbVersion ?? this.config?.dbVersion ?? IndexedDbArchivist.defaultDbVersion\n return this._dbVersion\n }\n\n override get queries() {\n return [\n ArchivistNextQuerySchema,\n ArchivistAllQuerySchema,\n ArchivistClearQuerySchema,\n ArchivistDeleteQuerySchema,\n ArchivistInsertQuerySchema,\n ...super.queries,\n ]\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 if (!this._storeName) {\n if (this.config?.storeName) {\n this._storeName = this.config?.storeName\n } else {\n this.logger.warn('No storeName provided, using default name: ', IndexedDbArchivist.defaultStoreName)\n this._storeName = IndexedDbArchivist.defaultStoreName\n }\n }\n return assertEx(this._storeName)\n }\n\n /**\n * The indexes to create on the store\n */\n private get indexes() {\n return [\n IndexedDbArchivist.dataHashIndex,\n IndexedDbArchivist.hashIndex,\n IndexedDbArchivist.schemaIndex,\n IndexedDbArchivist.sequenceIndex,\n ...(this.config?.storage?.indexes ?? []),\n ]\n }\n\n protected override async allHandler(): Promise<WithStorageMeta<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\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: Hash[]): Promise<Hash[]> {\n // Filter duplicates to prevent unnecessary DB queries\n const uniqueHashes = [...new Set(hashes)]\n const pairs = await PayloadBuilder.hashPairs(await this.getHandler(uniqueHashes))\n const hashesToDelete = (await Promise.all(pairs.map(async (pair) => {\n const dataHash0 = await PayloadBuilder.dataHash(pair[0])\n return [dataHash0, pair[1]]\n }))).flat()\n // Remove any duplicates\n const distinctHashes = [...new Set(hashesToDelete)]\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\n = (await db.getKeyFromIndex(this.storeName, IndexedDbArchivist.hashIndexName, hash))\n ?? (await db.getKeyFromIndex(this.storeName, IndexedDbArchivist.dataHashIndexName, 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).filter(hash => uniqueHashes.includes(hash))\n })\n }\n\n protected async getFromCursor(\n db: IDBPDatabase<ObjectStore>,\n storeName: string,\n order: 'asc' | 'desc' = 'asc',\n limit: number = 10,\n cursor?: Hex,\n ): Promise<WithStorageMeta[]> {\n // TODO: We have to handle the case where the cursor is not found, and then find the correct cursor to start with (thunked cursor)\n\n return await withReadOnlyStore(db, storeName, async (store) => {\n const sequenceIndex = assertEx(store?.index(IndexedDbArchivist.sequenceIndexName), () => 'Failed to get sequence index')\n let sequenceCursor: IDBPCursorWithValue<ObjectStore, [string]> | null | undefined\n const parsedCursor = cursor\n ? order === 'asc'\n ? IDBKeyRange.lowerBound(cursor, false)\n : IDBKeyRange.upperBound(cursor, false)\n : null\n\n sequenceCursor = await sequenceIndex.openCursor(\n parsedCursor,\n order === 'desc' ? 'prev' : 'next',\n )\n\n if (cursor) {\n sequenceCursor = await sequenceCursor?.advance(1)\n }\n\n let remaining = limit\n const result: WithStorageMeta[] = []\n while (remaining) {\n const value = sequenceCursor?.value\n if (value) {\n result.push(value)\n }\n try {\n sequenceCursor = await sequenceCursor?.advance(1)\n } catch {\n break\n }\n if (sequenceCursor === null) {\n break\n }\n remaining--\n }\n return result\n })\n }\n\n /**\n * Uses an index to get a payload by the index value, but returns the value with the primary key (from the root store)\n * @param db The db instance to use\n * @param storeName The name of the store to use\n * @param indexName The index to use\n * @param key The key to get from the index\n * @returns The primary key and the payload, or undefined if not found\n */\n protected async getFromIndexWithPrimaryKey(\n db: IDBPDatabase<ObjectStore>,\n storeName: string,\n indexName: string,\n key: IDBValidKey,\n ): Promise<[number, WithStorageMeta] | undefined> {\n return await withReadOnlyStore(db, storeName, async (store) => {\n if (store) {\n const index = store.index(indexName)\n const cursor = await index.openCursor(key)\n if (cursor) {\n const singleValue = cursor.value\n // NOTE: It's known to be a number because we are using IndexedDB supplied auto-incrementing keys\n if (typeof cursor.primaryKey !== 'number') {\n throw new TypeError('primaryKey must be a number')\n }\n\n return [cursor.primaryKey, singleValue]\n }\n }\n })\n }\n\n protected override async getHandler(hashes: string[]): Promise<WithStorageMeta[]> {\n const payloads = await this.useDb(db =>\n Promise.all(\n // Filter duplicates to prevent unnecessary DB queries\n uniq(hashes).map(async (hash) => {\n // Find by hash\n const payload = await this.getFromIndexWithPrimaryKey(db, this.storeName, IndexedDbArchivist.hashIndexName, hash)\n // If found, return\n if (payload) return payload\n // Otherwise, find by data hash\n return this.getFromIndexWithPrimaryKey(db, this.storeName, IndexedDbArchivist.dataHashIndexName, hash)\n }),\n ))\n\n const found = new Set<string>()\n return (\n payloads\n // Filter out not found\n .filter(exists)\n // Sort by primary key\n .sort((a, b) => a![0] - b![0])\n // Filter out duplicates by hash\n .filter(([_key, payload]) => {\n if (found.has(payload._hash)) {\n return false\n } else {\n found.add(payload._hash)\n return true\n }\n })\n // Return just the payloads\n .map(([_key, payload]) => payload)\n )\n }\n\n protected override async insertHandler(payloads: WithStorageMeta<Payload>[]): Promise<WithStorageMeta<Payload>[]> {\n return await this.useDb(async (db) => {\n // Perform all inserts via a single transaction to ensure atomicity\n // with respect to checking for the pre-existence of the hash.\n // This is done to prevent duplicate root hashes due to race\n // conditions between checking vs insertion.\n return await withReadWriteStore(db, this.storeName, async (store) => {\n // Return only the payloads that were successfully inserted\n if (store) {\n const inserted: WithStorageMeta<Payload>[] = []\n await Promise.all(\n payloads.map(async (payload) => {\n // only insert if hash does not already exist\n if (!await store.index(IndexedDbArchivist.hashIndexName).get(payload._hash)) {\n // Insert the payload\n await store.put(payload)\n // Add it to the inserted list\n inserted.push(payload)\n }\n }),\n )\n return inserted\n } else {\n throw new Error('Failed to get store')\n }\n })\n })\n }\n\n protected override async nextHandler(options?: ArchivistNextOptions): Promise<WithStorageMeta<Payload>[]> {\n const {\n limit, cursor, order,\n } = options ?? {}\n return await this.useDb(async (db) => {\n return await this.getFromCursor(db, this.storeName, order, limit ?? 10, cursor)\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 * 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<ObjectStore>) => Promise<T> | T): Promise<T> {\n return await withDb<ObjectStore, T>(this.dbName, async (db) => {\n return await callback(db)\n }, { [this.storeName]: this.indexes }, this.logger)\n }\n}\n","export const IndexedDbArchivistSchema = 'network.xyo.archivist.indexeddb' as const\nexport type IndexedDbArchivistSchema = typeof IndexedDbArchivistSchema\n","import type { ArchivistConfig } from '@xyo-network/archivist-model'\n\nimport { IndexedDbArchivistSchema } from './Schema.ts'\n\nexport type IndexedDbArchivistConfigSchema = `${IndexedDbArchivistSchema}.config`\nexport const IndexedDbArchivistConfigSchema: IndexedDbArchivistConfigSchema = `${IndexedDbArchivistSchema}.config`\n\nexport type IndexedDbArchivistConfig<TStoreName extends string = string> = 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 name of the object store\n */\n storeName?: TStoreName\n}>\n"],"mappings":";;;;;;;;;;;;;;;;;AAAA,SAAS,YAAY;AACrB,SAAS,gBAAgB;AACzB,SAAS,cAAc;AAEvB;AAAA,EAEE;AAAA,EACA;AAAA,EAAmB;AAAA,OACd;AACP,SAAS,yBAAyB;AAClC;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAGA;AAAA,EACA;AAAA,OAEK;AACP,SAAS,uBAAuB;AAChC,SAAS,sBAAsB;;;ACtBxB,IAAM,2BAA2B;;;ACKjC,IAAM,iCAAiE,GAAG,wBAAwB;;;AF+BlG,IAAM,qBAAN,cAGG,kBAAuC;AAAA,EA+BvC;AAAA,EACA;AAAA,EACA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASR,IAAI,SAAS;AACX,QAAI,CAAC,KAAK,SAAS;AACjB,UAAI,KAAK,QAAQ,QAAQ;AACvB,aAAK,UAAU,KAAK,QAAQ;AAAA,MAC9B,OAAO;AACL,YAAI,KAAK,QAAQ,MAAM;AACrB,eAAK,O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
1
1
|
{
|
|
2
2
|
"name": "@xyo-network/archivist-indexeddb",
|
|
3
|
-
"version": "3.6.
|
|
3
|
+
"version": "3.6.11",
|
|
4
4
|
"description": "Primary SDK for using XYO Protocol 2.0",
|
|
5
5
|
"homepage": "https://xyo.network",
|
|
6
6
|
"bugs": {
|
|
@@ -29,29 +29,29 @@
|
|
|
29
29
|
"module": "dist/browser/index.mjs",
|
|
30
30
|
"types": "dist/browser/index.d.ts",
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@xylabs/array": "^4.
|
|
33
|
-
"@xylabs/assert": "^4.
|
|
34
|
-
"@xylabs/exists": "^4.
|
|
35
|
-
"@xylabs/hex": "^4.
|
|
36
|
-
"@xylabs/indexed-db": "^4.
|
|
37
|
-
"@xyo-network/archivist-abstract": "^3.6.
|
|
38
|
-
"@xyo-network/archivist-model": "^3.6.
|
|
39
|
-
"@xyo-network/module-model": "^3.6.
|
|
40
|
-
"@xyo-network/payload-builder": "^3.6.
|
|
41
|
-
"@xyo-network/payload-model": "^3.6.
|
|
32
|
+
"@xylabs/array": "^4.5.1",
|
|
33
|
+
"@xylabs/assert": "^4.5.1",
|
|
34
|
+
"@xylabs/exists": "^4.5.1",
|
|
35
|
+
"@xylabs/hex": "^4.5.1",
|
|
36
|
+
"@xylabs/indexed-db": "^4.5.1",
|
|
37
|
+
"@xyo-network/archivist-abstract": "^3.6.11",
|
|
38
|
+
"@xyo-network/archivist-model": "^3.6.11",
|
|
39
|
+
"@xyo-network/module-model": "^3.6.11",
|
|
40
|
+
"@xyo-network/payload-builder": "^3.6.11",
|
|
41
|
+
"@xyo-network/payload-model": "^3.6.11",
|
|
42
42
|
"idb": "^8.0.1"
|
|
43
43
|
},
|
|
44
44
|
"devDependencies": {
|
|
45
|
-
"@xylabs/delay": "^4.
|
|
46
|
-
"@xylabs/object": "^4.
|
|
45
|
+
"@xylabs/delay": "^4.5.1",
|
|
46
|
+
"@xylabs/object": "^4.5.1",
|
|
47
47
|
"@xylabs/ts-scripts-yarn3": "^4.2.6",
|
|
48
48
|
"@xylabs/tsconfig": "^4.2.6",
|
|
49
|
-
"@xyo-network/account": "^3.6.
|
|
50
|
-
"@xyo-network/id-payload-plugin": "^3.6.
|
|
51
|
-
"@xyo-network/payload-wrapper": "^3.6.
|
|
49
|
+
"@xyo-network/account": "^3.6.11",
|
|
50
|
+
"@xyo-network/id-payload-plugin": "^3.6.11",
|
|
51
|
+
"@xyo-network/payload-wrapper": "^3.6.11",
|
|
52
52
|
"fake-indexeddb": "^6.0.0",
|
|
53
|
-
"typescript": "^5.7.
|
|
54
|
-
"vitest": "^
|
|
53
|
+
"typescript": "^5.7.3",
|
|
54
|
+
"vitest": "^3.0.4"
|
|
55
55
|
},
|
|
56
56
|
"publishConfig": {
|
|
57
57
|
"access": "public"
|
package/src/Archivist.ts
CHANGED
|
@@ -172,7 +172,7 @@ export class IndexedDbArchivist<
|
|
|
172
172
|
// Check if the hash exists
|
|
173
173
|
const existing
|
|
174
174
|
= (await db.getKeyFromIndex(this.storeName, IndexedDbArchivist.hashIndexName, hash))
|
|
175
|
-
|
|
175
|
+
?? (await db.getKeyFromIndex(this.storeName, IndexedDbArchivist.dataHashIndexName, hash))
|
|
176
176
|
// If it does exist
|
|
177
177
|
if (existing) {
|
|
178
178
|
// Delete it
|
package/src/Schema.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export
|
|
2
|
-
export
|
|
1
|
+
export const IndexedDbArchivistSchema = 'network.xyo.archivist.indexeddb' as const
|
|
2
|
+
export type IndexedDbArchivistSchema = typeof IndexedDbArchivistSchema
|