@xyo-network/archivist-indexeddb 2.74.5 → 2.75.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/IndexedDbArchivist.cjs +130 -0
- package/dist/browser/IndexedDbArchivist.cjs.map +1 -0
- package/dist/browser/IndexedDbArchivist.d.mts.map +1 -0
- package/dist/browser/IndexedDbArchivist.d.ts.map +1 -0
- package/dist/browser/IndexedDbArchivist.js +111 -0
- package/dist/browser/IndexedDbArchivist.js.map +1 -0
- package/dist/{index.js → browser/index.cjs} +1 -6
- package/dist/browser/index.cjs.map +1 -0
- package/dist/{index.d.mts.map → browser/index.d.mts.map} +1 -1
- package/dist/{index.d.ts.map → browser/index.d.ts.map} +1 -1
- package/dist/{index.mjs → browser/index.js} +1 -1
- package/dist/browser/index.js.map +1 -0
- package/dist/browser/sample.archivist.xyo.json +16 -0
- package/dist/node/IndexedDbArchivist.d.mts +52 -0
- package/dist/node/IndexedDbArchivist.d.mts.map +1 -0
- package/dist/node/IndexedDbArchivist.d.ts +52 -0
- package/dist/node/IndexedDbArchivist.d.ts.map +1 -0
- package/dist/node/IndexedDbArchivist.js +133 -0
- package/dist/node/IndexedDbArchivist.js.map +1 -0
- package/dist/node/IndexedDbArchivist.mjs +109 -0
- package/dist/node/IndexedDbArchivist.mjs.map +1 -0
- package/dist/node/index.d.mts +2 -0
- package/dist/node/index.d.mts.map +1 -0
- package/dist/node/index.d.ts +2 -0
- package/dist/node/index.d.ts.map +1 -0
- package/dist/node/index.js +23 -0
- package/dist/node/index.js.map +1 -0
- package/dist/node/index.mjs +2 -0
- package/dist/node/index.mjs.map +1 -0
- package/dist/node/sample.archivist.xyo.json +16 -0
- package/package.json +34 -31
- package/dist/IndexedDbArchivist.d.mts.map +0 -1
- package/dist/IndexedDbArchivist.d.ts.map +0 -1
- package/dist/docs.json +0 -22913
- package/dist/index.js.map +0 -1
- package/dist/index.mjs.map +0 -1
- /package/dist/{IndexedDbArchivist.d.mts → browser/IndexedDbArchivist.d.mts} +0 -0
- /package/dist/{IndexedDbArchivist.d.ts → browser/IndexedDbArchivist.d.ts} +0 -0
- /package/dist/{index.d.mts → browser/index.d.mts} +0 -0
- /package/dist/{index.d.ts → browser/index.d.ts} +0 -0
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
+
|
|
21
|
+
// src/IndexedDbArchivist.ts
|
|
22
|
+
var IndexedDbArchivist_exports = {};
|
|
23
|
+
__export(IndexedDbArchivist_exports, {
|
|
24
|
+
IndexedDbArchivist: () => IndexedDbArchivist,
|
|
25
|
+
IndexedDbArchivistConfigSchema: () => IndexedDbArchivistConfigSchema
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(IndexedDbArchivist_exports);
|
|
28
|
+
var import_assert = require("@xylabs/assert");
|
|
29
|
+
var import_archivist_abstract = require("@xyo-network/archivist-abstract");
|
|
30
|
+
var import_archivist_model = require("@xyo-network/archivist-model");
|
|
31
|
+
var import_core = require("@xyo-network/core");
|
|
32
|
+
var import_module = require("@xyo-network/module");
|
|
33
|
+
var import_idb_keyval = require("idb-keyval");
|
|
34
|
+
function _ts_decorate(decorators, target, key, desc) {
|
|
35
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
36
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
|
|
37
|
+
r = Reflect.decorate(decorators, target, key, desc);
|
|
38
|
+
else
|
|
39
|
+
for (var i = decorators.length - 1; i >= 0; i--)
|
|
40
|
+
if (d = decorators[i])
|
|
41
|
+
r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
42
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
43
|
+
}
|
|
44
|
+
__name(_ts_decorate, "_ts_decorate");
|
|
45
|
+
var IndexedDbArchivistConfigSchema = "network.xyo.archivist.indexeddb.config";
|
|
46
|
+
var IndexedDbArchivist = class IndexedDbArchivist2 extends import_archivist_abstract.AbstractArchivist {
|
|
47
|
+
static {
|
|
48
|
+
__name(this, "IndexedDbArchivist");
|
|
49
|
+
}
|
|
50
|
+
static configSchemas = [
|
|
51
|
+
IndexedDbArchivistConfigSchema
|
|
52
|
+
];
|
|
53
|
+
static defaultDbName = "archivist";
|
|
54
|
+
static defaultStoreName = "payloads";
|
|
55
|
+
_db;
|
|
56
|
+
/**
|
|
57
|
+
* The database name. If not supplied via config, it defaults
|
|
58
|
+
* to the module name (not guaranteed to be unique) and if module
|
|
59
|
+
* name is not supplied, it defaults to `archivist`. This behavior
|
|
60
|
+
* biases towards a single, isolated DB per archivist which seems to
|
|
61
|
+
* make the most sense for 99% of use cases.
|
|
62
|
+
*/
|
|
63
|
+
get dbName() {
|
|
64
|
+
return this.config?.dbName ?? this.config?.name ?? IndexedDbArchivist2.defaultDbName;
|
|
65
|
+
}
|
|
66
|
+
get queries() {
|
|
67
|
+
return [
|
|
68
|
+
import_archivist_model.ArchivistAllQuerySchema,
|
|
69
|
+
import_archivist_model.ArchivistClearQuerySchema,
|
|
70
|
+
import_archivist_model.ArchivistDeleteQuerySchema,
|
|
71
|
+
import_archivist_model.ArchivistInsertQuerySchema,
|
|
72
|
+
...super.queries
|
|
73
|
+
];
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* The name of the object store. If not supplied via config, it defaults
|
|
77
|
+
* to `payloads`. The limitation of the current IndexedDB wrapper we're
|
|
78
|
+
* using is that it only supports a single object store per DB. See here:
|
|
79
|
+
* https://github.com/jakearchibald/idb-keyval/blob/main/custom-stores.md#defining-a-custom-database--store-name
|
|
80
|
+
* If this becomes a problem or we need migrations/transactions, we can
|
|
81
|
+
* move to this more-flexible library, which they recommend (and who
|
|
82
|
+
* recommends them for our simple use case of key-value storage):
|
|
83
|
+
* https://www.npmjs.com/package/idb
|
|
84
|
+
*/
|
|
85
|
+
get storeName() {
|
|
86
|
+
return this.config?.storeName ?? IndexedDbArchivist2.defaultStoreName;
|
|
87
|
+
}
|
|
88
|
+
get db() {
|
|
89
|
+
return (0, import_assert.assertEx)(this._db, "DB not initialized");
|
|
90
|
+
}
|
|
91
|
+
async allHandler() {
|
|
92
|
+
const result = await (0, import_idb_keyval.entries)(this.db);
|
|
93
|
+
return result.map(([_hash, payload]) => payload);
|
|
94
|
+
}
|
|
95
|
+
async clearHandler() {
|
|
96
|
+
await (0, import_idb_keyval.clear)(this.db);
|
|
97
|
+
}
|
|
98
|
+
async deleteHandler(hashes) {
|
|
99
|
+
const payloadPairs = await Promise.all((await this.get(hashes)).map(async (payload) => [
|
|
100
|
+
await import_core.PayloadHasher.hashAsync(payload),
|
|
101
|
+
payload
|
|
102
|
+
]));
|
|
103
|
+
const foundHashesToDelete = payloadPairs.map(([hash, _]) => hash);
|
|
104
|
+
await (0, import_idb_keyval.delMany)(foundHashesToDelete, this.db);
|
|
105
|
+
return foundHashesToDelete;
|
|
106
|
+
}
|
|
107
|
+
async getHandler(hashes) {
|
|
108
|
+
return (await (0, import_idb_keyval.getMany)(hashes, this.db)).filter((result) => result !== void 0);
|
|
109
|
+
}
|
|
110
|
+
async insertHandler(payloads) {
|
|
111
|
+
const entries2 = await Promise.all(payloads.map(async (payload) => {
|
|
112
|
+
const hash = await import_core.PayloadHasher.hashAsync(payload);
|
|
113
|
+
return [
|
|
114
|
+
hash,
|
|
115
|
+
payload
|
|
116
|
+
];
|
|
117
|
+
}));
|
|
118
|
+
await (0, import_idb_keyval.setMany)(entries2, this.db);
|
|
119
|
+
return payloads;
|
|
120
|
+
}
|
|
121
|
+
async startHandler() {
|
|
122
|
+
await super.startHandler();
|
|
123
|
+
this._db = (0, import_idb_keyval.createStore)(this.dbName, this.storeName);
|
|
124
|
+
return true;
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
IndexedDbArchivist = _ts_decorate([
|
|
128
|
+
(0, import_module.creatableModule)()
|
|
129
|
+
], IndexedDbArchivist);
|
|
130
|
+
//# sourceMappingURL=IndexedDbArchivist.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/IndexedDbArchivist.ts"],"sourcesContent":["import { assertEx } from '@xylabs/assert'\nimport { AbstractArchivist } from '@xyo-network/archivist-abstract'\nimport {\n ArchivistAllQuerySchema,\n ArchivistClearQuerySchema,\n ArchivistConfig,\n ArchivistDeleteQuerySchema,\n ArchivistInsertQuerySchema,\n ArchivistModuleEventData,\n ArchivistParams,\n} from '@xyo-network/archivist-model'\nimport { PayloadHasher } from '@xyo-network/core'\nimport { AnyConfigSchema, creatableModule } from '@xyo-network/module'\nimport { Payload } from '@xyo-network/payload-model'\nimport { clear, createStore, delMany, entries, getMany, setMany, UseStore } from 'idb-keyval'\n\nexport type IndexedDbArchivistConfigSchema = 'network.xyo.archivist.indexeddb.config'\nexport const IndexedDbArchivistConfigSchema: IndexedDbArchivistConfigSchema = 'network.xyo.archivist.indexeddb.config'\n\nexport type IndexedDbArchivistConfig = ArchivistConfig<{\n /**\n * The database name\n */\n dbName?: string\n schema: IndexedDbArchivistConfigSchema\n /**\n * The name of the object store\n */\n storeName?: string\n}>\n\nexport type IndexedDbArchivistParams = ArchivistParams<AnyConfigSchema<IndexedDbArchivistConfig>>\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 defaultDbName = 'archivist'\n static defaultStoreName = 'payloads'\n\n private _db: UseStore | 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 override get queries() {\n return [ArchivistAllQuerySchema, ArchivistClearQuerySchema, ArchivistDeleteQuerySchema, ArchivistInsertQuerySchema, ...super.queries]\n }\n /**\n * The name of the object store. If not supplied via config, it defaults\n * to `payloads`. The limitation of the current IndexedDB wrapper we're\n * using is that it only supports a single object store per DB. See here:\n * https://github.com/jakearchibald/idb-keyval/blob/main/custom-stores.md#defining-a-custom-database--store-name\n * If this becomes a problem or we need migrations/transactions, we can\n * move to this more-flexible library, which they recommend (and who\n * recommends them for our simple use case of key-value storage):\n * https://www.npmjs.com/package/idb\n */\n get storeName() {\n return this.config?.storeName ?? IndexedDbArchivist.defaultStoreName\n }\n\n private get db(): UseStore {\n return assertEx(this._db, 'DB not initialized')\n }\n\n protected override async allHandler(): Promise<Payload[]> {\n const result = await entries<string, Payload>(this.db)\n return result.map<Payload>(([_hash, payload]) => payload)\n }\n\n protected override async clearHandler(): Promise<void> {\n await clear(this.db)\n }\n\n protected override async deleteHandler(hashes: string[]): Promise<string[]> {\n const payloadPairs: [string, Payload][] = await Promise.all(\n (await this.get(hashes)).map<Promise<[string, Payload]>>(async (payload) => [await PayloadHasher.hashAsync(payload), payload]),\n )\n\n const foundHashesToDelete = payloadPairs.map(([hash, _]) => hash)\n await delMany(foundHashesToDelete, this.db)\n\n return foundHashesToDelete\n }\n\n protected override async getHandler(hashes: string[]): Promise<Payload[]> {\n return (await getMany<Payload>(hashes, this.db)).filter((result) => result !== undefined)\n }\n\n protected override async insertHandler(payloads: Payload[]): Promise<Payload[]> {\n const entries = await Promise.all(\n payloads.map<Promise<[string, Payload]>>(async (payload) => {\n const hash = await PayloadHasher.hashAsync(payload)\n return [hash, payload]\n }),\n )\n await setMany(entries, this.db)\n\n return payloads\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 this._db = createStore(this.dbName, this.storeName)\n return true\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,oBAAyB;AACzB,gCAAkC;AAClC,6BAQO;AACP,kBAA8B;AAC9B,oBAAiD;AAEjD,wBAAiF;;;;;;;;;;;;AAG1E,IAAMA,iCAAiE;AAiB9E,IAAaC,qBAAN,MAAAA,4BAGGC,4CAAAA;SAAAA;;;EACR,OAAgBC,gBAAgB;IAACH;;EACjC,OAAOI,gBAAgB;EACvB,OAAOC,mBAAmB;EAElBC;;;;;;;;EASR,IAAIC,SAAS;AACX,WAAO,KAAKC,QAAQD,UAAU,KAAKC,QAAQC,QAAQR,oBAAmBG;EACxE;EAEA,IAAaM,UAAU;AACrB,WAAO;MAACC;MAAyBC;MAA2BC;MAA4BC;SAA+B,MAAMJ;;EAC/H;;;;;;;;;;;EAWA,IAAIK,YAAY;AACd,WAAO,KAAKP,QAAQO,aAAad,oBAAmBI;EACtD;EAEA,IAAYW,KAAe;AACzB,eAAOC,wBAAS,KAAKX,KAAK,oBAAA;EAC5B;EAEA,MAAyBY,aAAiC;AACxD,UAAMC,SAAS,UAAMC,2BAAyB,KAAKJ,EAAE;AACrD,WAAOG,OAAOE,IAAa,CAAC,CAACC,OAAOC,OAAAA,MAAaA,OAAAA;EACnD;EAEA,MAAyBC,eAA8B;AACrD,cAAMC,yBAAM,KAAKT,EAAE;EACrB;EAEA,MAAyBU,cAAcC,QAAqC;AAC1E,UAAMC,eAAoC,MAAMC,QAAQC,KACrD,MAAM,KAAKC,IAAIJ,MAAAA,GAASN,IAAgC,OAAOE,YAAY;MAAC,MAAMS,0BAAcC,UAAUV,OAAAA;MAAUA;KAAQ,CAAA;AAG/H,UAAMW,sBAAsBN,aAAaP,IAAI,CAAC,CAACc,MAAMC,CAAAA,MAAOD,IAAAA;AAC5D,cAAME,2BAAQH,qBAAqB,KAAKlB,EAAE;AAE1C,WAAOkB;EACT;EAEA,MAAyBI,WAAWX,QAAsC;AACxE,YAAQ,UAAMY,2BAAiBZ,QAAQ,KAAKX,EAAE,GAAGwB,OAAO,CAACrB,WAAWA,WAAWsB,MAAAA;EACjF;EAEA,MAAyBC,cAAcC,UAAyC;AAC9E,UAAMvB,WAAU,MAAMS,QAAQC,IAC5Ba,SAAStB,IAAgC,OAAOE,YAAAA;AAC9C,YAAMY,OAAO,MAAMH,0BAAcC,UAAUV,OAAAA;AAC3C,aAAO;QAACY;QAAMZ;;IAChB,CAAA,CAAA;AAEF,cAAMqB,2BAAQxB,UAAS,KAAKJ,EAAE;AAE9B,WAAO2B;EACT;EAEA,MAAyBE,eAAe;AACtC,UAAM,MAAMA,aAAAA;AAGZ,SAAKvC,UAAMwC,+BAAY,KAAKvC,QAAQ,KAAKQ,SAAS;AAClD,WAAO;EACT;AACF;AArFad,qBAAAA,aAAAA;MADZ8C,+BAAAA;GACY9C,kBAAAA;","names":["IndexedDbArchivistConfigSchema","IndexedDbArchivist","AbstractArchivist","configSchemas","defaultDbName","defaultStoreName","_db","dbName","config","name","queries","ArchivistAllQuerySchema","ArchivistClearQuerySchema","ArchivistDeleteQuerySchema","ArchivistInsertQuerySchema","storeName","db","assertEx","allHandler","result","entries","map","_hash","payload","clearHandler","clear","deleteHandler","hashes","payloadPairs","Promise","all","get","PayloadHasher","hashAsync","foundHashesToDelete","hash","_","delMany","getHandler","getMany","filter","undefined","insertHandler","payloads","setMany","startHandler","createStore","creatableModule"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"IndexedDbArchivist.d.ts","sourceRoot":"","sources":["../../src/IndexedDbArchivist.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,MAAM,iCAAiC,CAAA;AACnE,OAAO,EAGL,eAAe,EAGf,wBAAwB,EACxB,eAAe,EAChB,MAAM,8BAA8B,CAAA;AAErC,OAAO,EAAE,eAAe,EAAmB,MAAM,qBAAqB,CAAA;AACtE,OAAO,EAAE,OAAO,EAAE,MAAM,4BAA4B,CAAA;AAGpD,MAAM,MAAM,8BAA8B,GAAG,wCAAwC,CAAA;AACrF,eAAO,MAAM,8BAA8B,EAAE,8BAAyE,CAAA;AAEtH,MAAM,MAAM,wBAAwB,GAAG,eAAe,CAAC;IACrD;;OAEG;IACH,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,MAAM,EAAE,8BAA8B,CAAA;IACtC;;OAEG;IACH,SAAS,CAAC,EAAE,MAAM,CAAA;CACnB,CAAC,CAAA;AAEF,MAAM,MAAM,wBAAwB,GAAG,eAAe,CAAC,eAAe,CAAC,wBAAwB,CAAC,CAAC,CAAA;AAEjG,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,aAAa,SAAc;IAClC,MAAM,CAAC,gBAAgB,SAAa;IAEpC,OAAO,CAAC,GAAG,CAAsB;IAEjC;;;;;;OAMG;IACH,IAAI,MAAM,WAET;IAED,IAAa,OAAO,aAEnB;IACD;;;;;;;;;OASG;IACH,IAAI,SAAS,WAEZ;IAED,OAAO,KAAK,EAAE,GAEb;cAEwB,UAAU,IAAI,OAAO,CAAC,OAAO,EAAE,CAAC;cAKhC,YAAY,IAAI,OAAO,CAAC,IAAI,CAAC;cAI7B,aAAa,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;cAWlD,UAAU,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,OAAO,EAAE,CAAC;cAIhD,aAAa,CAAC,QAAQ,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC,OAAO,EAAE,CAAC;cAYtD,YAAY;CAOtC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"IndexedDbArchivist.d.ts","sourceRoot":"","sources":["../../src/IndexedDbArchivist.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,MAAM,iCAAiC,CAAA;AACnE,OAAO,EAGL,eAAe,EAGf,wBAAwB,EACxB,eAAe,EAChB,MAAM,8BAA8B,CAAA;AAErC,OAAO,EAAE,eAAe,EAAmB,MAAM,qBAAqB,CAAA;AACtE,OAAO,EAAE,OAAO,EAAE,MAAM,4BAA4B,CAAA;AAGpD,MAAM,MAAM,8BAA8B,GAAG,wCAAwC,CAAA;AACrF,eAAO,MAAM,8BAA8B,EAAE,8BAAyE,CAAA;AAEtH,MAAM,MAAM,wBAAwB,GAAG,eAAe,CAAC;IACrD;;OAEG;IACH,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,MAAM,EAAE,8BAA8B,CAAA;IACtC;;OAEG;IACH,SAAS,CAAC,EAAE,MAAM,CAAA;CACnB,CAAC,CAAA;AAEF,MAAM,MAAM,wBAAwB,GAAG,eAAe,CAAC,eAAe,CAAC,wBAAwB,CAAC,CAAC,CAAA;AAEjG,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,aAAa,SAAc;IAClC,MAAM,CAAC,gBAAgB,SAAa;IAEpC,OAAO,CAAC,GAAG,CAAsB;IAEjC;;;;;;OAMG;IACH,IAAI,MAAM,WAET;IAED,IAAa,OAAO,aAEnB;IACD;;;;;;;;;OASG;IACH,IAAI,SAAS,WAEZ;IAED,OAAO,KAAK,EAAE,GAEb;cAEwB,UAAU,IAAI,OAAO,CAAC,OAAO,EAAE,CAAC;cAKhC,YAAY,IAAI,OAAO,CAAC,IAAI,CAAC;cAI7B,aAAa,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;cAWlD,UAAU,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,OAAO,EAAE,CAAC;cAIhD,aAAa,CAAC,QAAQ,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC,OAAO,EAAE,CAAC;cAYtD,YAAY;CAOtC"}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
+
|
|
4
|
+
// src/IndexedDbArchivist.ts
|
|
5
|
+
import { assertEx } from "@xylabs/assert";
|
|
6
|
+
import { AbstractArchivist } from "@xyo-network/archivist-abstract";
|
|
7
|
+
import { ArchivistAllQuerySchema, ArchivistClearQuerySchema, ArchivistDeleteQuerySchema, ArchivistInsertQuerySchema } from "@xyo-network/archivist-model";
|
|
8
|
+
import { PayloadHasher } from "@xyo-network/core";
|
|
9
|
+
import { creatableModule } from "@xyo-network/module";
|
|
10
|
+
import { clear, createStore, delMany, entries, getMany, setMany } from "idb-keyval";
|
|
11
|
+
function _ts_decorate(decorators, target, key, desc) {
|
|
12
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
13
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
|
|
14
|
+
r = Reflect.decorate(decorators, target, key, desc);
|
|
15
|
+
else
|
|
16
|
+
for (var i = decorators.length - 1; i >= 0; i--)
|
|
17
|
+
if (d = decorators[i])
|
|
18
|
+
r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
19
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
20
|
+
}
|
|
21
|
+
__name(_ts_decorate, "_ts_decorate");
|
|
22
|
+
var IndexedDbArchivistConfigSchema = "network.xyo.archivist.indexeddb.config";
|
|
23
|
+
var IndexedDbArchivist = class IndexedDbArchivist2 extends AbstractArchivist {
|
|
24
|
+
static {
|
|
25
|
+
__name(this, "IndexedDbArchivist");
|
|
26
|
+
}
|
|
27
|
+
static configSchemas = [
|
|
28
|
+
IndexedDbArchivistConfigSchema
|
|
29
|
+
];
|
|
30
|
+
static defaultDbName = "archivist";
|
|
31
|
+
static defaultStoreName = "payloads";
|
|
32
|
+
_db;
|
|
33
|
+
/**
|
|
34
|
+
* The database name. If not supplied via config, it defaults
|
|
35
|
+
* to the module name (not guaranteed to be unique) and if module
|
|
36
|
+
* name is not supplied, it defaults to `archivist`. This behavior
|
|
37
|
+
* biases towards a single, isolated DB per archivist which seems to
|
|
38
|
+
* make the most sense for 99% of use cases.
|
|
39
|
+
*/
|
|
40
|
+
get dbName() {
|
|
41
|
+
return this.config?.dbName ?? this.config?.name ?? IndexedDbArchivist2.defaultDbName;
|
|
42
|
+
}
|
|
43
|
+
get queries() {
|
|
44
|
+
return [
|
|
45
|
+
ArchivistAllQuerySchema,
|
|
46
|
+
ArchivistClearQuerySchema,
|
|
47
|
+
ArchivistDeleteQuerySchema,
|
|
48
|
+
ArchivistInsertQuerySchema,
|
|
49
|
+
...super.queries
|
|
50
|
+
];
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* The name of the object store. If not supplied via config, it defaults
|
|
54
|
+
* to `payloads`. The limitation of the current IndexedDB wrapper we're
|
|
55
|
+
* using is that it only supports a single object store per DB. See here:
|
|
56
|
+
* https://github.com/jakearchibald/idb-keyval/blob/main/custom-stores.md#defining-a-custom-database--store-name
|
|
57
|
+
* If this becomes a problem or we need migrations/transactions, we can
|
|
58
|
+
* move to this more-flexible library, which they recommend (and who
|
|
59
|
+
* recommends them for our simple use case of key-value storage):
|
|
60
|
+
* https://www.npmjs.com/package/idb
|
|
61
|
+
*/
|
|
62
|
+
get storeName() {
|
|
63
|
+
return this.config?.storeName ?? IndexedDbArchivist2.defaultStoreName;
|
|
64
|
+
}
|
|
65
|
+
get db() {
|
|
66
|
+
return assertEx(this._db, "DB not initialized");
|
|
67
|
+
}
|
|
68
|
+
async allHandler() {
|
|
69
|
+
const result = await entries(this.db);
|
|
70
|
+
return result.map(([_hash, payload]) => payload);
|
|
71
|
+
}
|
|
72
|
+
async clearHandler() {
|
|
73
|
+
await clear(this.db);
|
|
74
|
+
}
|
|
75
|
+
async deleteHandler(hashes) {
|
|
76
|
+
const payloadPairs = await Promise.all((await this.get(hashes)).map(async (payload) => [
|
|
77
|
+
await PayloadHasher.hashAsync(payload),
|
|
78
|
+
payload
|
|
79
|
+
]));
|
|
80
|
+
const foundHashesToDelete = payloadPairs.map(([hash, _]) => hash);
|
|
81
|
+
await delMany(foundHashesToDelete, this.db);
|
|
82
|
+
return foundHashesToDelete;
|
|
83
|
+
}
|
|
84
|
+
async getHandler(hashes) {
|
|
85
|
+
return (await getMany(hashes, this.db)).filter((result) => result !== void 0);
|
|
86
|
+
}
|
|
87
|
+
async insertHandler(payloads) {
|
|
88
|
+
const entries2 = await Promise.all(payloads.map(async (payload) => {
|
|
89
|
+
const hash = await PayloadHasher.hashAsync(payload);
|
|
90
|
+
return [
|
|
91
|
+
hash,
|
|
92
|
+
payload
|
|
93
|
+
];
|
|
94
|
+
}));
|
|
95
|
+
await setMany(entries2, this.db);
|
|
96
|
+
return payloads;
|
|
97
|
+
}
|
|
98
|
+
async startHandler() {
|
|
99
|
+
await super.startHandler();
|
|
100
|
+
this._db = createStore(this.dbName, this.storeName);
|
|
101
|
+
return true;
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
IndexedDbArchivist = _ts_decorate([
|
|
105
|
+
creatableModule()
|
|
106
|
+
], IndexedDbArchivist);
|
|
107
|
+
export {
|
|
108
|
+
IndexedDbArchivist,
|
|
109
|
+
IndexedDbArchivistConfigSchema
|
|
110
|
+
};
|
|
111
|
+
//# sourceMappingURL=IndexedDbArchivist.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/IndexedDbArchivist.ts"],"sourcesContent":["import { assertEx } from '@xylabs/assert'\nimport { AbstractArchivist } from '@xyo-network/archivist-abstract'\nimport {\n ArchivistAllQuerySchema,\n ArchivistClearQuerySchema,\n ArchivistConfig,\n ArchivistDeleteQuerySchema,\n ArchivistInsertQuerySchema,\n ArchivistModuleEventData,\n ArchivistParams,\n} from '@xyo-network/archivist-model'\nimport { PayloadHasher } from '@xyo-network/core'\nimport { AnyConfigSchema, creatableModule } from '@xyo-network/module'\nimport { Payload } from '@xyo-network/payload-model'\nimport { clear, createStore, delMany, entries, getMany, setMany, UseStore } from 'idb-keyval'\n\nexport type IndexedDbArchivistConfigSchema = 'network.xyo.archivist.indexeddb.config'\nexport const IndexedDbArchivistConfigSchema: IndexedDbArchivistConfigSchema = 'network.xyo.archivist.indexeddb.config'\n\nexport type IndexedDbArchivistConfig = ArchivistConfig<{\n /**\n * The database name\n */\n dbName?: string\n schema: IndexedDbArchivistConfigSchema\n /**\n * The name of the object store\n */\n storeName?: string\n}>\n\nexport type IndexedDbArchivistParams = ArchivistParams<AnyConfigSchema<IndexedDbArchivistConfig>>\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 defaultDbName = 'archivist'\n static defaultStoreName = 'payloads'\n\n private _db: UseStore | 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 override get queries() {\n return [ArchivistAllQuerySchema, ArchivistClearQuerySchema, ArchivistDeleteQuerySchema, ArchivistInsertQuerySchema, ...super.queries]\n }\n /**\n * The name of the object store. If not supplied via config, it defaults\n * to `payloads`. The limitation of the current IndexedDB wrapper we're\n * using is that it only supports a single object store per DB. See here:\n * https://github.com/jakearchibald/idb-keyval/blob/main/custom-stores.md#defining-a-custom-database--store-name\n * If this becomes a problem or we need migrations/transactions, we can\n * move to this more-flexible library, which they recommend (and who\n * recommends them for our simple use case of key-value storage):\n * https://www.npmjs.com/package/idb\n */\n get storeName() {\n return this.config?.storeName ?? IndexedDbArchivist.defaultStoreName\n }\n\n private get db(): UseStore {\n return assertEx(this._db, 'DB not initialized')\n }\n\n protected override async allHandler(): Promise<Payload[]> {\n const result = await entries<string, Payload>(this.db)\n return result.map<Payload>(([_hash, payload]) => payload)\n }\n\n protected override async clearHandler(): Promise<void> {\n await clear(this.db)\n }\n\n protected override async deleteHandler(hashes: string[]): Promise<string[]> {\n const payloadPairs: [string, Payload][] = await Promise.all(\n (await this.get(hashes)).map<Promise<[string, Payload]>>(async (payload) => [await PayloadHasher.hashAsync(payload), payload]),\n )\n\n const foundHashesToDelete = payloadPairs.map(([hash, _]) => hash)\n await delMany(foundHashesToDelete, this.db)\n\n return foundHashesToDelete\n }\n\n protected override async getHandler(hashes: string[]): Promise<Payload[]> {\n return (await getMany<Payload>(hashes, this.db)).filter((result) => result !== undefined)\n }\n\n protected override async insertHandler(payloads: Payload[]): Promise<Payload[]> {\n const entries = await Promise.all(\n payloads.map<Promise<[string, Payload]>>(async (payload) => {\n const hash = await PayloadHasher.hashAsync(payload)\n return [hash, payload]\n }),\n )\n await setMany(entries, this.db)\n\n return payloads\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 this._db = createStore(this.dbName, this.storeName)\n return true\n }\n}\n"],"mappings":";;;;AAAA,SAASA,gBAAgB;AACzB,SAASC,yBAAyB;AAClC,SACEC,yBACAC,2BAEAC,4BACAC,kCAGK;AACP,SAASC,qBAAqB;AAC9B,SAA0BC,uBAAuB;AAEjD,SAASC,OAAOC,aAAaC,SAASC,SAASC,SAASC,eAAyB;;;;;;;;;;;;AAG1E,IAAMC,iCAAiE;AAiB9E,IAAaC,qBAAN,MAAAA,4BAGGC,kBAAAA;SAAAA;;;EACR,OAAgBC,gBAAgB;IAACH;;EACjC,OAAOI,gBAAgB;EACvB,OAAOC,mBAAmB;EAElBC;;;;;;;;EASR,IAAIC,SAAS;AACX,WAAO,KAAKC,QAAQD,UAAU,KAAKC,QAAQC,QAAQR,oBAAmBG;EACxE;EAEA,IAAaM,UAAU;AACrB,WAAO;MAACC;MAAyBC;MAA2BC;MAA4BC;SAA+B,MAAMJ;;EAC/H;;;;;;;;;;;EAWA,IAAIK,YAAY;AACd,WAAO,KAAKP,QAAQO,aAAad,oBAAmBI;EACtD;EAEA,IAAYW,KAAe;AACzB,WAAOC,SAAS,KAAKX,KAAK,oBAAA;EAC5B;EAEA,MAAyBY,aAAiC;AACxD,UAAMC,SAAS,MAAMC,QAAyB,KAAKJ,EAAE;AACrD,WAAOG,OAAOE,IAAa,CAAC,CAACC,OAAOC,OAAAA,MAAaA,OAAAA;EACnD;EAEA,MAAyBC,eAA8B;AACrD,UAAMC,MAAM,KAAKT,EAAE;EACrB;EAEA,MAAyBU,cAAcC,QAAqC;AAC1E,UAAMC,eAAoC,MAAMC,QAAQC,KACrD,MAAM,KAAKC,IAAIJ,MAAAA,GAASN,IAAgC,OAAOE,YAAY;MAAC,MAAMS,cAAcC,UAAUV,OAAAA;MAAUA;KAAQ,CAAA;AAG/H,UAAMW,sBAAsBN,aAAaP,IAAI,CAAC,CAACc,MAAMC,CAAAA,MAAOD,IAAAA;AAC5D,UAAME,QAAQH,qBAAqB,KAAKlB,EAAE;AAE1C,WAAOkB;EACT;EAEA,MAAyBI,WAAWX,QAAsC;AACxE,YAAQ,MAAMY,QAAiBZ,QAAQ,KAAKX,EAAE,GAAGwB,OAAO,CAACrB,WAAWA,WAAWsB,MAAAA;EACjF;EAEA,MAAyBC,cAAcC,UAAyC;AAC9E,UAAMvB,WAAU,MAAMS,QAAQC,IAC5Ba,SAAStB,IAAgC,OAAOE,YAAAA;AAC9C,YAAMY,OAAO,MAAMH,cAAcC,UAAUV,OAAAA;AAC3C,aAAO;QAACY;QAAMZ;;IAChB,CAAA,CAAA;AAEF,UAAMqB,QAAQxB,UAAS,KAAKJ,EAAE;AAE9B,WAAO2B;EACT;EAEA,MAAyBE,eAAe;AACtC,UAAM,MAAMA,aAAAA;AAGZ,SAAKvC,MAAMwC,YAAY,KAAKvC,QAAQ,KAAKQ,SAAS;AAClD,WAAO;EACT;AACF;AArFad,qBAAAA,aAAAA;EADZ8C,gBAAAA;GACY9C,kBAAAA;","names":["assertEx","AbstractArchivist","ArchivistAllQuerySchema","ArchivistClearQuerySchema","ArchivistDeleteQuerySchema","ArchivistInsertQuerySchema","PayloadHasher","creatableModule","clear","createStore","delMany","entries","getMany","setMany","IndexedDbArchivistConfigSchema","IndexedDbArchivist","AbstractArchivist","configSchemas","defaultDbName","defaultStoreName","_db","dbName","config","name","queries","ArchivistAllQuerySchema","ArchivistClearQuerySchema","ArchivistDeleteQuerySchema","ArchivistInsertQuerySchema","storeName","db","assertEx","allHandler","result","entries","map","_hash","payload","clearHandler","clear","deleteHandler","hashes","payloadPairs","Promise","all","get","PayloadHasher","hashAsync","foundHashesToDelete","hash","_","delMany","getHandler","getMany","filter","undefined","insertHandler","payloads","setMany","startHandler","createStore","creatableModule"]}
|
|
@@ -129,9 +129,4 @@ var IndexedDbArchivist = class IndexedDbArchivist2 extends import_archivist_abst
|
|
|
129
129
|
IndexedDbArchivist = _ts_decorate([
|
|
130
130
|
(0, import_module.creatableModule)()
|
|
131
131
|
], IndexedDbArchivist);
|
|
132
|
-
|
|
133
|
-
0 && (module.exports = {
|
|
134
|
-
IndexedDbArchivist,
|
|
135
|
-
IndexedDbArchivistConfigSchema
|
|
136
|
-
});
|
|
137
|
-
//# sourceMappingURL=index.js.map
|
|
132
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts","../../src/IndexedDbArchivist.ts"],"sourcesContent":["export * from './IndexedDbArchivist'\n","import { assertEx } from '@xylabs/assert'\nimport { AbstractArchivist } from '@xyo-network/archivist-abstract'\nimport {\n ArchivistAllQuerySchema,\n ArchivistClearQuerySchema,\n ArchivistConfig,\n ArchivistDeleteQuerySchema,\n ArchivistInsertQuerySchema,\n ArchivistModuleEventData,\n ArchivistParams,\n} from '@xyo-network/archivist-model'\nimport { PayloadHasher } from '@xyo-network/core'\nimport { AnyConfigSchema, creatableModule } from '@xyo-network/module'\nimport { Payload } from '@xyo-network/payload-model'\nimport { clear, createStore, delMany, entries, getMany, setMany, UseStore } from 'idb-keyval'\n\nexport type IndexedDbArchivistConfigSchema = 'network.xyo.archivist.indexeddb.config'\nexport const IndexedDbArchivistConfigSchema: IndexedDbArchivistConfigSchema = 'network.xyo.archivist.indexeddb.config'\n\nexport type IndexedDbArchivistConfig = ArchivistConfig<{\n /**\n * The database name\n */\n dbName?: string\n schema: IndexedDbArchivistConfigSchema\n /**\n * The name of the object store\n */\n storeName?: string\n}>\n\nexport type IndexedDbArchivistParams = ArchivistParams<AnyConfigSchema<IndexedDbArchivistConfig>>\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 defaultDbName = 'archivist'\n static defaultStoreName = 'payloads'\n\n private _db: UseStore | 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 override get queries() {\n return [ArchivistAllQuerySchema, ArchivistClearQuerySchema, ArchivistDeleteQuerySchema, ArchivistInsertQuerySchema, ...super.queries]\n }\n /**\n * The name of the object store. If not supplied via config, it defaults\n * to `payloads`. The limitation of the current IndexedDB wrapper we're\n * using is that it only supports a single object store per DB. See here:\n * https://github.com/jakearchibald/idb-keyval/blob/main/custom-stores.md#defining-a-custom-database--store-name\n * If this becomes a problem or we need migrations/transactions, we can\n * move to this more-flexible library, which they recommend (and who\n * recommends them for our simple use case of key-value storage):\n * https://www.npmjs.com/package/idb\n */\n get storeName() {\n return this.config?.storeName ?? IndexedDbArchivist.defaultStoreName\n }\n\n private get db(): UseStore {\n return assertEx(this._db, 'DB not initialized')\n }\n\n protected override async allHandler(): Promise<Payload[]> {\n const result = await entries<string, Payload>(this.db)\n return result.map<Payload>(([_hash, payload]) => payload)\n }\n\n protected override async clearHandler(): Promise<void> {\n await clear(this.db)\n }\n\n protected override async deleteHandler(hashes: string[]): Promise<string[]> {\n const payloadPairs: [string, Payload][] = await Promise.all(\n (await this.get(hashes)).map<Promise<[string, Payload]>>(async (payload) => [await PayloadHasher.hashAsync(payload), payload]),\n )\n\n const foundHashesToDelete = payloadPairs.map(([hash, _]) => hash)\n await delMany(foundHashesToDelete, this.db)\n\n return foundHashesToDelete\n }\n\n protected override async getHandler(hashes: string[]): Promise<Payload[]> {\n return (await getMany<Payload>(hashes, this.db)).filter((result) => result !== undefined)\n }\n\n protected override async insertHandler(payloads: Payload[]): Promise<Payload[]> {\n const entries = await Promise.all(\n payloads.map<Promise<[string, Payload]>>(async (payload) => {\n const hash = await PayloadHasher.hashAsync(payload)\n return [hash, payload]\n }),\n )\n await setMany(entries, this.db)\n\n return payloads\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 this._db = createStore(this.dbName, this.storeName)\n return true\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;ACAA,oBAAyB;AACzB,gCAAkC;AAClC,6BAQO;AACP,kBAA8B;AAC9B,oBAAiD;AAEjD,wBAAiF;;;;;;;;;;;;AAG1E,IAAMA,iCAAiE;AAiB9E,IAAaC,qBAAN,MAAAA,4BAGGC,4CAAAA;SAAAA;;;EACR,OAAgBC,gBAAgB;IAACH;;EACjC,OAAOI,gBAAgB;EACvB,OAAOC,mBAAmB;EAElBC;;;;;;;;EASR,IAAIC,SAAS;AACX,WAAO,KAAKC,QAAQD,UAAU,KAAKC,QAAQC,QAAQR,oBAAmBG;EACxE;EAEA,IAAaM,UAAU;AACrB,WAAO;MAACC;MAAyBC;MAA2BC;MAA4BC;SAA+B,MAAMJ;;EAC/H;;;;;;;;;;;EAWA,IAAIK,YAAY;AACd,WAAO,KAAKP,QAAQO,aAAad,oBAAmBI;EACtD;EAEA,IAAYW,KAAe;AACzB,eAAOC,wBAAS,KAAKX,KAAK,oBAAA;EAC5B;EAEA,MAAyBY,aAAiC;AACxD,UAAMC,SAAS,UAAMC,2BAAyB,KAAKJ,EAAE;AACrD,WAAOG,OAAOE,IAAa,CAAC,CAACC,OAAOC,OAAAA,MAAaA,OAAAA;EACnD;EAEA,MAAyBC,eAA8B;AACrD,cAAMC,yBAAM,KAAKT,EAAE;EACrB;EAEA,MAAyBU,cAAcC,QAAqC;AAC1E,UAAMC,eAAoC,MAAMC,QAAQC,KACrD,MAAM,KAAKC,IAAIJ,MAAAA,GAASN,IAAgC,OAAOE,YAAY;MAAC,MAAMS,0BAAcC,UAAUV,OAAAA;MAAUA;KAAQ,CAAA;AAG/H,UAAMW,sBAAsBN,aAAaP,IAAI,CAAC,CAACc,MAAMC,CAAAA,MAAOD,IAAAA;AAC5D,cAAME,2BAAQH,qBAAqB,KAAKlB,EAAE;AAE1C,WAAOkB;EACT;EAEA,MAAyBI,WAAWX,QAAsC;AACxE,YAAQ,UAAMY,2BAAiBZ,QAAQ,KAAKX,EAAE,GAAGwB,OAAO,CAACrB,WAAWA,WAAWsB,MAAAA;EACjF;EAEA,MAAyBC,cAAcC,UAAyC;AAC9E,UAAMvB,WAAU,MAAMS,QAAQC,IAC5Ba,SAAStB,IAAgC,OAAOE,YAAAA;AAC9C,YAAMY,OAAO,MAAMH,0BAAcC,UAAUV,OAAAA;AAC3C,aAAO;QAACY;QAAMZ;;IAChB,CAAA,CAAA;AAEF,cAAMqB,2BAAQxB,UAAS,KAAKJ,EAAE;AAE9B,WAAO2B;EACT;EAEA,MAAyBE,eAAe;AACtC,UAAM,MAAMA,aAAAA;AAGZ,SAAKvC,UAAMwC,+BAAY,KAAKvC,QAAQ,KAAKQ,SAAS;AAClD,WAAO;EACT;AACF;AArFad,qBAAAA,aAAAA;MADZ8C,+BAAAA;GACY9C,kBAAAA;","names":["IndexedDbArchivistConfigSchema","IndexedDbArchivist","AbstractArchivist","configSchemas","defaultDbName","defaultStoreName","_db","dbName","config","name","queries","ArchivistAllQuerySchema","ArchivistClearQuerySchema","ArchivistDeleteQuerySchema","ArchivistInsertQuerySchema","storeName","db","assertEx","allHandler","result","entries","map","_hash","payload","clearHandler","clear","deleteHandler","hashes","payloadPairs","Promise","all","get","PayloadHasher","hashAsync","foundHashesToDelete","hash","_","delMany","getHandler","getMany","filter","undefined","insertHandler","payloads","setMany","startHandler","createStore","creatableModule"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,sBAAsB,CAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,sBAAsB,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/IndexedDbArchivist.ts"],"sourcesContent":["import { assertEx } from '@xylabs/assert'\nimport { AbstractArchivist } from '@xyo-network/archivist-abstract'\nimport {\n ArchivistAllQuerySchema,\n ArchivistClearQuerySchema,\n ArchivistConfig,\n ArchivistDeleteQuerySchema,\n ArchivistInsertQuerySchema,\n ArchivistModuleEventData,\n ArchivistParams,\n} from '@xyo-network/archivist-model'\nimport { PayloadHasher } from '@xyo-network/core'\nimport { AnyConfigSchema, creatableModule } from '@xyo-network/module'\nimport { Payload } from '@xyo-network/payload-model'\nimport { clear, createStore, delMany, entries, getMany, setMany, UseStore } from 'idb-keyval'\n\nexport type IndexedDbArchivistConfigSchema = 'network.xyo.archivist.indexeddb.config'\nexport const IndexedDbArchivistConfigSchema: IndexedDbArchivistConfigSchema = 'network.xyo.archivist.indexeddb.config'\n\nexport type IndexedDbArchivistConfig = ArchivistConfig<{\n /**\n * The database name\n */\n dbName?: string\n schema: IndexedDbArchivistConfigSchema\n /**\n * The name of the object store\n */\n storeName?: string\n}>\n\nexport type IndexedDbArchivistParams = ArchivistParams<AnyConfigSchema<IndexedDbArchivistConfig>>\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 defaultDbName = 'archivist'\n static defaultStoreName = 'payloads'\n\n private _db: UseStore | 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 override get queries() {\n return [ArchivistAllQuerySchema, ArchivistClearQuerySchema, ArchivistDeleteQuerySchema, ArchivistInsertQuerySchema, ...super.queries]\n }\n /**\n * The name of the object store. If not supplied via config, it defaults\n * to `payloads`. The limitation of the current IndexedDB wrapper we're\n * using is that it only supports a single object store per DB. See here:\n * https://github.com/jakearchibald/idb-keyval/blob/main/custom-stores.md#defining-a-custom-database--store-name\n * If this becomes a problem or we need migrations/transactions, we can\n * move to this more-flexible library, which they recommend (and who\n * recommends them for our simple use case of key-value storage):\n * https://www.npmjs.com/package/idb\n */\n get storeName() {\n return this.config?.storeName ?? IndexedDbArchivist.defaultStoreName\n }\n\n private get db(): UseStore {\n return assertEx(this._db, 'DB not initialized')\n }\n\n protected override async allHandler(): Promise<Payload[]> {\n const result = await entries<string, Payload>(this.db)\n return result.map<Payload>(([_hash, payload]) => payload)\n }\n\n protected override async clearHandler(): Promise<void> {\n await clear(this.db)\n }\n\n protected override async deleteHandler(hashes: string[]): Promise<string[]> {\n const payloadPairs: [string, Payload][] = await Promise.all(\n (await this.get(hashes)).map<Promise<[string, Payload]>>(async (payload) => [await PayloadHasher.hashAsync(payload), payload]),\n )\n\n const foundHashesToDelete = payloadPairs.map(([hash, _]) => hash)\n await delMany(foundHashesToDelete, this.db)\n\n return foundHashesToDelete\n }\n\n protected override async getHandler(hashes: string[]): Promise<Payload[]> {\n return (await getMany<Payload>(hashes, this.db)).filter((result) => result !== undefined)\n }\n\n protected override async insertHandler(payloads: Payload[]): Promise<Payload[]> {\n const entries = await Promise.all(\n payloads.map<Promise<[string, Payload]>>(async (payload) => {\n const hash = await PayloadHasher.hashAsync(payload)\n return [hash, payload]\n }),\n )\n await setMany(entries, this.db)\n\n return payloads\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 this._db = createStore(this.dbName, this.storeName)\n return true\n }\n}\n"],"mappings":";;;;AAAA,SAASA,gBAAgB;AACzB,SAASC,yBAAyB;AAClC,SACEC,yBACAC,2BAEAC,4BACAC,kCAGK;AACP,SAASC,qBAAqB;AAC9B,SAA0BC,uBAAuB;AAEjD,SAASC,OAAOC,aAAaC,SAASC,SAASC,SAASC,eAAyB;;;;;;;;;;;;AAG1E,IAAMC,iCAAiE;AAiB9E,IAAaC,qBAAN,MAAAA,4BAGGC,kBAAAA;SAAAA;;;EACR,OAAgBC,gBAAgB;IAACH;;EACjC,OAAOI,gBAAgB;EACvB,OAAOC,mBAAmB;EAElBC;;;;;;;;EASR,IAAIC,SAAS;AACX,WAAO,KAAKC,QAAQD,UAAU,KAAKC,QAAQC,QAAQR,oBAAmBG;EACxE;EAEA,IAAaM,UAAU;AACrB,WAAO;MAACC;MAAyBC;MAA2BC;MAA4BC;SAA+B,MAAMJ;;EAC/H;;;;;;;;;;;EAWA,IAAIK,YAAY;AACd,WAAO,KAAKP,QAAQO,aAAad,oBAAmBI;EACtD;EAEA,IAAYW,KAAe;AACzB,WAAOC,SAAS,KAAKX,KAAK,oBAAA;EAC5B;EAEA,MAAyBY,aAAiC;AACxD,UAAMC,SAAS,MAAMC,QAAyB,KAAKJ,EAAE;AACrD,WAAOG,OAAOE,IAAa,CAAC,CAACC,OAAOC,OAAAA,MAAaA,OAAAA;EACnD;EAEA,MAAyBC,eAA8B;AACrD,UAAMC,MAAM,KAAKT,EAAE;EACrB;EAEA,MAAyBU,cAAcC,QAAqC;AAC1E,UAAMC,eAAoC,MAAMC,QAAQC,KACrD,MAAM,KAAKC,IAAIJ,MAAAA,GAASN,IAAgC,OAAOE,YAAY;MAAC,MAAMS,cAAcC,UAAUV,OAAAA;MAAUA;KAAQ,CAAA;AAG/H,UAAMW,sBAAsBN,aAAaP,IAAI,CAAC,CAACc,MAAMC,CAAAA,MAAOD,IAAAA;AAC5D,UAAME,QAAQH,qBAAqB,KAAKlB,EAAE;AAE1C,WAAOkB;EACT;EAEA,MAAyBI,WAAWX,QAAsC;AACxE,YAAQ,MAAMY,QAAiBZ,QAAQ,KAAKX,EAAE,GAAGwB,OAAO,CAACrB,WAAWA,WAAWsB,MAAAA;EACjF;EAEA,MAAyBC,cAAcC,UAAyC;AAC9E,UAAMvB,WAAU,MAAMS,QAAQC,IAC5Ba,SAAStB,IAAgC,OAAOE,YAAAA;AAC9C,YAAMY,OAAO,MAAMH,cAAcC,UAAUV,OAAAA;AAC3C,aAAO;QAACY;QAAMZ;;IAChB,CAAA,CAAA;AAEF,UAAMqB,QAAQxB,UAAS,KAAKJ,EAAE;AAE9B,WAAO2B;EACT;EAEA,MAAyBE,eAAe;AACtC,UAAM,MAAMA,aAAAA;AAGZ,SAAKvC,MAAMwC,YAAY,KAAKvC,QAAQ,KAAKQ,SAAS;AAClD,WAAO;EACT;AACF;AArFad,qBAAAA,aAAAA;EADZ8C,gBAAAA;GACY9C,kBAAAA;","names":["assertEx","AbstractArchivist","ArchivistAllQuerySchema","ArchivistClearQuerySchema","ArchivistDeleteQuerySchema","ArchivistInsertQuerySchema","PayloadHasher","creatableModule","clear","createStore","delMany","entries","getMany","setMany","IndexedDbArchivistConfigSchema","IndexedDbArchivist","AbstractArchivist","configSchemas","defaultDbName","defaultStoreName","_db","dbName","config","name","queries","ArchivistAllQuerySchema","ArchivistClearQuerySchema","ArchivistDeleteQuerySchema","ArchivistInsertQuerySchema","storeName","db","assertEx","allHandler","result","entries","map","_hash","payload","clearHandler","clear","deleteHandler","hashes","payloadPairs","Promise","all","get","PayloadHasher","hashAsync","foundHashesToDelete","hash","_","delMany","getHandler","getMany","filter","undefined","insertHandler","payloads","setMany","startHandler","createStore","creatableModule"]}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { AbstractArchivist } from '@xyo-network/archivist-abstract';
|
|
2
|
+
import { ArchivistConfig, ArchivistModuleEventData, ArchivistParams } from '@xyo-network/archivist-model';
|
|
3
|
+
import { AnyConfigSchema } from '@xyo-network/module';
|
|
4
|
+
import { Payload } from '@xyo-network/payload-model';
|
|
5
|
+
export type IndexedDbArchivistConfigSchema = 'network.xyo.archivist.indexeddb.config';
|
|
6
|
+
export declare const IndexedDbArchivistConfigSchema: IndexedDbArchivistConfigSchema;
|
|
7
|
+
export type IndexedDbArchivistConfig = ArchivistConfig<{
|
|
8
|
+
/**
|
|
9
|
+
* The database name
|
|
10
|
+
*/
|
|
11
|
+
dbName?: string;
|
|
12
|
+
schema: IndexedDbArchivistConfigSchema;
|
|
13
|
+
/**
|
|
14
|
+
* The name of the object store
|
|
15
|
+
*/
|
|
16
|
+
storeName?: string;
|
|
17
|
+
}>;
|
|
18
|
+
export type IndexedDbArchivistParams = ArchivistParams<AnyConfigSchema<IndexedDbArchivistConfig>>;
|
|
19
|
+
export declare class IndexedDbArchivist<TParams extends IndexedDbArchivistParams = IndexedDbArchivistParams, TEventData extends ArchivistModuleEventData = ArchivistModuleEventData> extends AbstractArchivist<TParams, TEventData> {
|
|
20
|
+
static configSchemas: "network.xyo.archivist.indexeddb.config"[];
|
|
21
|
+
static defaultDbName: string;
|
|
22
|
+
static defaultStoreName: string;
|
|
23
|
+
private _db;
|
|
24
|
+
/**
|
|
25
|
+
* The database name. If not supplied via config, it defaults
|
|
26
|
+
* to the module name (not guaranteed to be unique) and if module
|
|
27
|
+
* name is not supplied, it defaults to `archivist`. This behavior
|
|
28
|
+
* biases towards a single, isolated DB per archivist which seems to
|
|
29
|
+
* make the most sense for 99% of use cases.
|
|
30
|
+
*/
|
|
31
|
+
get dbName(): string;
|
|
32
|
+
get queries(): string[];
|
|
33
|
+
/**
|
|
34
|
+
* The name of the object store. If not supplied via config, it defaults
|
|
35
|
+
* to `payloads`. The limitation of the current IndexedDB wrapper we're
|
|
36
|
+
* using is that it only supports a single object store per DB. See here:
|
|
37
|
+
* https://github.com/jakearchibald/idb-keyval/blob/main/custom-stores.md#defining-a-custom-database--store-name
|
|
38
|
+
* If this becomes a problem or we need migrations/transactions, we can
|
|
39
|
+
* move to this more-flexible library, which they recommend (and who
|
|
40
|
+
* recommends them for our simple use case of key-value storage):
|
|
41
|
+
* https://www.npmjs.com/package/idb
|
|
42
|
+
*/
|
|
43
|
+
get storeName(): string;
|
|
44
|
+
private get db();
|
|
45
|
+
protected allHandler(): Promise<Payload[]>;
|
|
46
|
+
protected clearHandler(): Promise<void>;
|
|
47
|
+
protected deleteHandler(hashes: string[]): Promise<string[]>;
|
|
48
|
+
protected getHandler(hashes: string[]): Promise<Payload[]>;
|
|
49
|
+
protected insertHandler(payloads: Payload[]): Promise<Payload[]>;
|
|
50
|
+
protected startHandler(): Promise<boolean>;
|
|
51
|
+
}
|
|
52
|
+
//# sourceMappingURL=IndexedDbArchivist.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"IndexedDbArchivist.d.ts","sourceRoot":"","sources":["../../src/IndexedDbArchivist.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,MAAM,iCAAiC,CAAA;AACnE,OAAO,EAGL,eAAe,EAGf,wBAAwB,EACxB,eAAe,EAChB,MAAM,8BAA8B,CAAA;AAErC,OAAO,EAAE,eAAe,EAAmB,MAAM,qBAAqB,CAAA;AACtE,OAAO,EAAE,OAAO,EAAE,MAAM,4BAA4B,CAAA;AAGpD,MAAM,MAAM,8BAA8B,GAAG,wCAAwC,CAAA;AACrF,eAAO,MAAM,8BAA8B,EAAE,8BAAyE,CAAA;AAEtH,MAAM,MAAM,wBAAwB,GAAG,eAAe,CAAC;IACrD;;OAEG;IACH,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,MAAM,EAAE,8BAA8B,CAAA;IACtC;;OAEG;IACH,SAAS,CAAC,EAAE,MAAM,CAAA;CACnB,CAAC,CAAA;AAEF,MAAM,MAAM,wBAAwB,GAAG,eAAe,CAAC,eAAe,CAAC,wBAAwB,CAAC,CAAC,CAAA;AAEjG,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,aAAa,SAAc;IAClC,MAAM,CAAC,gBAAgB,SAAa;IAEpC,OAAO,CAAC,GAAG,CAAsB;IAEjC;;;;;;OAMG;IACH,IAAI,MAAM,WAET;IAED,IAAa,OAAO,aAEnB;IACD;;;;;;;;;OASG;IACH,IAAI,SAAS,WAEZ;IAED,OAAO,KAAK,EAAE,GAEb;cAEwB,UAAU,IAAI,OAAO,CAAC,OAAO,EAAE,CAAC;cAKhC,YAAY,IAAI,OAAO,CAAC,IAAI,CAAC;cAI7B,aAAa,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;cAWlD,UAAU,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,OAAO,EAAE,CAAC;cAIhD,aAAa,CAAC,QAAQ,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC,OAAO,EAAE,CAAC;cAYtD,YAAY;CAOtC"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { AbstractArchivist } from '@xyo-network/archivist-abstract';
|
|
2
|
+
import { ArchivistConfig, ArchivistModuleEventData, ArchivistParams } from '@xyo-network/archivist-model';
|
|
3
|
+
import { AnyConfigSchema } from '@xyo-network/module';
|
|
4
|
+
import { Payload } from '@xyo-network/payload-model';
|
|
5
|
+
export type IndexedDbArchivistConfigSchema = 'network.xyo.archivist.indexeddb.config';
|
|
6
|
+
export declare const IndexedDbArchivistConfigSchema: IndexedDbArchivistConfigSchema;
|
|
7
|
+
export type IndexedDbArchivistConfig = ArchivistConfig<{
|
|
8
|
+
/**
|
|
9
|
+
* The database name
|
|
10
|
+
*/
|
|
11
|
+
dbName?: string;
|
|
12
|
+
schema: IndexedDbArchivistConfigSchema;
|
|
13
|
+
/**
|
|
14
|
+
* The name of the object store
|
|
15
|
+
*/
|
|
16
|
+
storeName?: string;
|
|
17
|
+
}>;
|
|
18
|
+
export type IndexedDbArchivistParams = ArchivistParams<AnyConfigSchema<IndexedDbArchivistConfig>>;
|
|
19
|
+
export declare class IndexedDbArchivist<TParams extends IndexedDbArchivistParams = IndexedDbArchivistParams, TEventData extends ArchivistModuleEventData = ArchivistModuleEventData> extends AbstractArchivist<TParams, TEventData> {
|
|
20
|
+
static configSchemas: "network.xyo.archivist.indexeddb.config"[];
|
|
21
|
+
static defaultDbName: string;
|
|
22
|
+
static defaultStoreName: string;
|
|
23
|
+
private _db;
|
|
24
|
+
/**
|
|
25
|
+
* The database name. If not supplied via config, it defaults
|
|
26
|
+
* to the module name (not guaranteed to be unique) and if module
|
|
27
|
+
* name is not supplied, it defaults to `archivist`. This behavior
|
|
28
|
+
* biases towards a single, isolated DB per archivist which seems to
|
|
29
|
+
* make the most sense for 99% of use cases.
|
|
30
|
+
*/
|
|
31
|
+
get dbName(): string;
|
|
32
|
+
get queries(): string[];
|
|
33
|
+
/**
|
|
34
|
+
* The name of the object store. If not supplied via config, it defaults
|
|
35
|
+
* to `payloads`. The limitation of the current IndexedDB wrapper we're
|
|
36
|
+
* using is that it only supports a single object store per DB. See here:
|
|
37
|
+
* https://github.com/jakearchibald/idb-keyval/blob/main/custom-stores.md#defining-a-custom-database--store-name
|
|
38
|
+
* If this becomes a problem or we need migrations/transactions, we can
|
|
39
|
+
* move to this more-flexible library, which they recommend (and who
|
|
40
|
+
* recommends them for our simple use case of key-value storage):
|
|
41
|
+
* https://www.npmjs.com/package/idb
|
|
42
|
+
*/
|
|
43
|
+
get storeName(): string;
|
|
44
|
+
private get db();
|
|
45
|
+
protected allHandler(): Promise<Payload[]>;
|
|
46
|
+
protected clearHandler(): Promise<void>;
|
|
47
|
+
protected deleteHandler(hashes: string[]): Promise<string[]>;
|
|
48
|
+
protected getHandler(hashes: string[]): Promise<Payload[]>;
|
|
49
|
+
protected insertHandler(payloads: Payload[]): Promise<Payload[]>;
|
|
50
|
+
protected startHandler(): Promise<boolean>;
|
|
51
|
+
}
|
|
52
|
+
//# sourceMappingURL=IndexedDbArchivist.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"IndexedDbArchivist.d.ts","sourceRoot":"","sources":["../../src/IndexedDbArchivist.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,MAAM,iCAAiC,CAAA;AACnE,OAAO,EAGL,eAAe,EAGf,wBAAwB,EACxB,eAAe,EAChB,MAAM,8BAA8B,CAAA;AAErC,OAAO,EAAE,eAAe,EAAmB,MAAM,qBAAqB,CAAA;AACtE,OAAO,EAAE,OAAO,EAAE,MAAM,4BAA4B,CAAA;AAGpD,MAAM,MAAM,8BAA8B,GAAG,wCAAwC,CAAA;AACrF,eAAO,MAAM,8BAA8B,EAAE,8BAAyE,CAAA;AAEtH,MAAM,MAAM,wBAAwB,GAAG,eAAe,CAAC;IACrD;;OAEG;IACH,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,MAAM,EAAE,8BAA8B,CAAA;IACtC;;OAEG;IACH,SAAS,CAAC,EAAE,MAAM,CAAA;CACnB,CAAC,CAAA;AAEF,MAAM,MAAM,wBAAwB,GAAG,eAAe,CAAC,eAAe,CAAC,wBAAwB,CAAC,CAAC,CAAA;AAEjG,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,aAAa,SAAc;IAClC,MAAM,CAAC,gBAAgB,SAAa;IAEpC,OAAO,CAAC,GAAG,CAAsB;IAEjC;;;;;;OAMG;IACH,IAAI,MAAM,WAET;IAED,IAAa,OAAO,aAEnB;IACD;;;;;;;;;OASG;IACH,IAAI,SAAS,WAEZ;IAED,OAAO,KAAK,EAAE,GAEb;cAEwB,UAAU,IAAI,OAAO,CAAC,OAAO,EAAE,CAAC;cAKhC,YAAY,IAAI,OAAO,CAAC,IAAI,CAAC;cAI7B,aAAa,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;cAWlD,UAAU,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,OAAO,EAAE,CAAC;cAIhD,aAAa,CAAC,QAAQ,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC,OAAO,EAAE,CAAC;cAYtD,YAAY;CAOtC"}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
+
var IndexedDbArchivist_exports = {};
|
|
21
|
+
__export(IndexedDbArchivist_exports, {
|
|
22
|
+
IndexedDbArchivist: () => IndexedDbArchivist,
|
|
23
|
+
IndexedDbArchivistConfigSchema: () => IndexedDbArchivistConfigSchema
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(IndexedDbArchivist_exports);
|
|
26
|
+
var import_assert = require("@xylabs/assert");
|
|
27
|
+
var import_archivist_abstract = require("@xyo-network/archivist-abstract");
|
|
28
|
+
var import_archivist_model = require("@xyo-network/archivist-model");
|
|
29
|
+
var import_core = require("@xyo-network/core");
|
|
30
|
+
var import_module = require("@xyo-network/module");
|
|
31
|
+
var import_idb_keyval = require("idb-keyval");
|
|
32
|
+
function _ts_decorate(decorators, target, key, desc) {
|
|
33
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
34
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
|
|
35
|
+
r = Reflect.decorate(decorators, target, key, desc);
|
|
36
|
+
else
|
|
37
|
+
for (var i = decorators.length - 1; i >= 0; i--)
|
|
38
|
+
if (d = decorators[i])
|
|
39
|
+
r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
40
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
41
|
+
}
|
|
42
|
+
__name(_ts_decorate, "_ts_decorate");
|
|
43
|
+
const IndexedDbArchivistConfigSchema = "network.xyo.archivist.indexeddb.config";
|
|
44
|
+
let IndexedDbArchivist = class IndexedDbArchivist2 extends import_archivist_abstract.AbstractArchivist {
|
|
45
|
+
static {
|
|
46
|
+
__name(this, "IndexedDbArchivist");
|
|
47
|
+
}
|
|
48
|
+
static configSchemas = [
|
|
49
|
+
IndexedDbArchivistConfigSchema
|
|
50
|
+
];
|
|
51
|
+
static defaultDbName = "archivist";
|
|
52
|
+
static defaultStoreName = "payloads";
|
|
53
|
+
_db;
|
|
54
|
+
/**
|
|
55
|
+
* The database name. If not supplied via config, it defaults
|
|
56
|
+
* to the module name (not guaranteed to be unique) and if module
|
|
57
|
+
* name is not supplied, it defaults to `archivist`. This behavior
|
|
58
|
+
* biases towards a single, isolated DB per archivist which seems to
|
|
59
|
+
* make the most sense for 99% of use cases.
|
|
60
|
+
*/
|
|
61
|
+
get dbName() {
|
|
62
|
+
return this.config?.dbName ?? this.config?.name ?? IndexedDbArchivist2.defaultDbName;
|
|
63
|
+
}
|
|
64
|
+
get queries() {
|
|
65
|
+
return [
|
|
66
|
+
import_archivist_model.ArchivistAllQuerySchema,
|
|
67
|
+
import_archivist_model.ArchivistClearQuerySchema,
|
|
68
|
+
import_archivist_model.ArchivistDeleteQuerySchema,
|
|
69
|
+
import_archivist_model.ArchivistInsertQuerySchema,
|
|
70
|
+
...super.queries
|
|
71
|
+
];
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* The name of the object store. If not supplied via config, it defaults
|
|
75
|
+
* to `payloads`. The limitation of the current IndexedDB wrapper we're
|
|
76
|
+
* using is that it only supports a single object store per DB. See here:
|
|
77
|
+
* https://github.com/jakearchibald/idb-keyval/blob/main/custom-stores.md#defining-a-custom-database--store-name
|
|
78
|
+
* If this becomes a problem or we need migrations/transactions, we can
|
|
79
|
+
* move to this more-flexible library, which they recommend (and who
|
|
80
|
+
* recommends them for our simple use case of key-value storage):
|
|
81
|
+
* https://www.npmjs.com/package/idb
|
|
82
|
+
*/
|
|
83
|
+
get storeName() {
|
|
84
|
+
return this.config?.storeName ?? IndexedDbArchivist2.defaultStoreName;
|
|
85
|
+
}
|
|
86
|
+
get db() {
|
|
87
|
+
return (0, import_assert.assertEx)(this._db, "DB not initialized");
|
|
88
|
+
}
|
|
89
|
+
async allHandler() {
|
|
90
|
+
const result = await (0, import_idb_keyval.entries)(this.db);
|
|
91
|
+
return result.map(([_hash, payload]) => payload);
|
|
92
|
+
}
|
|
93
|
+
async clearHandler() {
|
|
94
|
+
await (0, import_idb_keyval.clear)(this.db);
|
|
95
|
+
}
|
|
96
|
+
async deleteHandler(hashes) {
|
|
97
|
+
const payloadPairs = await Promise.all((await this.get(hashes)).map(async (payload) => [
|
|
98
|
+
await import_core.PayloadHasher.hashAsync(payload),
|
|
99
|
+
payload
|
|
100
|
+
]));
|
|
101
|
+
const foundHashesToDelete = payloadPairs.map(([hash, _]) => hash);
|
|
102
|
+
await (0, import_idb_keyval.delMany)(foundHashesToDelete, this.db);
|
|
103
|
+
return foundHashesToDelete;
|
|
104
|
+
}
|
|
105
|
+
async getHandler(hashes) {
|
|
106
|
+
return (await (0, import_idb_keyval.getMany)(hashes, this.db)).filter((result) => result !== void 0);
|
|
107
|
+
}
|
|
108
|
+
async insertHandler(payloads) {
|
|
109
|
+
const entries2 = await Promise.all(payloads.map(async (payload) => {
|
|
110
|
+
const hash = await import_core.PayloadHasher.hashAsync(payload);
|
|
111
|
+
return [
|
|
112
|
+
hash,
|
|
113
|
+
payload
|
|
114
|
+
];
|
|
115
|
+
}));
|
|
116
|
+
await (0, import_idb_keyval.setMany)(entries2, this.db);
|
|
117
|
+
return payloads;
|
|
118
|
+
}
|
|
119
|
+
async startHandler() {
|
|
120
|
+
await super.startHandler();
|
|
121
|
+
this._db = (0, import_idb_keyval.createStore)(this.dbName, this.storeName);
|
|
122
|
+
return true;
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
IndexedDbArchivist = _ts_decorate([
|
|
126
|
+
(0, import_module.creatableModule)()
|
|
127
|
+
], IndexedDbArchivist);
|
|
128
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
129
|
+
0 && (module.exports = {
|
|
130
|
+
IndexedDbArchivist,
|
|
131
|
+
IndexedDbArchivistConfigSchema
|
|
132
|
+
});
|
|
133
|
+
//# sourceMappingURL=IndexedDbArchivist.js.map
|