@xyo-network/diviner-payload-indexeddb 2.107.1 → 2.107.4
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/index.cjs +217 -1
- package/dist/browser/index.cjs.map +1 -1
- package/dist/browser/index.js +196 -1
- package/dist/browser/index.js.map +1 -1
- package/dist/neutral/index.cjs +217 -1
- package/dist/neutral/index.cjs.map +1 -1
- package/dist/neutral/index.js +196 -1
- package/dist/neutral/index.js.map +1 -1
- package/dist/node/index.cjs +230 -1
- package/dist/node/index.cjs.map +1 -1
- package/dist/node/index.js +203 -1
- package/dist/node/index.js.map +1 -1
- package/package.json +14 -14
package/dist/neutral/index.cjs
CHANGED
|
@@ -1,2 +1,218 @@
|
|
|
1
|
-
"use strict";
|
|
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/index.ts
|
|
22
|
+
var src_exports = {};
|
|
23
|
+
__export(src_exports, {
|
|
24
|
+
IndexedDbPayloadDiviner: () => IndexedDbPayloadDiviner,
|
|
25
|
+
IndexedDbPayloadDivinerConfigSchema: () => IndexedDbPayloadDivinerConfigSchema,
|
|
26
|
+
IndexedDbPayloadDivinerSchema: () => IndexedDbPayloadDivinerSchema
|
|
27
|
+
});
|
|
28
|
+
module.exports = __toCommonJS(src_exports);
|
|
29
|
+
|
|
30
|
+
// src/Schema.ts
|
|
31
|
+
var import_diviner_payload_model = require("@xyo-network/diviner-payload-model");
|
|
32
|
+
var IndexedDbPayloadDivinerSchema = `${import_diviner_payload_model.PayloadDivinerSchema}.indexeddb`;
|
|
33
|
+
|
|
34
|
+
// src/Config.ts
|
|
35
|
+
var IndexedDbPayloadDivinerConfigSchema = `${IndexedDbPayloadDivinerSchema}.config`;
|
|
36
|
+
|
|
37
|
+
// src/Diviner.ts
|
|
38
|
+
var import_array = require("@xylabs/array");
|
|
39
|
+
var import_assert = require("@xylabs/assert");
|
|
40
|
+
var import_exists = require("@xylabs/exists");
|
|
41
|
+
var import_object = require("@xylabs/object");
|
|
42
|
+
var import_archivist_indexeddb = require("@xyo-network/archivist-indexeddb");
|
|
43
|
+
var import_archivist_model = require("@xyo-network/archivist-model");
|
|
44
|
+
var import_diviner_payload_abstract = require("@xyo-network/diviner-payload-abstract");
|
|
45
|
+
var import_diviner_payload_model2 = require("@xyo-network/diviner-payload-model");
|
|
46
|
+
var import_payload_builder = require("@xyo-network/payload-builder");
|
|
47
|
+
var import_idb = require("idb");
|
|
48
|
+
var payloadValueFilter = /* @__PURE__ */ __name((key, value) => {
|
|
49
|
+
if (!value) return void 0;
|
|
50
|
+
return (payload) => {
|
|
51
|
+
if (!payload) return false;
|
|
52
|
+
const sourceValue = payload?.[key];
|
|
53
|
+
if (sourceValue === void 0) return false;
|
|
54
|
+
return Array.isArray(sourceValue) && Array.isArray(value) ? (0, import_array.containsAll)(sourceValue, value) : sourceValue == value;
|
|
55
|
+
};
|
|
56
|
+
}, "payloadValueFilter");
|
|
57
|
+
var IndexedDbPayloadDiviner = class extends import_diviner_payload_abstract.PayloadDiviner {
|
|
58
|
+
static {
|
|
59
|
+
__name(this, "IndexedDbPayloadDiviner");
|
|
60
|
+
}
|
|
61
|
+
static configSchemas = [
|
|
62
|
+
...super.configSchemas,
|
|
63
|
+
IndexedDbPayloadDivinerConfigSchema
|
|
64
|
+
];
|
|
65
|
+
static defaultConfigSchema = IndexedDbPayloadDivinerConfigSchema;
|
|
66
|
+
_db;
|
|
67
|
+
/**
|
|
68
|
+
* The database name. If not supplied via config, it defaults
|
|
69
|
+
* to the archivist's name and if archivist's name is not supplied,
|
|
70
|
+
* it defaults to `archivist`. This behavior
|
|
71
|
+
* biases towards a single, isolated DB per archivist which seems to
|
|
72
|
+
* make the most sense for 99% of use cases.
|
|
73
|
+
*/
|
|
74
|
+
get dbName() {
|
|
75
|
+
return this.config?.dbName ?? this.config?.archivist ?? import_archivist_indexeddb.IndexedDbArchivist.defaultDbName;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* The database version. If not supplied via config, it defaults to the archivist default version.
|
|
79
|
+
*/
|
|
80
|
+
get dbVersion() {
|
|
81
|
+
return this.config?.dbVersion ?? import_archivist_indexeddb.IndexedDbArchivist.defaultDbVersion;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* The name of the object store. If not supplied via config, it defaults
|
|
85
|
+
* to `payloads`.
|
|
86
|
+
*/
|
|
87
|
+
get storeName() {
|
|
88
|
+
return this.config?.storeName ?? import_archivist_indexeddb.IndexedDbArchivist.defaultStoreName;
|
|
89
|
+
}
|
|
90
|
+
async divineHandler(payloads) {
|
|
91
|
+
const query = payloads?.find(import_diviner_payload_model2.isPayloadDivinerQueryPayload);
|
|
92
|
+
if (!query) return [];
|
|
93
|
+
const result = await this.tryUseDb(async (db) => {
|
|
94
|
+
const { schemas, limit, offset, order, ...props } = (0, import_object.removeFields)(query, [
|
|
95
|
+
"hash",
|
|
96
|
+
"schema",
|
|
97
|
+
"$meta",
|
|
98
|
+
"$hash"
|
|
99
|
+
]);
|
|
100
|
+
const tx = db.transaction(this.storeName, "readonly");
|
|
101
|
+
const store = tx.objectStore(this.storeName);
|
|
102
|
+
const results = [];
|
|
103
|
+
let parsedOffset = offset ?? 0;
|
|
104
|
+
const parsedLimit = limit ?? 10;
|
|
105
|
+
(0, import_assert.assertEx)((schemas?.length ?? 1) === 1, () => "IndexedDbPayloadDiviner: Only one filter schema supported");
|
|
106
|
+
const filterSchema = schemas?.[0];
|
|
107
|
+
const filter = filterSchema ? {
|
|
108
|
+
schema: filterSchema,
|
|
109
|
+
...props
|
|
110
|
+
} : {
|
|
111
|
+
...props
|
|
112
|
+
};
|
|
113
|
+
const direction = order === "desc" ? "prev" : "next";
|
|
114
|
+
const suggestedIndex = this.selectBestIndex(filter, store);
|
|
115
|
+
const keyRangeValue = this.getKeyRangeValue(suggestedIndex, filter);
|
|
116
|
+
const valueFilters = props ? Object.entries(props).map(([key, value]) => payloadValueFilter(key, value)).filter(import_exists.exists) : [];
|
|
117
|
+
let cursor = suggestedIndex ? (
|
|
118
|
+
// Conditionally filter on schemas
|
|
119
|
+
await store.index(suggestedIndex).openCursor(IDBKeyRange.only(keyRangeValue), direction)
|
|
120
|
+
) : await store.openCursor(suggestedIndex, direction);
|
|
121
|
+
while (cursor && parsedOffset > 0) {
|
|
122
|
+
cursor = await cursor.advance(parsedOffset);
|
|
123
|
+
parsedOffset = 0;
|
|
124
|
+
}
|
|
125
|
+
while (cursor && results.length < parsedLimit) {
|
|
126
|
+
const value = cursor.value;
|
|
127
|
+
if (value) {
|
|
128
|
+
if (valueFilters.length > 0) {
|
|
129
|
+
if (valueFilters.every((filter2) => filter2(value))) {
|
|
130
|
+
results.push(value);
|
|
131
|
+
}
|
|
132
|
+
} else {
|
|
133
|
+
results.push(value);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
try {
|
|
137
|
+
cursor = await cursor.continue();
|
|
138
|
+
} catch {
|
|
139
|
+
break;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
await tx.done;
|
|
143
|
+
return await Promise.all(results.map((payload) => import_payload_builder.PayloadBuilder.build(payload)));
|
|
144
|
+
});
|
|
145
|
+
return result ?? [];
|
|
146
|
+
}
|
|
147
|
+
async startHandler() {
|
|
148
|
+
await super.startHandler();
|
|
149
|
+
return true;
|
|
150
|
+
}
|
|
151
|
+
getKeyRangeValue(indexName, query) {
|
|
152
|
+
if (!indexName) return [];
|
|
153
|
+
const extractFields = /* @__PURE__ */ __name((indexName2) => {
|
|
154
|
+
return indexName2.slice(3).split(import_archivist_model.IndexSeparator).map((field) => field.toLowerCase());
|
|
155
|
+
}, "extractFields");
|
|
156
|
+
const indexFields = extractFields(indexName);
|
|
157
|
+
const keyRangeValue = indexFields.map((field) => query[field]);
|
|
158
|
+
return keyRangeValue.length === 1 ? keyRangeValue[0] : keyRangeValue;
|
|
159
|
+
}
|
|
160
|
+
selectBestIndex(query, store) {
|
|
161
|
+
const { indexNames } = store;
|
|
162
|
+
const extractFields = /* @__PURE__ */ __name((indexName) => {
|
|
163
|
+
return indexName.slice(3).split(import_archivist_model.IndexSeparator).map((field) => field.toLowerCase());
|
|
164
|
+
}, "extractFields");
|
|
165
|
+
const queryKeys = new Set(Object.keys(query).map((key) => key.toLowerCase()));
|
|
166
|
+
let bestMatch = {
|
|
167
|
+
indexName: "",
|
|
168
|
+
matchCount: 0
|
|
169
|
+
};
|
|
170
|
+
for (const indexName of indexNames) {
|
|
171
|
+
const indexFields = extractFields(indexName);
|
|
172
|
+
const matchCount = indexFields.filter((field) => queryKeys.has(field)).length;
|
|
173
|
+
if (matchCount > bestMatch.matchCount) {
|
|
174
|
+
bestMatch = {
|
|
175
|
+
indexName,
|
|
176
|
+
matchCount
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
return bestMatch.matchCount > 0 ? bestMatch.indexName : null;
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Checks that the desired DB/objectStore exists and is initialized to the correct version
|
|
184
|
+
* @returns The initialized DB or undefined if it does not exist in the desired state
|
|
185
|
+
*/
|
|
186
|
+
async tryGetInitializedDb() {
|
|
187
|
+
const dbs = await indexedDB.databases();
|
|
188
|
+
const dbExists = dbs.some((db) => {
|
|
189
|
+
return db.name === this.dbName && db.version === this.dbVersion;
|
|
190
|
+
});
|
|
191
|
+
if (dbExists) {
|
|
192
|
+
const db = await (0, import_idb.openDB)(this.dbName, this.dbVersion);
|
|
193
|
+
const storeExists = db.objectStoreNames.contains(this.storeName);
|
|
194
|
+
if (storeExists) {
|
|
195
|
+
return db;
|
|
196
|
+
} else {
|
|
197
|
+
db.close();
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Executes a callback with the initialized DB and then closes the db
|
|
203
|
+
* @param callback The method to execute with the initialized DB
|
|
204
|
+
* @returns
|
|
205
|
+
*/
|
|
206
|
+
async tryUseDb(callback) {
|
|
207
|
+
const db = await this.tryGetInitializedDb();
|
|
208
|
+
if (db) {
|
|
209
|
+
try {
|
|
210
|
+
return await callback(db);
|
|
211
|
+
} finally {
|
|
212
|
+
db.close();
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
return void 0;
|
|
216
|
+
}
|
|
217
|
+
};
|
|
2
218
|
//# sourceMappingURL=index.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/index.ts","../../src/Schema.ts","../../src/Config.ts","../../src/Diviner.ts"],"sourcesContent":["export * from './Config'\nexport * from './Diviner'\nexport * from './Params'\nexport * from './Schema'\n","import { PayloadDivinerSchema } from '@xyo-network/diviner-payload-model'\n\nexport const IndexedDbPayloadDivinerSchema = `${PayloadDivinerSchema}.indexeddb` as const\nexport type IndexedDbPayloadDivinerSchema = typeof IndexedDbPayloadDivinerSchema\n","import { IndexDescription } from '@xyo-network/archivist-model'\nimport { DivinerConfig } from '@xyo-network/diviner-model'\n\nimport { IndexedDbPayloadDivinerSchema } from './Schema'\n\nexport const IndexedDbPayloadDivinerConfigSchema = `${IndexedDbPayloadDivinerSchema}.config` as const\nexport type IndexedDbPayloadDivinerConfigSchema = typeof IndexedDbPayloadDivinerConfigSchema\n\nexport type IndexedDbPayloadDivinerConfig = DivinerConfig<{\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: IndexedDbPayloadDivinerConfigSchema\n /**\n * The storage configuration\n * // TODO: Hoist to main diviner config\n */\n storage?: {\n /**\n * The indexes to create on the object store\n */\n indexes?: IndexDescription[]\n }\n /**\n * The name of the object store\n */\n storeName?: string\n}>\n","import { containsAll } from '@xylabs/array'\nimport { assertEx } from '@xylabs/assert'\nimport { exists } from '@xylabs/exists'\nimport { Hash } from '@xylabs/hex'\nimport { AnyObject, removeFields } from '@xylabs/object'\nimport { IndexedDbArchivist } from '@xyo-network/archivist-indexeddb'\nimport { IndexSeparator } from '@xyo-network/archivist-model'\nimport { DivinerInstance, DivinerModuleEventData } from '@xyo-network/diviner-model'\nimport { PayloadDiviner } from '@xyo-network/diviner-payload-abstract'\nimport { isPayloadDivinerQueryPayload, PayloadDivinerQueryPayload } from '@xyo-network/diviner-payload-model'\nimport { PayloadBuilder } from '@xyo-network/payload-builder'\nimport { Payload, Schema, WithMeta } from '@xyo-network/payload-model'\nimport { IDBPDatabase, IDBPObjectStore, openDB } from 'idb'\n\nimport { IndexedDbPayloadDivinerConfigSchema } from './Config'\nimport { IndexedDbPayloadDivinerParams } from './Params'\n\ninterface PayloadStore {\n [s: string]: Payload\n}\n\ntype AnyPayload = Payload<Record<string, unknown>>\n\ntype ValueFilter = (payload?: AnyPayload | null) => boolean\n\nconst payloadValueFilter = (key: keyof AnyPayload, value?: unknown | unknown[]): ValueFilter | undefined => {\n if (!value) return undefined\n return (payload) => {\n if (!payload) return false\n const sourceValue = payload?.[key]\n if (sourceValue === undefined) return false\n return Array.isArray(sourceValue) && Array.isArray(value) ? containsAll(sourceValue, value) : sourceValue == value\n }\n}\n\nexport class IndexedDbPayloadDiviner<\n TParams extends IndexedDbPayloadDivinerParams = IndexedDbPayloadDivinerParams,\n TIn extends PayloadDivinerQueryPayload = PayloadDivinerQueryPayload,\n TOut extends Payload = Payload,\n TEventData extends DivinerModuleEventData<DivinerInstance<TParams, TIn, TOut>, TIn, TOut> = DivinerModuleEventData<\n DivinerInstance<TParams, TIn, TOut>,\n TIn,\n TOut\n >,\n> extends PayloadDiviner<TParams, TIn, TOut, TEventData> {\n static override readonly configSchemas: Schema[] = [...super.configSchemas, IndexedDbPayloadDivinerConfigSchema]\n static override readonly defaultConfigSchema: Schema = IndexedDbPayloadDivinerConfigSchema\n\n private _db: IDBPDatabase<PayloadStore> | undefined\n\n /**\n * The database name. If not supplied via config, it defaults\n * to the archivist's name and if archivist's name is not supplied,\n * 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?.archivist ?? IndexedDbArchivist.defaultDbName\n }\n\n /**\n * The database version. If not supplied via config, it defaults to the archivist default version.\n */\n get dbVersion() {\n return this.config?.dbVersion ?? IndexedDbArchivist.defaultDbVersion\n }\n\n /**\n * The name of the object store. If not supplied via config, it defaults\n * to `payloads`.\n */\n get storeName() {\n return this.config?.storeName ?? IndexedDbArchivist.defaultStoreName\n }\n\n protected override async divineHandler(payloads?: TIn[]): Promise<TOut[]> {\n const query = payloads?.find(isPayloadDivinerQueryPayload) as TIn\n if (!query) return []\n const result = await this.tryUseDb(async (db) => {\n const { schemas, limit, offset, order, ...props } = removeFields(query as unknown as WithMeta<TIn> & { sources?: Hash[] }, [\n 'hash',\n 'schema',\n '$meta',\n '$hash',\n ])\n const tx = db.transaction(this.storeName, 'readonly')\n const store = tx.objectStore(this.storeName)\n const results: TOut[] = []\n let parsedOffset = offset ?? 0\n const parsedLimit = limit ?? 10\n assertEx((schemas?.length ?? 1) === 1, () => 'IndexedDbPayloadDiviner: Only one filter schema supported')\n const filterSchema = schemas?.[0]\n const filter = filterSchema ? { schema: filterSchema, ...props } : { ...props }\n const direction: IDBCursorDirection = order === 'desc' ? 'prev' : 'next'\n const suggestedIndex = this.selectBestIndex(filter, store)\n const keyRangeValue = this.getKeyRangeValue(suggestedIndex, filter)\n const valueFilters: ValueFilter[] =\n props ?\n Object.entries(props)\n .map(([key, value]) => payloadValueFilter(key, value))\n .filter(exists)\n : []\n let cursor =\n suggestedIndex ?\n // Conditionally filter on schemas\n await store.index(suggestedIndex).openCursor(IDBKeyRange.only(keyRangeValue), direction)\n // Just iterate all records\n : await store.openCursor(suggestedIndex, direction)\n\n // Skip records until the offset is reached\n while (cursor && parsedOffset > 0) {\n cursor = await cursor.advance(parsedOffset)\n parsedOffset = 0 // Reset offset after skipping\n }\n // Collect results up to the limit\n while (cursor && results.length < parsedLimit) {\n const value = cursor.value\n if (value) {\n // If we're filtering on more than just the schema\n if (valueFilters.length > 0) {\n // Ensure all filters pass\n if (valueFilters.every((filter) => filter(value))) {\n // Then save the value\n results.push(value)\n }\n } else {\n // Otherwise just save the value\n results.push(value)\n }\n }\n try {\n cursor = await cursor.continue()\n } catch {\n break\n }\n }\n await tx.done\n // Remove any metadata before returning to the client\n return await Promise.all(results.map((payload) => PayloadBuilder.build(payload)))\n })\n return result ?? []\n }\n\n protected override async startHandler() {\n await super.startHandler()\n return true\n }\n\n private getKeyRangeValue(indexName: string | null, query: AnyObject): unknown | unknown[] {\n if (!indexName) return []\n // Function to extract fields from an index name\n const extractFields = (indexName: string): string[] => {\n return indexName\n .slice(3)\n .split(IndexSeparator)\n .map((field) => field.toLowerCase())\n }\n\n // Extracting the relevant fields from the index name\n const indexFields = extractFields(indexName)\n\n // Collecting the values for these fields from the query object\n const keyRangeValue = indexFields.map((field) => query[field as keyof AnyObject])\n return keyRangeValue.length === 1 ? keyRangeValue[0] : keyRangeValue\n }\n\n private selectBestIndex(query: AnyObject, store: IDBPObjectStore<PayloadStore>): string | null {\n // List of available indexes\n const { indexNames } = store\n\n // Function to extract fields from an index name\n const extractFields = (indexName: string): string[] => {\n return indexName\n .slice(3)\n .split(IndexSeparator)\n .map((field) => field.toLowerCase())\n }\n\n // Convert query object keys to a set for easier comparison\n const queryKeys = new Set(Object.keys(query).map((key) => key.toLowerCase()))\n\n // Find the best matching index\n let bestMatch: { indexName: string; matchCount: number } = { indexName: '', matchCount: 0 }\n\n for (const indexName of indexNames) {\n const indexFields = extractFields(indexName)\n const matchCount = indexFields.filter((field) => queryKeys.has(field)).length\n if (matchCount > bestMatch.matchCount) {\n bestMatch = { indexName, matchCount }\n }\n }\n return bestMatch.matchCount > 0 ? bestMatch.indexName : null\n }\n\n /**\n * Checks that the desired DB/objectStore exists and is initialized to the correct version\n * @returns The initialized DB or undefined if it does not exist in the desired state\n */\n private async tryGetInitializedDb(): Promise<IDBPDatabase<PayloadStore> | undefined> {\n // Enumerate the DBs\n const dbs = await indexedDB.databases()\n // Check that the DB exists at the desired version\n const dbExists = dbs.some((db) => {\n return db.name === this.dbName && db.version === this.dbVersion\n })\n // If the DB exists at the desired version\n if (dbExists) {\n // If the db does exist, open it\n const db = await openDB<PayloadStore>(this.dbName, this.dbVersion)\n // Check that the desired objectStore exists\n const storeExists = db.objectStoreNames.contains(this.storeName)\n // If the correct db/version/objectStore exists\n if (storeExists) {\n return db\n } else {\n // Otherwise close the db so the process that is going to update the\n // db can open it\n db.close()\n }\n }\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 tryUseDb<T>(callback: (db: IDBPDatabase<PayloadStore>) => Promise<T> | T): Promise<T | undefined> {\n // Get the initialized DB\n const db = await this.tryGetInitializedDb()\n if (db) {\n try {\n // Perform the callback\n return await callback(db)\n } finally {\n // Close the DB\n db.close()\n }\n }\n return undefined\n }\n}\n"],"mappings":"4dAAA,IAAAA,EAAA,GAAAC,E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EAAK,QAAMgE,UAAqB,KAAK/E,OAAQ,KAAKK,SAAS,EAIjE,GAFoBU,EAAGiE,iBAAiBC,SAAS,KAAK1E,SAAS,EAG7D,OAAOQ,EAIPA,EAAGmE,MAAK,CAEZ,CACF,CAOA,MAAcpE,SAAYqE,EAAsF,CAE9G,IAAMpE,EAAK,MAAM,KAAK0D,oBAAmB,EACzC,GAAI1D,EACF,GAAI,CAEF,OAAO,MAAMoE,EAASpE,CAAAA,CACxB,QAAA,CAEEA,EAAGmE,MAAK,CACV,CAGJ,CACF","names":["src_exports","__export","IndexedDbPayloadDiviner","IndexedDbPayloadDivinerConfigSchema","IndexedDbPayloadDivinerSchema","__toCommonJS","import_diviner_payload_model","IndexedDbPayloadDivinerSchema","PayloadDivinerSchema","IndexedDbPayloadDivinerConfigSchema","IndexedDbPayloadDivinerSchema","import_array","import_assert","import_exists","import_object","import_archivist_indexeddb","import_archivist_model","import_diviner_payload_abstract","import_diviner_payload_model","import_payload_builder","import_idb","payloadValueFilter","__name","key","value","payload","sourceValue","undefined","Array","isArray","containsAll","IndexedDbPayloadDiviner","PayloadDiviner","configSchemas","IndexedDbPayloadDivinerConfigSchema","defaultConfigSchema","_db","dbName","config","archivist","IndexedDbArchivist","defaultDbName","dbVersion","defaultDbVersion","storeName","defaultStoreName","divineHandler","payloads","query","find","isPayloadDivinerQueryPayload","tryUseDb","db","schemas","limit","offset","order","props","removeFields","tx","transaction","store","objectStore","results","parsedOffset","parsedLimit","assertEx","length","filterSchema","filter","schema","direction","suggestedIndex","selectBestIndex","keyRangeValue","getKeyRangeValue","valueFilters","Object","entries","map","exists","cursor","index","openCursor","IDBKeyRange","only","advance","every","push","continue","done","Promise","all","PayloadBuilder","build","startHandler","indexName","slice","split","IndexSeparator","field","toLowerCase","indexNames","extractFields","queryKeys","Set","keys","bestMatch","matchCount","has","tryGetInitializedDb","indexedDB","databases","some","name","version","openDB","objectStoreNames","contains","close","callback"]}
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts","../../src/Schema.ts","../../src/Config.ts","../../src/Diviner.ts"],"sourcesContent":["export * from './Config'\nexport * from './Diviner'\nexport * from './Params'\nexport * from './Schema'\n","import { PayloadDivinerSchema } from '@xyo-network/diviner-payload-model'\n\nexport const IndexedDbPayloadDivinerSchema = `${PayloadDivinerSchema}.indexeddb` as const\nexport type IndexedDbPayloadDivinerSchema = typeof IndexedDbPayloadDivinerSchema\n","import { IndexDescription } from '@xyo-network/archivist-model'\nimport { DivinerConfig } from '@xyo-network/diviner-model'\n\nimport { IndexedDbPayloadDivinerSchema } from './Schema'\n\nexport const IndexedDbPayloadDivinerConfigSchema = `${IndexedDbPayloadDivinerSchema}.config` as const\nexport type IndexedDbPayloadDivinerConfigSchema = typeof IndexedDbPayloadDivinerConfigSchema\n\nexport type IndexedDbPayloadDivinerConfig = DivinerConfig<{\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: IndexedDbPayloadDivinerConfigSchema\n /**\n * The storage configuration\n * // TODO: Hoist to main diviner config\n */\n storage?: {\n /**\n * The indexes to create on the object store\n */\n indexes?: IndexDescription[]\n }\n /**\n * The name of the object store\n */\n storeName?: string\n}>\n","import { containsAll } from '@xylabs/array'\nimport { assertEx } from '@xylabs/assert'\nimport { exists } from '@xylabs/exists'\nimport { Hash } from '@xylabs/hex'\nimport { AnyObject, removeFields } from '@xylabs/object'\nimport { IndexedDbArchivist } from '@xyo-network/archivist-indexeddb'\nimport { IndexSeparator } from '@xyo-network/archivist-model'\nimport { DivinerInstance, DivinerModuleEventData } from '@xyo-network/diviner-model'\nimport { PayloadDiviner } from '@xyo-network/diviner-payload-abstract'\nimport { isPayloadDivinerQueryPayload, PayloadDivinerQueryPayload } from '@xyo-network/diviner-payload-model'\nimport { PayloadBuilder } from '@xyo-network/payload-builder'\nimport { Payload, Schema, WithMeta } from '@xyo-network/payload-model'\nimport { IDBPDatabase, IDBPObjectStore, openDB } from 'idb'\n\nimport { IndexedDbPayloadDivinerConfigSchema } from './Config'\nimport { IndexedDbPayloadDivinerParams } from './Params'\n\ninterface PayloadStore {\n [s: string]: Payload\n}\n\ntype AnyPayload = Payload<Record<string, unknown>>\n\ntype ValueFilter = (payload?: AnyPayload | null) => boolean\n\nconst payloadValueFilter = (key: keyof AnyPayload, value?: unknown | unknown[]): ValueFilter | undefined => {\n if (!value) return undefined\n return (payload) => {\n if (!payload) return false\n const sourceValue = payload?.[key]\n if (sourceValue === undefined) return false\n return Array.isArray(sourceValue) && Array.isArray(value) ? containsAll(sourceValue, value) : sourceValue == value\n }\n}\n\nexport class IndexedDbPayloadDiviner<\n TParams extends IndexedDbPayloadDivinerParams = IndexedDbPayloadDivinerParams,\n TIn extends PayloadDivinerQueryPayload = PayloadDivinerQueryPayload,\n TOut extends Payload = Payload,\n TEventData extends DivinerModuleEventData<DivinerInstance<TParams, TIn, TOut>, TIn, TOut> = DivinerModuleEventData<\n DivinerInstance<TParams, TIn, TOut>,\n TIn,\n TOut\n >,\n> extends PayloadDiviner<TParams, TIn, TOut, TEventData> {\n static override readonly configSchemas: Schema[] = [...super.configSchemas, IndexedDbPayloadDivinerConfigSchema]\n static override readonly defaultConfigSchema: Schema = IndexedDbPayloadDivinerConfigSchema\n\n private _db: IDBPDatabase<PayloadStore> | undefined\n\n /**\n * The database name. If not supplied via config, it defaults\n * to the archivist's name and if archivist's name is not supplied,\n * 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?.archivist ?? IndexedDbArchivist.defaultDbName\n }\n\n /**\n * The database version. If not supplied via config, it defaults to the archivist default version.\n */\n get dbVersion() {\n return this.config?.dbVersion ?? IndexedDbArchivist.defaultDbVersion\n }\n\n /**\n * The name of the object store. If not supplied via config, it defaults\n * to `payloads`.\n */\n get storeName() {\n return this.config?.storeName ?? IndexedDbArchivist.defaultStoreName\n }\n\n protected override async divineHandler(payloads?: TIn[]): Promise<TOut[]> {\n const query = payloads?.find(isPayloadDivinerQueryPayload) as TIn\n if (!query) return []\n const result = await this.tryUseDb(async (db) => {\n const { schemas, limit, offset, order, ...props } = removeFields(query as unknown as WithMeta<TIn> & { sources?: Hash[] }, [\n 'hash',\n 'schema',\n '$meta',\n '$hash',\n ])\n const tx = db.transaction(this.storeName, 'readonly')\n const store = tx.objectStore(this.storeName)\n const results: TOut[] = []\n let parsedOffset = offset ?? 0\n const parsedLimit = limit ?? 10\n assertEx((schemas?.length ?? 1) === 1, () => 'IndexedDbPayloadDiviner: Only one filter schema supported')\n const filterSchema = schemas?.[0]\n const filter = filterSchema ? { schema: filterSchema, ...props } : { ...props }\n const direction: IDBCursorDirection = order === 'desc' ? 'prev' : 'next'\n const suggestedIndex = this.selectBestIndex(filter, store)\n const keyRangeValue = this.getKeyRangeValue(suggestedIndex, filter)\n const valueFilters: ValueFilter[] =\n props ?\n Object.entries(props)\n .map(([key, value]) => payloadValueFilter(key, value))\n .filter(exists)\n : []\n let cursor =\n suggestedIndex ?\n // Conditionally filter on schemas\n await store.index(suggestedIndex).openCursor(IDBKeyRange.only(keyRangeValue), direction)\n // Just iterate all records\n : await store.openCursor(suggestedIndex, direction)\n\n // Skip records until the offset is reached\n while (cursor && parsedOffset > 0) {\n cursor = await cursor.advance(parsedOffset)\n parsedOffset = 0 // Reset offset after skipping\n }\n // Collect results up to the limit\n while (cursor && results.length < parsedLimit) {\n const value = cursor.value\n if (value) {\n // If we're filtering on more than just the schema\n if (valueFilters.length > 0) {\n // Ensure all filters pass\n if (valueFilters.every((filter) => filter(value))) {\n // Then save the value\n results.push(value)\n }\n } else {\n // Otherwise just save the value\n results.push(value)\n }\n }\n try {\n cursor = await cursor.continue()\n } catch {\n break\n }\n }\n await tx.done\n // Remove any metadata before returning to the client\n return await Promise.all(results.map((payload) => PayloadBuilder.build(payload)))\n })\n return result ?? []\n }\n\n protected override async startHandler() {\n await super.startHandler()\n return true\n }\n\n private getKeyRangeValue(indexName: string | null, query: AnyObject): unknown | unknown[] {\n if (!indexName) return []\n // Function to extract fields from an index name\n const extractFields = (indexName: string): string[] => {\n return indexName\n .slice(3)\n .split(IndexSeparator)\n .map((field) => field.toLowerCase())\n }\n\n // Extracting the relevant fields from the index name\n const indexFields = extractFields(indexName)\n\n // Collecting the values for these fields from the query object\n const keyRangeValue = indexFields.map((field) => query[field as keyof AnyObject])\n return keyRangeValue.length === 1 ? keyRangeValue[0] : keyRangeValue\n }\n\n private selectBestIndex(query: AnyObject, store: IDBPObjectStore<PayloadStore>): string | null {\n // List of available indexes\n const { indexNames } = store\n\n // Function to extract fields from an index name\n const extractFields = (indexName: string): string[] => {\n return indexName\n .slice(3)\n .split(IndexSeparator)\n .map((field) => field.toLowerCase())\n }\n\n // Convert query object keys to a set for easier comparison\n const queryKeys = new Set(Object.keys(query).map((key) => key.toLowerCase()))\n\n // Find the best matching index\n let bestMatch: { indexName: string; matchCount: number } = { indexName: '', matchCount: 0 }\n\n for (const indexName of indexNames) {\n const indexFields = extractFields(indexName)\n const matchCount = indexFields.filter((field) => queryKeys.has(field)).length\n if (matchCount > bestMatch.matchCount) {\n bestMatch = { indexName, matchCount }\n }\n }\n return bestMatch.matchCount > 0 ? bestMatch.indexName : null\n }\n\n /**\n * Checks that the desired DB/objectStore exists and is initialized to the correct version\n * @returns The initialized DB or undefined if it does not exist in the desired state\n */\n private async tryGetInitializedDb(): Promise<IDBPDatabase<PayloadStore> | undefined> {\n // Enumerate the DBs\n const dbs = await indexedDB.databases()\n // Check that the DB exists at the desired version\n const dbExists = dbs.some((db) => {\n return db.name === this.dbName && db.version === this.dbVersion\n })\n // If the DB exists at the desired version\n if (dbExists) {\n // If the db does exist, open it\n const db = await openDB<PayloadStore>(this.dbName, this.dbVersion)\n // Check that the desired objectStore exists\n const storeExists = db.objectStoreNames.contains(this.storeName)\n // If the correct db/version/objectStore exists\n if (storeExists) {\n return db\n } else {\n // Otherwise close the db so the process that is going to update the\n // db can open it\n db.close()\n }\n }\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 tryUseDb<T>(callback: (db: IDBPDatabase<PayloadStore>) => Promise<T> | T): Promise<T | undefined> {\n // Get the initialized DB\n const db = await this.tryGetInitializedDb()\n if (db) {\n try {\n // Perform the callback\n return await callback(db)\n } finally {\n // Close the DB\n db.close()\n }\n }\n return undefined\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;ACAA,mCAAqC;AAE9B,IAAMA,gCAAgC,GAAGC,iDAAAA;;;ACGzC,IAAMC,sCAAsC,GAAGC,6BAAAA;;;ACLtD,mBAA4B;AAC5B,oBAAyB;AACzB,oBAAuB;AAEvB,oBAAwC;AACxC,iCAAmC;AACnC,6BAA+B;AAE/B,sCAA+B;AAC/B,IAAAC,gCAAyE;AACzE,6BAA+B;AAE/B,iBAAsD;AAatD,IAAMC,qBAAqB,wBAACC,KAAuBC,UAAAA;AACjD,MAAI,CAACA,MAAO,QAAOC;AACnB,SAAO,CAACC,YAAAA;AACN,QAAI,CAACA,QAAS,QAAO;AACrB,UAAMC,cAAcD,UAAUH,GAAAA;AAC9B,QAAII,gBAAgBF,OAAW,QAAO;AACtC,WAAOG,MAAMC,QAAQF,WAAAA,KAAgBC,MAAMC,QAAQL,KAAAA,QAASM,0BAAYH,aAAaH,KAAAA,IAASG,eAAeH;EAC/G;AACF,GAR2B;AAUpB,IAAMO,0BAAN,cASGC,+CAAAA;EA5CV,OA4CUA;;;EACR,OAAyBC,gBAA0B;OAAI,MAAMA;IAAeC;;EAC5E,OAAyBC,sBAA8BD;EAE/CE;;;;;;;;EASR,IAAIC,SAAS;AACX,WAAO,KAAKC,QAAQD,UAAU,KAAKC,QAAQC,aAAaC,8CAAmBC;EAC7E;;;;EAKA,IAAIC,YAAY;AACd,WAAO,KAAKJ,QAAQI,aAAaF,8CAAmBG;EACtD;;;;;EAMA,IAAIC,YAAY;AACd,WAAO,KAAKN,Q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,UAAMG,YAAY,IAAIC,IAAI7B,OAAO8B,KAAK5D,KAAAA,EAAOgC,IAAI,CAACzD,QAAQA,IAAIgF,YAAW,CAAA,CAAA;AAGzE,QAAIM,YAAuD;MAAEZ,WAAW;MAAIa,YAAY;IAAE;AAE1F,eAAWb,aAAaQ,YAAY;AAClC,YAAMD,cAAcN,cAAcD,SAAAA;AAClC,YAAMa,aAAaN,YAAYlC,OAAO,CAACgC,UAAUI,UAAUK,IAAIT,KAAAA,CAAAA,EAAQlC;AACvE,UAAI0C,aAAaD,UAAUC,YAAY;AACrCD,oBAAY;UAAEZ;UAAWa;QAAW;MACtC;IACF;AACA,WAAOD,UAAUC,aAAa,IAAID,UAAUZ,YAAY;EAC1D;;;;;EAMA,MAAce,sBAAuE;AAEnF,UAAMC,MAAM,MAAMC,UAAUC,UAAS;AAErC,UAAMC,WAAWH,IAAII,KAAK,CAAChE,OAAAA;AACzB,aAAOA,GAAGiE,SAAS,KAAKjF,UAAUgB,GAAGkE,YAAY,KAAK7E;IACxD,CAAA;AAEA,QAAI0E,UAAU;AAEZ,YAAM/D,KAAK,UAAMmE,mBAAqB,KAAKnF,QAAQ,KAAKK,SAAS;AAEjE,YAAM+E,cAAcpE,GAAGqE,iBAAiBC,SAAS,KAAK/E,SAAS;AAE/D,UAAI6E,aAAa;AACf,eAAOpE;MACT,OAAO;AAGLA,WAAGuE,MAAK;MACV;IACF;EACF;;;;;;EAOA,MAAcxE,SAAYyE,UAAsF;AAE9G,UAAMxE,KAAK,MAAM,KAAK2D,oBAAmB;AACzC,QAAI3D,IAAI;AACN,UAAI;AAEF,eAAO,MAAMwE,SAASxE,EAAAA;MACxB,UAAA;AAEEA,WAAGuE,MAAK;MACV;IACF;AACA,WAAOnG;EACT;AACF;","names":["IndexedDbPayloadDivinerSchema","PayloadDivinerSchema","IndexedDbPayloadDivinerConfigSchema","IndexedDbPayloadDivinerSchema","import_diviner_payload_model","payloadValueFilter","key","value","undefined","payload","sourceValue","Array","isArray","containsAll","IndexedDbPayloadDiviner","PayloadDiviner","configSchemas","IndexedDbPayloadDivinerConfigSchema","defaultConfigSchema","_db","dbName","config","archivist","IndexedDbArchivist","defaultDbName","dbVersion","defaultDbVersion","storeName","defaultStoreName","divineHandler","payloads","query","find","isPayloadDivinerQueryPayload","result","tryUseDb","db","schemas","limit","offset","order","props","removeFields","tx","transaction","store","objectStore","results","parsedOffset","parsedLimit","assertEx","length","filterSchema","filter","schema","direction","suggestedIndex","selectBestIndex","keyRangeValue","getKeyRangeValue","valueFilters","Object","entries","map","exists","cursor","index","openCursor","IDBKeyRange","only","advance","every","push","continue","done","Promise","all","PayloadBuilder","build","startHandler","indexName","extractFields","slice","split","IndexSeparator","field","toLowerCase","indexFields","indexNames","queryKeys","Set","keys","bestMatch","matchCount","has","tryGetInitializedDb","dbs","indexedDB","databases","dbExists","some","name","version","openDB","storeExists","objectStoreNames","contains","close","callback"]}
|
package/dist/neutral/index.js
CHANGED
|
@@ -1,2 +1,197 @@
|
|
|
1
|
-
var
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
+
|
|
4
|
+
// src/Schema.ts
|
|
5
|
+
import { PayloadDivinerSchema } from "@xyo-network/diviner-payload-model";
|
|
6
|
+
var IndexedDbPayloadDivinerSchema = `${PayloadDivinerSchema}.indexeddb`;
|
|
7
|
+
|
|
8
|
+
// src/Config.ts
|
|
9
|
+
var IndexedDbPayloadDivinerConfigSchema = `${IndexedDbPayloadDivinerSchema}.config`;
|
|
10
|
+
|
|
11
|
+
// src/Diviner.ts
|
|
12
|
+
import { containsAll } from "@xylabs/array";
|
|
13
|
+
import { assertEx } from "@xylabs/assert";
|
|
14
|
+
import { exists } from "@xylabs/exists";
|
|
15
|
+
import { removeFields } from "@xylabs/object";
|
|
16
|
+
import { IndexedDbArchivist } from "@xyo-network/archivist-indexeddb";
|
|
17
|
+
import { IndexSeparator } from "@xyo-network/archivist-model";
|
|
18
|
+
import { PayloadDiviner } from "@xyo-network/diviner-payload-abstract";
|
|
19
|
+
import { isPayloadDivinerQueryPayload } from "@xyo-network/diviner-payload-model";
|
|
20
|
+
import { PayloadBuilder } from "@xyo-network/payload-builder";
|
|
21
|
+
import { openDB } from "idb";
|
|
22
|
+
var payloadValueFilter = /* @__PURE__ */ __name((key, value) => {
|
|
23
|
+
if (!value) return void 0;
|
|
24
|
+
return (payload) => {
|
|
25
|
+
if (!payload) return false;
|
|
26
|
+
const sourceValue = payload?.[key];
|
|
27
|
+
if (sourceValue === void 0) return false;
|
|
28
|
+
return Array.isArray(sourceValue) && Array.isArray(value) ? containsAll(sourceValue, value) : sourceValue == value;
|
|
29
|
+
};
|
|
30
|
+
}, "payloadValueFilter");
|
|
31
|
+
var IndexedDbPayloadDiviner = class extends PayloadDiviner {
|
|
32
|
+
static {
|
|
33
|
+
__name(this, "IndexedDbPayloadDiviner");
|
|
34
|
+
}
|
|
35
|
+
static configSchemas = [
|
|
36
|
+
...super.configSchemas,
|
|
37
|
+
IndexedDbPayloadDivinerConfigSchema
|
|
38
|
+
];
|
|
39
|
+
static defaultConfigSchema = IndexedDbPayloadDivinerConfigSchema;
|
|
40
|
+
_db;
|
|
41
|
+
/**
|
|
42
|
+
* The database name. If not supplied via config, it defaults
|
|
43
|
+
* to the archivist's name and if archivist's name is not supplied,
|
|
44
|
+
* it defaults to `archivist`. This behavior
|
|
45
|
+
* biases towards a single, isolated DB per archivist which seems to
|
|
46
|
+
* make the most sense for 99% of use cases.
|
|
47
|
+
*/
|
|
48
|
+
get dbName() {
|
|
49
|
+
return this.config?.dbName ?? this.config?.archivist ?? IndexedDbArchivist.defaultDbName;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* The database version. If not supplied via config, it defaults to the archivist default version.
|
|
53
|
+
*/
|
|
54
|
+
get dbVersion() {
|
|
55
|
+
return this.config?.dbVersion ?? IndexedDbArchivist.defaultDbVersion;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* The name of the object store. If not supplied via config, it defaults
|
|
59
|
+
* to `payloads`.
|
|
60
|
+
*/
|
|
61
|
+
get storeName() {
|
|
62
|
+
return this.config?.storeName ?? IndexedDbArchivist.defaultStoreName;
|
|
63
|
+
}
|
|
64
|
+
async divineHandler(payloads) {
|
|
65
|
+
const query = payloads?.find(isPayloadDivinerQueryPayload);
|
|
66
|
+
if (!query) return [];
|
|
67
|
+
const result = await this.tryUseDb(async (db) => {
|
|
68
|
+
const { schemas, limit, offset, order, ...props } = removeFields(query, [
|
|
69
|
+
"hash",
|
|
70
|
+
"schema",
|
|
71
|
+
"$meta",
|
|
72
|
+
"$hash"
|
|
73
|
+
]);
|
|
74
|
+
const tx = db.transaction(this.storeName, "readonly");
|
|
75
|
+
const store = tx.objectStore(this.storeName);
|
|
76
|
+
const results = [];
|
|
77
|
+
let parsedOffset = offset ?? 0;
|
|
78
|
+
const parsedLimit = limit ?? 10;
|
|
79
|
+
assertEx((schemas?.length ?? 1) === 1, () => "IndexedDbPayloadDiviner: Only one filter schema supported");
|
|
80
|
+
const filterSchema = schemas?.[0];
|
|
81
|
+
const filter = filterSchema ? {
|
|
82
|
+
schema: filterSchema,
|
|
83
|
+
...props
|
|
84
|
+
} : {
|
|
85
|
+
...props
|
|
86
|
+
};
|
|
87
|
+
const direction = order === "desc" ? "prev" : "next";
|
|
88
|
+
const suggestedIndex = this.selectBestIndex(filter, store);
|
|
89
|
+
const keyRangeValue = this.getKeyRangeValue(suggestedIndex, filter);
|
|
90
|
+
const valueFilters = props ? Object.entries(props).map(([key, value]) => payloadValueFilter(key, value)).filter(exists) : [];
|
|
91
|
+
let cursor = suggestedIndex ? (
|
|
92
|
+
// Conditionally filter on schemas
|
|
93
|
+
await store.index(suggestedIndex).openCursor(IDBKeyRange.only(keyRangeValue), direction)
|
|
94
|
+
) : await store.openCursor(suggestedIndex, direction);
|
|
95
|
+
while (cursor && parsedOffset > 0) {
|
|
96
|
+
cursor = await cursor.advance(parsedOffset);
|
|
97
|
+
parsedOffset = 0;
|
|
98
|
+
}
|
|
99
|
+
while (cursor && results.length < parsedLimit) {
|
|
100
|
+
const value = cursor.value;
|
|
101
|
+
if (value) {
|
|
102
|
+
if (valueFilters.length > 0) {
|
|
103
|
+
if (valueFilters.every((filter2) => filter2(value))) {
|
|
104
|
+
results.push(value);
|
|
105
|
+
}
|
|
106
|
+
} else {
|
|
107
|
+
results.push(value);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
try {
|
|
111
|
+
cursor = await cursor.continue();
|
|
112
|
+
} catch {
|
|
113
|
+
break;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
await tx.done;
|
|
117
|
+
return await Promise.all(results.map((payload) => PayloadBuilder.build(payload)));
|
|
118
|
+
});
|
|
119
|
+
return result ?? [];
|
|
120
|
+
}
|
|
121
|
+
async startHandler() {
|
|
122
|
+
await super.startHandler();
|
|
123
|
+
return true;
|
|
124
|
+
}
|
|
125
|
+
getKeyRangeValue(indexName, query) {
|
|
126
|
+
if (!indexName) return [];
|
|
127
|
+
const extractFields = /* @__PURE__ */ __name((indexName2) => {
|
|
128
|
+
return indexName2.slice(3).split(IndexSeparator).map((field) => field.toLowerCase());
|
|
129
|
+
}, "extractFields");
|
|
130
|
+
const indexFields = extractFields(indexName);
|
|
131
|
+
const keyRangeValue = indexFields.map((field) => query[field]);
|
|
132
|
+
return keyRangeValue.length === 1 ? keyRangeValue[0] : keyRangeValue;
|
|
133
|
+
}
|
|
134
|
+
selectBestIndex(query, store) {
|
|
135
|
+
const { indexNames } = store;
|
|
136
|
+
const extractFields = /* @__PURE__ */ __name((indexName) => {
|
|
137
|
+
return indexName.slice(3).split(IndexSeparator).map((field) => field.toLowerCase());
|
|
138
|
+
}, "extractFields");
|
|
139
|
+
const queryKeys = new Set(Object.keys(query).map((key) => key.toLowerCase()));
|
|
140
|
+
let bestMatch = {
|
|
141
|
+
indexName: "",
|
|
142
|
+
matchCount: 0
|
|
143
|
+
};
|
|
144
|
+
for (const indexName of indexNames) {
|
|
145
|
+
const indexFields = extractFields(indexName);
|
|
146
|
+
const matchCount = indexFields.filter((field) => queryKeys.has(field)).length;
|
|
147
|
+
if (matchCount > bestMatch.matchCount) {
|
|
148
|
+
bestMatch = {
|
|
149
|
+
indexName,
|
|
150
|
+
matchCount
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
return bestMatch.matchCount > 0 ? bestMatch.indexName : null;
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Checks that the desired DB/objectStore exists and is initialized to the correct version
|
|
158
|
+
* @returns The initialized DB or undefined if it does not exist in the desired state
|
|
159
|
+
*/
|
|
160
|
+
async tryGetInitializedDb() {
|
|
161
|
+
const dbs = await indexedDB.databases();
|
|
162
|
+
const dbExists = dbs.some((db) => {
|
|
163
|
+
return db.name === this.dbName && db.version === this.dbVersion;
|
|
164
|
+
});
|
|
165
|
+
if (dbExists) {
|
|
166
|
+
const db = await openDB(this.dbName, this.dbVersion);
|
|
167
|
+
const storeExists = db.objectStoreNames.contains(this.storeName);
|
|
168
|
+
if (storeExists) {
|
|
169
|
+
return db;
|
|
170
|
+
} else {
|
|
171
|
+
db.close();
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Executes a callback with the initialized DB and then closes the db
|
|
177
|
+
* @param callback The method to execute with the initialized DB
|
|
178
|
+
* @returns
|
|
179
|
+
*/
|
|
180
|
+
async tryUseDb(callback) {
|
|
181
|
+
const db = await this.tryGetInitializedDb();
|
|
182
|
+
if (db) {
|
|
183
|
+
try {
|
|
184
|
+
return await callback(db);
|
|
185
|
+
} finally {
|
|
186
|
+
db.close();
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
return void 0;
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
export {
|
|
193
|
+
IndexedDbPayloadDiviner,
|
|
194
|
+
IndexedDbPayloadDivinerConfigSchema,
|
|
195
|
+
IndexedDbPayloadDivinerSchema
|
|
196
|
+
};
|
|
2
197
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/Schema.ts","../../src/Config.ts","../../src/Diviner.ts"],"sourcesContent":["import { PayloadDivinerSchema } from '@xyo-network/diviner-payload-model'\n\nexport const IndexedDbPayloadDivinerSchema = `${PayloadDivinerSchema}.indexeddb` as const\nexport type IndexedDbPayloadDivinerSchema = typeof IndexedDbPayloadDivinerSchema\n","import { IndexDescription } from '@xyo-network/archivist-model'\nimport { DivinerConfig } from '@xyo-network/diviner-model'\n\nimport { IndexedDbPayloadDivinerSchema } from './Schema'\n\nexport const IndexedDbPayloadDivinerConfigSchema = `${IndexedDbPayloadDivinerSchema}.config` as const\nexport type IndexedDbPayloadDivinerConfigSchema = typeof IndexedDbPayloadDivinerConfigSchema\n\nexport type IndexedDbPayloadDivinerConfig = DivinerConfig<{\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: IndexedDbPayloadDivinerConfigSchema\n /**\n * The storage configuration\n * // TODO: Hoist to main diviner config\n */\n storage?: {\n /**\n * The indexes to create on the object store\n */\n indexes?: IndexDescription[]\n }\n /**\n * The name of the object store\n */\n storeName?: string\n}>\n","import { containsAll } from '@xylabs/array'\nimport { assertEx } from '@xylabs/assert'\nimport { exists } from '@xylabs/exists'\nimport { Hash } from '@xylabs/hex'\nimport { AnyObject, removeFields } from '@xylabs/object'\nimport { IndexedDbArchivist } from '@xyo-network/archivist-indexeddb'\nimport { IndexSeparator } from '@xyo-network/archivist-model'\nimport { DivinerInstance, DivinerModuleEventData } from '@xyo-network/diviner-model'\nimport { PayloadDiviner } from '@xyo-network/diviner-payload-abstract'\nimport { isPayloadDivinerQueryPayload, PayloadDivinerQueryPayload } from '@xyo-network/diviner-payload-model'\nimport { PayloadBuilder } from '@xyo-network/payload-builder'\nimport { Payload, Schema, WithMeta } from '@xyo-network/payload-model'\nimport { IDBPDatabase, IDBPObjectStore, openDB } from 'idb'\n\nimport { IndexedDbPayloadDivinerConfigSchema } from './Config'\nimport { IndexedDbPayloadDivinerParams } from './Params'\n\ninterface PayloadStore {\n [s: string]: Payload\n}\n\ntype AnyPayload = Payload<Record<string, unknown>>\n\ntype ValueFilter = (payload?: AnyPayload | null) => boolean\n\nconst payloadValueFilter = (key: keyof AnyPayload, value?: unknown | unknown[]): ValueFilter | undefined => {\n if (!value) return undefined\n return (payload) => {\n if (!payload) return false\n const sourceValue = payload?.[key]\n if (sourceValue === undefined) return false\n return Array.isArray(sourceValue) && Array.isArray(value) ? containsAll(sourceValue, value) : sourceValue == value\n }\n}\n\nexport class IndexedDbPayloadDiviner<\n TParams extends IndexedDbPayloadDivinerParams = IndexedDbPayloadDivinerParams,\n TIn extends PayloadDivinerQueryPayload = PayloadDivinerQueryPayload,\n TOut extends Payload = Payload,\n TEventData extends DivinerModuleEventData<DivinerInstance<TParams, TIn, TOut>, TIn, TOut> = DivinerModuleEventData<\n DivinerInstance<TParams, TIn, TOut>,\n TIn,\n TOut\n >,\n> extends PayloadDiviner<TParams, TIn, TOut, TEventData> {\n static override readonly configSchemas: Schema[] = [...super.configSchemas, IndexedDbPayloadDivinerConfigSchema]\n static override readonly defaultConfigSchema: Schema = IndexedDbPayloadDivinerConfigSchema\n\n private _db: IDBPDatabase<PayloadStore> | undefined\n\n /**\n * The database name. If not supplied via config, it defaults\n * to the archivist's name and if archivist's name is not supplied,\n * 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?.archivist ?? IndexedDbArchivist.defaultDbName\n }\n\n /**\n * The database version. If not supplied via config, it defaults to the archivist default version.\n */\n get dbVersion() {\n return this.config?.dbVersion ?? IndexedDbArchivist.defaultDbVersion\n }\n\n /**\n * The name of the object store. If not supplied via config, it defaults\n * to `payloads`.\n */\n get storeName() {\n return this.config?.storeName ?? IndexedDbArchivist.defaultStoreName\n }\n\n protected override async divineHandler(payloads?: TIn[]): Promise<TOut[]> {\n const query = payloads?.find(isPayloadDivinerQueryPayload) as TIn\n if (!query) return []\n const result = await this.tryUseDb(async (db) => {\n const { schemas, limit, offset, order, ...props } = removeFields(query as unknown as WithMeta<TIn> & { sources?: Hash[] }, [\n 'hash',\n 'schema',\n '$meta',\n '$hash',\n ])\n const tx = db.transaction(this.storeName, 'readonly')\n const store = tx.objectStore(this.storeName)\n const results: TOut[] = []\n let parsedOffset = offset ?? 0\n const parsedLimit = limit ?? 10\n assertEx((schemas?.length ?? 1) === 1, () => 'IndexedDbPayloadDiviner: Only one filter schema supported')\n const filterSchema = schemas?.[0]\n const filter = filterSchema ? { schema: filterSchema, ...props } : { ...props }\n const direction: IDBCursorDirection = order === 'desc' ? 'prev' : 'next'\n const suggestedIndex = this.selectBestIndex(filter, store)\n const keyRangeValue = this.getKeyRangeValue(suggestedIndex, filter)\n const valueFilters: ValueFilter[] =\n props ?\n Object.entries(props)\n .map(([key, value]) => payloadValueFilter(key, value))\n .filter(exists)\n : []\n let cursor =\n suggestedIndex ?\n // Conditionally filter on schemas\n await store.index(suggestedIndex).openCursor(IDBKeyRange.only(keyRangeValue), direction)\n // Just iterate all records\n : await store.openCursor(suggestedIndex, direction)\n\n // Skip records until the offset is reached\n while (cursor && parsedOffset > 0) {\n cursor = await cursor.advance(parsedOffset)\n parsedOffset = 0 // Reset offset after skipping\n }\n // Collect results up to the limit\n while (cursor && results.length < parsedLimit) {\n const value = cursor.value\n if (value) {\n // If we're filtering on more than just the schema\n if (valueFilters.length > 0) {\n // Ensure all filters pass\n if (valueFilters.every((filter) => filter(value))) {\n // Then save the value\n results.push(value)\n }\n } else {\n // Otherwise just save the value\n results.push(value)\n }\n }\n try {\n cursor = await cursor.continue()\n } catch {\n break\n }\n }\n await tx.done\n // Remove any metadata before returning to the client\n return await Promise.all(results.map((payload) => PayloadBuilder.build(payload)))\n })\n return result ?? []\n }\n\n protected override async startHandler() {\n await super.startHandler()\n return true\n }\n\n private getKeyRangeValue(indexName: string | null, query: AnyObject): unknown | unknown[] {\n if (!indexName) return []\n // Function to extract fields from an index name\n const extractFields = (indexName: string): string[] => {\n return indexName\n .slice(3)\n .split(IndexSeparator)\n .map((field) => field.toLowerCase())\n }\n\n // Extracting the relevant fields from the index name\n const indexFields = extractFields(indexName)\n\n // Collecting the values for these fields from the query object\n const keyRangeValue = indexFields.map((field) => query[field as keyof AnyObject])\n return keyRangeValue.length === 1 ? keyRangeValue[0] : keyRangeValue\n }\n\n private selectBestIndex(query: AnyObject, store: IDBPObjectStore<PayloadStore>): string | null {\n // List of available indexes\n const { indexNames } = store\n\n // Function to extract fields from an index name\n const extractFields = (indexName: string): string[] => {\n return indexName\n .slice(3)\n .split(IndexSeparator)\n .map((field) => field.toLowerCase())\n }\n\n // Convert query object keys to a set for easier comparison\n const queryKeys = new Set(Object.keys(query).map((key) => key.toLowerCase()))\n\n // Find the best matching index\n let bestMatch: { indexName: string; matchCount: number } = { indexName: '', matchCount: 0 }\n\n for (const indexName of indexNames) {\n const indexFields = extractFields(indexName)\n const matchCount = indexFields.filter((field) => queryKeys.has(field)).length\n if (matchCount > bestMatch.matchCount) {\n bestMatch = { indexName, matchCount }\n }\n }\n return bestMatch.matchCount > 0 ? bestMatch.indexName : null\n }\n\n /**\n * Checks that the desired DB/objectStore exists and is initialized to the correct version\n * @returns The initialized DB or undefined if it does not exist in the desired state\n */\n private async tryGetInitializedDb(): Promise<IDBPDatabase<PayloadStore> | undefined> {\n // Enumerate the DBs\n const dbs = await indexedDB.databases()\n // Check that the DB exists at the desired version\n const dbExists = dbs.some((db) => {\n return db.name === this.dbName && db.version === this.dbVersion\n })\n // If the DB exists at the desired version\n if (dbExists) {\n // If the db does exist, open it\n const db = await openDB<PayloadStore>(this.dbName, this.dbVersion)\n // Check that the desired objectStore exists\n const storeExists = db.objectStoreNames.contains(this.storeName)\n // If the correct db/version/objectStore exists\n if (storeExists) {\n return db\n } else {\n // Otherwise close the db so the process that is going to update the\n // db can open it\n db.close()\n }\n }\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 tryUseDb<T>(callback: (db: IDBPDatabase<PayloadStore>) => Promise<T> | T): Promise<T | undefined> {\n // Get the initialized DB\n const db = await this.tryGetInitializedDb()\n if (db) {\n try {\n // Perform the callback\n return await callback(db)\n } finally {\n // Close the DB\n db.close()\n }\n }\n return undefined\n }\n}\n"],"mappings":"+EAAA,OAASA,wBAAAA,MAA4B,qCAE9B,IAAMC,EAAgC,GAAGD,CAAAA,aCGzC,IAAME,EAAsC,GAAGC,CAAAA,UCLtD,OAASC,eAAAA,MAAmB,gBAC5B,OAASC,YAAAA,MAAgB,iBACzB,OAASC,UAAAA,MAAc,iBAEvB,OAAoBC,gBAAAA,MAAoB,iBACxC,OAASC,sBAAAA,MAA0B,mCACnC,OAASC,kBAAAA,MAAsB,+BAE/B,OAASC,kBAAAA,MAAsB,wCAC/B,OAASC,gCAAAA,MAAgE,qCACzE,OAASC,kBAAAA,MAAsB,+BAE/B,OAAwCC,UAAAA,MAAc,MAatD,IAAMC,EAAqBC,EAAA,CAACC,EAAuBC,IAAAA,CACjD,GAAKA,EACL,OAAQC,GAAAA,CACN,GAAI,CAACA,EAAS,MAAO,GACrB,IAAMC,EAAcD,IAAUF,CAAAA,EAC9B,OAAIG,IAAgBC,OAAkB,GAC/BC,MAAMC,QAAQH,CAAAA,GAAgBE,MAAMC,QAAQL,CAAAA,EAASM,EAAYJ,EAAaF,CAAAA,EAASE,GAAeF,CAC/G,CACF,EAR2B,sBAUdO,EAAN,cASGC,CAAAA,CA5CV,MA4CUA,CAAAA,EAAAA,gCACR,OAAyBC,cAA0B,IAAI,MAAMA,cAAeC,GAC5E,OAAyBC,oBAA8BD,EAE/CE,IASR,IAAIC,QAAS,CACX,OAAO,KAAKC,QAAQD,QAAU,KAAKC,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E1F,QAAWZ,KAAaM,EAAY,CAElC,IAAMM,EADcL,EAAcP,CAAAA,EACH3B,OAAQ+B,GAAUI,EAAUK,IAAIT,CAAAA,CAAAA,EAAQjC,OACnEyC,EAAaD,EAAUC,aACzBD,EAAY,CAAEX,UAAAA,EAAWY,WAAAA,CAAW,EAExC,CACA,OAAOD,EAAUC,WAAa,EAAID,EAAUX,UAAY,IAC1D,CAMA,MAAcc,qBAAuE,CAQnF,IANY,MAAMC,UAAUC,UAAS,GAEhBC,KAAM7D,GAClBA,EAAG8D,OAAS,KAAK7E,QAAUe,EAAG+D,UAAY,KAAKzE,SACxD,EAEc,CAEZ,IAAMU,EAAK,MAAMgE,EAAqB,KAAK/E,OAAQ,KAAKK,SAAS,EAIjE,GAFoBU,EAAGiE,iBAAiBC,SAAS,KAAK1E,SAAS,EAG7D,OAAOQ,EAIPA,EAAGmE,MAAK,CAEZ,CACF,CAOA,MAAcpE,SAAYqE,EAAsF,CAE9G,IAAMpE,EAAK,MAAM,KAAK0D,oBAAmB,EACzC,GAAI1D,EACF,GAAI,CAEF,OAAO,MAAMoE,EAASpE,CAAAA,CACxB,QAAA,CAEEA,EAAGmE,MAAK,CACV,CAGJ,CACF","names":["PayloadDivinerSchema","IndexedDbPayloadDivinerSchema","IndexedDbPayloadDivinerConfigSchema","IndexedDbPayloadDivinerSchema","containsAll","assertEx","exists","removeFields","IndexedDbArchivist","IndexSeparator","PayloadDiviner","isPayloadDivinerQueryPayload","PayloadBuilder","openDB","payloadValueFilter","__name","key","value","payload","sourceValue","undefined","Array","isArray","containsAll","IndexedDbPayloadDiviner","PayloadDiviner","configSchemas","IndexedDbPayloadDivinerConfigSchema","defaultConfigSchema","_db","dbName","config","archivist","IndexedDbArchivist","defaultDbName","dbVersion","defaultDbVersion","storeName","defaultStoreName","divineHandler","payloads","query","find","isPayloadDivinerQueryPayload","tryUseDb","db","schemas","limit","offset","order","props","removeFields","tx","transaction","store","objectStore","results","parsedOffset","parsedLimit","assertEx","length","filterSchema","filter","schema","direction","suggestedIndex","selectBestIndex","keyRangeValue","getKeyRangeValue","valueFilters","Object","entries","map","exists","cursor","index","openCursor","IDBKeyRange","only","advance","every","push","continue","done","Promise","all","PayloadBuilder","build","startHandler","indexName","slice","split","IndexSeparator","field","toLowerCase","indexNames","extractFields","queryKeys","Set","keys","bestMatch","matchCount","has","tryGetInitializedDb","indexedDB","databases","some","name","version","openDB","objectStoreNames","contains","close","callback"]}
|
|
1
|
+
{"version":3,"sources":["../../src/Schema.ts","../../src/Config.ts","../../src/Diviner.ts"],"sourcesContent":["import { PayloadDivinerSchema } from '@xyo-network/diviner-payload-model'\n\nexport const IndexedDbPayloadDivinerSchema = `${PayloadDivinerSchema}.indexeddb` as const\nexport type IndexedDbPayloadDivinerSchema = typeof IndexedDbPayloadDivinerSchema\n","import { IndexDescription } from '@xyo-network/archivist-model'\nimport { DivinerConfig } from '@xyo-network/diviner-model'\n\nimport { IndexedDbPayloadDivinerSchema } from './Schema'\n\nexport const IndexedDbPayloadDivinerConfigSchema = `${IndexedDbPayloadDivinerSchema}.config` as const\nexport type IndexedDbPayloadDivinerConfigSchema = typeof IndexedDbPayloadDivinerConfigSchema\n\nexport type IndexedDbPayloadDivinerConfig = DivinerConfig<{\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: IndexedDbPayloadDivinerConfigSchema\n /**\n * The storage configuration\n * // TODO: Hoist to main diviner config\n */\n storage?: {\n /**\n * The indexes to create on the object store\n */\n indexes?: IndexDescription[]\n }\n /**\n * The name of the object store\n */\n storeName?: string\n}>\n","import { containsAll } from '@xylabs/array'\nimport { assertEx } from '@xylabs/assert'\nimport { exists } from '@xylabs/exists'\nimport { Hash } from '@xylabs/hex'\nimport { AnyObject, removeFields } from '@xylabs/object'\nimport { IndexedDbArchivist } from '@xyo-network/archivist-indexeddb'\nimport { IndexSeparator } from '@xyo-network/archivist-model'\nimport { DivinerInstance, DivinerModuleEventData } from '@xyo-network/diviner-model'\nimport { PayloadDiviner } from '@xyo-network/diviner-payload-abstract'\nimport { isPayloadDivinerQueryPayload, PayloadDivinerQueryPayload } from '@xyo-network/diviner-payload-model'\nimport { PayloadBuilder } from '@xyo-network/payload-builder'\nimport { Payload, Schema, WithMeta } from '@xyo-network/payload-model'\nimport { IDBPDatabase, IDBPObjectStore, openDB } from 'idb'\n\nimport { IndexedDbPayloadDivinerConfigSchema } from './Config'\nimport { IndexedDbPayloadDivinerParams } from './Params'\n\ninterface PayloadStore {\n [s: string]: Payload\n}\n\ntype AnyPayload = Payload<Record<string, unknown>>\n\ntype ValueFilter = (payload?: AnyPayload | null) => boolean\n\nconst payloadValueFilter = (key: keyof AnyPayload, value?: unknown | unknown[]): ValueFilter | undefined => {\n if (!value) return undefined\n return (payload) => {\n if (!payload) return false\n const sourceValue = payload?.[key]\n if (sourceValue === undefined) return false\n return Array.isArray(sourceValue) && Array.isArray(value) ? containsAll(sourceValue, value) : sourceValue == value\n }\n}\n\nexport class IndexedDbPayloadDiviner<\n TParams extends IndexedDbPayloadDivinerParams = IndexedDbPayloadDivinerParams,\n TIn extends PayloadDivinerQueryPayload = PayloadDivinerQueryPayload,\n TOut extends Payload = Payload,\n TEventData extends DivinerModuleEventData<DivinerInstance<TParams, TIn, TOut>, TIn, TOut> = DivinerModuleEventData<\n DivinerInstance<TParams, TIn, TOut>,\n TIn,\n TOut\n >,\n> extends PayloadDiviner<TParams, TIn, TOut, TEventData> {\n static override readonly configSchemas: Schema[] = [...super.configSchemas, IndexedDbPayloadDivinerConfigSchema]\n static override readonly defaultConfigSchema: Schema = IndexedDbPayloadDivinerConfigSchema\n\n private _db: IDBPDatabase<PayloadStore> | undefined\n\n /**\n * The database name. If not supplied via config, it defaults\n * to the archivist's name and if archivist's name is not supplied,\n * 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?.archivist ?? IndexedDbArchivist.defaultDbName\n }\n\n /**\n * The database version. If not supplied via config, it defaults to the archivist default version.\n */\n get dbVersion() {\n return this.config?.dbVersion ?? IndexedDbArchivist.defaultDbVersion\n }\n\n /**\n * The name of the object store. If not supplied via config, it defaults\n * to `payloads`.\n */\n get storeName() {\n return this.config?.storeName ?? IndexedDbArchivist.defaultStoreName\n }\n\n protected override async divineHandler(payloads?: TIn[]): Promise<TOut[]> {\n const query = payloads?.find(isPayloadDivinerQueryPayload) as TIn\n if (!query) return []\n const result = await this.tryUseDb(async (db) => {\n const { schemas, limit, offset, order, ...props } = removeFields(query as unknown as WithMeta<TIn> & { sources?: Hash[] }, [\n 'hash',\n 'schema',\n '$meta',\n '$hash',\n ])\n const tx = db.transaction(this.storeName, 'readonly')\n const store = tx.objectStore(this.storeName)\n const results: TOut[] = []\n let parsedOffset = offset ?? 0\n const parsedLimit = limit ?? 10\n assertEx((schemas?.length ?? 1) === 1, () => 'IndexedDbPayloadDiviner: Only one filter schema supported')\n const filterSchema = schemas?.[0]\n const filter = filterSchema ? { schema: filterSchema, ...props } : { ...props }\n const direction: IDBCursorDirection = order === 'desc' ? 'prev' : 'next'\n const suggestedIndex = this.selectBestIndex(filter, store)\n const keyRangeValue = this.getKeyRangeValue(suggestedIndex, filter)\n const valueFilters: ValueFilter[] =\n props ?\n Object.entries(props)\n .map(([key, value]) => payloadValueFilter(key, value))\n .filter(exists)\n : []\n let cursor =\n suggestedIndex ?\n // Conditionally filter on schemas\n await store.index(suggestedIndex).openCursor(IDBKeyRange.only(keyRangeValue), direction)\n // Just iterate all records\n : await store.openCursor(suggestedIndex, direction)\n\n // Skip records until the offset is reached\n while (cursor && parsedOffset > 0) {\n cursor = await cursor.advance(parsedOffset)\n parsedOffset = 0 // Reset offset after skipping\n }\n // Collect results up to the limit\n while (cursor && results.length < parsedLimit) {\n const value = cursor.value\n if (value) {\n // If we're filtering on more than just the schema\n if (valueFilters.length > 0) {\n // Ensure all filters pass\n if (valueFilters.every((filter) => filter(value))) {\n // Then save the value\n results.push(value)\n }\n } else {\n // Otherwise just save the value\n results.push(value)\n }\n }\n try {\n cursor = await cursor.continue()\n } catch {\n break\n }\n }\n await tx.done\n // Remove any metadata before returning to the client\n return await Promise.all(results.map((payload) => PayloadBuilder.build(payload)))\n })\n return result ?? []\n }\n\n protected override async startHandler() {\n await super.startHandler()\n return true\n }\n\n private getKeyRangeValue(indexName: string | null, query: AnyObject): unknown | unknown[] {\n if (!indexName) return []\n // Function to extract fields from an index name\n const extractFields = (indexName: string): string[] => {\n return indexName\n .slice(3)\n .split(IndexSeparator)\n .map((field) => field.toLowerCase())\n }\n\n // Extracting the relevant fields from the index name\n const indexFields = extractFields(indexName)\n\n // Collecting the values for these fields from the query object\n const keyRangeValue = indexFields.map((field) => query[field as keyof AnyObject])\n return keyRangeValue.length === 1 ? keyRangeValue[0] : keyRangeValue\n }\n\n private selectBestIndex(query: AnyObject, store: IDBPObjectStore<PayloadStore>): string | null {\n // List of available indexes\n const { indexNames } = store\n\n // Function to extract fields from an index name\n const extractFields = (indexName: string): string[] => {\n return indexName\n .slice(3)\n .split(IndexSeparator)\n .map((field) => field.toLowerCase())\n }\n\n // Convert query object keys to a set for easier comparison\n const queryKeys = new Set(Object.keys(query).map((key) => key.toLowerCase()))\n\n // Find the best matching index\n let bestMatch: { indexName: string; matchCount: number } = { indexName: '', matchCount: 0 }\n\n for (const indexName of indexNames) {\n const indexFields = extractFields(indexName)\n const matchCount = indexFields.filter((field) => queryKeys.has(field)).length\n if (matchCount > bestMatch.matchCount) {\n bestMatch = { indexName, matchCount }\n }\n }\n return bestMatch.matchCount > 0 ? bestMatch.indexName : null\n }\n\n /**\n * Checks that the desired DB/objectStore exists and is initialized to the correct version\n * @returns The initialized DB or undefined if it does not exist in the desired state\n */\n private async tryGetInitializedDb(): Promise<IDBPDatabase<PayloadStore> | undefined> {\n // Enumerate the DBs\n const dbs = await indexedDB.databases()\n // Check that the DB exists at the desired version\n const dbExists = dbs.some((db) => {\n return db.name === this.dbName && db.version === this.dbVersion\n })\n // If the DB exists at the desired version\n if (dbExists) {\n // If the db does exist, open it\n const db = await openDB<PayloadStore>(this.dbName, this.dbVersion)\n // Check that the desired objectStore exists\n const storeExists = db.objectStoreNames.contains(this.storeName)\n // If the correct db/version/objectStore exists\n if (storeExists) {\n return db\n } else {\n // Otherwise close the db so the process that is going to update the\n // db can open it\n db.close()\n }\n }\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 tryUseDb<T>(callback: (db: IDBPDatabase<PayloadStore>) => Promise<T> | T): Promise<T | undefined> {\n // Get the initialized DB\n const db = await this.tryGetInitializedDb()\n if (db) {\n try {\n // Perform the callback\n return await callback(db)\n } finally {\n // Close the DB\n db.close()\n }\n }\n return undefined\n }\n}\n"],"mappings":";;;;AAAA,SAASA,4BAA4B;AAE9B,IAAMC,gCAAgC,GAAGD,oBAAAA;;;ACGzC,IAAME,sCAAsC,GAAGC,6BAAAA;;;ACLtD,SAASC,mBAAmB;AAC5B,SAASC,gBAAgB;AACzB,SAASC,cAAc;AAEvB,SAAoBC,oBAAoB;AACxC,SAASC,0BAA0B;AACnC,SAASC,sBAAsB;AAE/B,SAASC,sBAAsB;AAC/B,SAASC,oCAAgE;AACzE,SAASC,sBAAsB;AAE/B,SAAwCC,cAAc;AAatD,IAAMC,qBAAqB,wBAACC,KAAuBC,UAAAA;AACjD,MAAI,CAACA,MAAO,QAAOC;AACnB,SAAO,CAACC,YAAAA;AACN,QAAI,CAACA,QAAS,QAAO;AACrB,UAAMC,cAAcD,UAAUH,GAAAA;AAC9B,QAAII,gBAAgBF,OAAW,QAAO;AACtC,WAAOG,MAAMC,QAAQF,WAAAA,KAAgBC,MAAMC,QAAQL,KAAAA,IAASM,YAAYH,aAAaH,KAAAA,IAASG,eAAeH;EAC/G;AACF,GAR2B;AAUpB,IAAMO,0BAAN,cASGC,eAAAA;EA5CV,OA4CUA;;;EACR,OAAyBC,gBAA0B;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AI;AACN,UAAI;AAEF,eAAO,MAAMwE,SAASxE,EAAAA;MACxB,UAAA;AAEEA,WAAGuE,MAAK;MACV;IACF;AACA,WAAOnG;EACT;AACF;","names":["PayloadDivinerSchema","IndexedDbPayloadDivinerSchema","IndexedDbPayloadDivinerConfigSchema","IndexedDbPayloadDivinerSchema","containsAll","assertEx","exists","removeFields","IndexedDbArchivist","IndexSeparator","PayloadDiviner","isPayloadDivinerQueryPayload","PayloadBuilder","openDB","payloadValueFilter","key","value","undefined","payload","sourceValue","Array","isArray","containsAll","IndexedDbPayloadDiviner","PayloadDiviner","configSchemas","IndexedDbPayloadDivinerConfigSchema","defaultConfigSchema","_db","dbName","config","archivist","IndexedDbArchivist","defaultDbName","dbVersion","defaultDbVersion","storeName","defaultStoreName","divineHandler","payloads","query","find","isPayloadDivinerQueryPayload","result","tryUseDb","db","schemas","limit","offset","order","props","removeFields","tx","transaction","store","objectStore","results","parsedOffset","parsedLimit","assertEx","length","filterSchema","filter","schema","direction","suggestedIndex","selectBestIndex","keyRangeValue","getKeyRangeValue","valueFilters","Object","entries","map","exists","cursor","index","openCursor","IDBKeyRange","only","advance","every","push","continue","done","Promise","all","PayloadBuilder","build","startHandler","indexName","extractFields","slice","split","IndexSeparator","field","toLowerCase","indexFields","indexNames","queryKeys","Set","keys","bestMatch","matchCount","has","tryGetInitializedDb","dbs","indexedDB","databases","dbExists","some","name","version","openDB","storeExists","objectStoreNames","contains","close","callback"]}
|