@xyo-network/diviner-payload-indexeddb 3.0.6 → 3.0.8

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.
@@ -0,0 +1,14 @@
1
+ import type { IndexDescription } from '@xyo-network/archivist-model';
2
+ import type { DivinerConfig } from '@xyo-network/diviner-model';
3
+ export declare const IndexedDbPayloadDivinerConfigSchema: "network.xyo.diviner.payload.indexeddb.config";
4
+ export type IndexedDbPayloadDivinerConfigSchema = typeof IndexedDbPayloadDivinerConfigSchema;
5
+ export type IndexedDbPayloadDivinerConfig = DivinerConfig<{
6
+ dbName?: string;
7
+ dbVersion?: number;
8
+ schema: IndexedDbPayloadDivinerConfigSchema;
9
+ storage?: {
10
+ indexes?: IndexDescription[];
11
+ };
12
+ storeName?: string;
13
+ }>;
14
+ //# sourceMappingURL=Config.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Config.d.ts","sourceRoot":"","sources":["../../src/Config.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,8BAA8B,CAAA;AACpE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAA;AAI/D,eAAO,MAAM,mCAAmC,gDAAqD,CAAA;AACrG,MAAM,MAAM,mCAAmC,GAAG,OAAO,mCAAmC,CAAA;AAE5F,MAAM,MAAM,6BAA6B,GAAG,aAAa,CAAC;IAIxD,MAAM,CAAC,EAAE,MAAM,CAAA;IAIf,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,MAAM,EAAE,mCAAmC,CAAA;IAK3C,OAAO,CAAC,EAAE;QAIR,OAAO,CAAC,EAAE,gBAAgB,EAAE,CAAA;KAC7B,CAAA;IAID,SAAS,CAAC,EAAE,MAAM,CAAA;CACnB,CAAC,CAAA"}
@@ -0,0 +1,20 @@
1
+ import type { DivinerInstance, DivinerModuleEventData } from '@xyo-network/diviner-model';
2
+ import { PayloadDiviner } from '@xyo-network/diviner-payload-abstract';
3
+ import type { PayloadDivinerQueryPayload } from '@xyo-network/diviner-payload-model';
4
+ import type { Payload, Schema } from '@xyo-network/payload-model';
5
+ import type { IndexedDbPayloadDivinerParams } from './Params.ts';
6
+ export declare class IndexedDbPayloadDiviner<TParams extends IndexedDbPayloadDivinerParams = IndexedDbPayloadDivinerParams, TIn extends PayloadDivinerQueryPayload = PayloadDivinerQueryPayload, TOut extends Payload = Payload, TEventData extends DivinerModuleEventData<DivinerInstance<TParams, TIn, TOut>, TIn, TOut> = DivinerModuleEventData<DivinerInstance<TParams, TIn, TOut>, TIn, TOut>> extends PayloadDiviner<TParams, TIn, TOut, TEventData> {
7
+ static readonly configSchemas: Schema[];
8
+ static readonly defaultConfigSchema: Schema;
9
+ private _db;
10
+ get dbName(): string;
11
+ get dbVersion(): number;
12
+ get storeName(): string;
13
+ protected divineHandler(payloads?: TIn[]): Promise<TOut[]>;
14
+ protected startHandler(): Promise<boolean>;
15
+ private getKeyRangeValue;
16
+ private selectBestIndex;
17
+ private tryGetInitializedDb;
18
+ private tryUseDb;
19
+ }
20
+ //# sourceMappingURL=Diviner.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Diviner.d.ts","sourceRoot":"","sources":["../../src/Diviner.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,eAAe,EAAE,sBAAsB,EAAE,MAAM,4BAA4B,CAAA;AACzF,OAAO,EAAE,cAAc,EAAE,MAAM,uCAAuC,CAAA;AACtE,OAAO,KAAK,EAAE,0BAA0B,EAAE,MAAM,oCAAoC,CAAA;AAGpF,OAAO,KAAK,EACV,OAAO,EAAE,MAAM,EAChB,MAAM,4BAA4B,CAAA;AAKnC,OAAO,KAAK,EAAE,6BAA6B,EAAE,MAAM,aAAa,CAAA;AA4BhE,qBAAa,uBAAuB,CAClC,OAAO,SAAS,6BAA6B,GAAG,6BAA6B,EAC7E,GAAG,SAAS,0BAA0B,GAAG,0BAA0B,EACnE,IAAI,SAAS,OAAO,GAAG,OAAO,EAC9B,UAAU,SAAS,sBAAsB,CAAC,eAAe,CAAC,OAAO,EAAE,GAAG,EAAE,IAAI,CAAC,EAAE,GAAG,EAAE,IAAI,CAAC,GAAG,sBAAsB,CAChH,eAAe,CAAC,OAAO,EAAE,GAAG,EAAE,IAAI,CAAC,EACnC,GAAG,EACH,IAAI,CACL,CACD,SAAQ,cAAc,CAAC,OAAO,EAAE,GAAG,EAAE,IAAI,EAAE,UAAU,CAAC;IACtD,gBAAyB,aAAa,EAAE,MAAM,EAAE,CAAgE;IAChH,gBAAyB,mBAAmB,EAAE,MAAM,CAAsC;IAE1F,OAAO,CAAC,GAAG,CAAwC;IASnD,IAAI,MAAM,WAET;IAKD,IAAI,SAAS,WAEZ;IAMD,IAAI,SAAS,WAEZ;cAEwB,aAAa,CAAC,QAAQ,CAAC,EAAE,GAAG,EAAE,GAAG,OAAO,CAAC,IAAI,EAAE,CAAC;cAsEhD,YAAY;IAKrC,OAAO,CAAC,gBAAgB;IAWxB,OAAO,CAAC,eAAe;YAwBT,mBAAmB;YA6BnB,QAAQ;CAcvB"}
@@ -0,0 +1,5 @@
1
+ import type { DivinerParams } from '@xyo-network/diviner-model';
2
+ import type { AnyConfigSchema } from '@xyo-network/module-model';
3
+ import type { IndexedDbPayloadDivinerConfig } from './Config.ts';
4
+ export type IndexedDbPayloadDivinerParams = DivinerParams<AnyConfigSchema<IndexedDbPayloadDivinerConfig>>;
5
+ //# sourceMappingURL=Params.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Params.d.ts","sourceRoot":"","sources":["../../src/Params.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAA;AAC/D,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,2BAA2B,CAAA;AAEhE,OAAO,KAAK,EAAE,6BAA6B,EAAE,MAAM,aAAa,CAAA;AAEhE,MAAM,MAAM,6BAA6B,GAAG,aAAa,CAAC,eAAe,CAAC,6BAA6B,CAAC,CAAC,CAAA"}
@@ -0,0 +1,3 @@
1
+ export declare const IndexedDbPayloadDivinerSchema: "network.xyo.diviner.payload.indexeddb";
2
+ export type IndexedDbPayloadDivinerSchema = typeof IndexedDbPayloadDivinerSchema;
3
+ //# sourceMappingURL=Schema.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Schema.d.ts","sourceRoot":"","sources":["../../src/Schema.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,6BAA6B,yCAA+C,CAAA;AACzF,MAAM,MAAM,6BAA6B,GAAG,OAAO,6BAA6B,CAAA"}
@@ -1,40 +1,5 @@
1
- import { IndexDescription } from '@xyo-network/archivist-model';
2
- import { DivinerConfig, DivinerParams, DivinerModuleEventData, DivinerInstance } from '@xyo-network/diviner-model';
3
- import { PayloadDiviner } from '@xyo-network/diviner-payload-abstract';
4
- import { PayloadDivinerQueryPayload } from '@xyo-network/diviner-payload-model';
5
- import { Payload, Schema } from '@xyo-network/payload-model';
6
- import { AnyConfigSchema } from '@xyo-network/module-model';
7
-
8
- declare const IndexedDbPayloadDivinerConfigSchema: "network.xyo.diviner.payload.indexeddb.config";
9
- type IndexedDbPayloadDivinerConfigSchema = typeof IndexedDbPayloadDivinerConfigSchema;
10
- type IndexedDbPayloadDivinerConfig = DivinerConfig<{
11
- dbName?: string;
12
- dbVersion?: number;
13
- schema: IndexedDbPayloadDivinerConfigSchema;
14
- storage?: {
15
- indexes?: IndexDescription[];
16
- };
17
- storeName?: string;
18
- }>;
19
-
20
- type IndexedDbPayloadDivinerParams = DivinerParams<AnyConfigSchema<IndexedDbPayloadDivinerConfig>>;
21
-
22
- declare class IndexedDbPayloadDiviner<TParams extends IndexedDbPayloadDivinerParams = IndexedDbPayloadDivinerParams, TIn extends PayloadDivinerQueryPayload = PayloadDivinerQueryPayload, TOut extends Payload = Payload, TEventData extends DivinerModuleEventData<DivinerInstance<TParams, TIn, TOut>, TIn, TOut> = DivinerModuleEventData<DivinerInstance<TParams, TIn, TOut>, TIn, TOut>> extends PayloadDiviner<TParams, TIn, TOut, TEventData> {
23
- static readonly configSchemas: Schema[];
24
- static readonly defaultConfigSchema: Schema;
25
- private _db;
26
- get dbName(): string;
27
- get dbVersion(): number;
28
- get storeName(): string;
29
- protected divineHandler(payloads?: TIn[]): Promise<TOut[]>;
30
- protected startHandler(): Promise<boolean>;
31
- private getKeyRangeValue;
32
- private selectBestIndex;
33
- private tryGetInitializedDb;
34
- private tryUseDb;
35
- }
36
-
37
- declare const IndexedDbPayloadDivinerSchema: "network.xyo.diviner.payload.indexeddb";
38
- type IndexedDbPayloadDivinerSchema = typeof IndexedDbPayloadDivinerSchema;
39
-
40
- export { IndexedDbPayloadDiviner, type IndexedDbPayloadDivinerConfig, IndexedDbPayloadDivinerConfigSchema, type IndexedDbPayloadDivinerParams, IndexedDbPayloadDivinerSchema };
1
+ export * from './Config.ts';
2
+ export * from './Diviner.ts';
3
+ export * from './Params.ts';
4
+ export * from './Schema.ts';
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAA;AAC3B,cAAc,cAAc,CAAA;AAC5B,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA"}
@@ -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 type { IndexDescription } from '@xyo-network/archivist-model'\nimport type { DivinerConfig } from '@xyo-network/diviner-model'\n\nimport { IndexedDbPayloadDivinerSchema } from './Schema.ts'\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 type { Hash } from '@xylabs/hex'\nimport type { AnyObject } from '@xylabs/object'\nimport { removeFields } from '@xylabs/object'\nimport { IndexedDbArchivist } from '@xyo-network/archivist-indexeddb'\nimport { IndexSeparator } from '@xyo-network/archivist-model'\nimport type { DivinerInstance, DivinerModuleEventData } from '@xyo-network/diviner-model'\nimport { PayloadDiviner } from '@xyo-network/diviner-payload-abstract'\nimport type { PayloadDivinerQueryPayload } from '@xyo-network/diviner-payload-model'\nimport { isPayloadDivinerQueryPayload } from '@xyo-network/diviner-payload-model'\nimport { PayloadBuilder } from '@xyo-network/payload-builder'\nimport type { Payload, Schema, WithMeta } from '@xyo-network/payload-model'\nimport type { IDBPDatabase, IDBPObjectStore } from 'idb'\nimport { openDB } from 'idb'\n\nimport { IndexedDbPayloadDivinerConfigSchema } from './Config.ts'\nimport type { IndexedDbPayloadDivinerParams } from './Params.ts'\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\n// Function to extract fields from an index name\nconst extractFields = (indexName: string): string[] => {\n return indexName\n .slice(3)\n .split(IndexSeparator)\n .map(field => field.toLowerCase())\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\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 // 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;AAGvB,SAASC,oBAAo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GyD,SAAS,KAAKzE,UAAUgB,GAAG0D,YAAY,KAAKrE;IACxD,CAAA;AAEA,QAAIkE,UAAU;AAEZ,YAAMvD,KAAK,MAAM2D,OAAqB,KAAK3E,QAAQ,KAAKK,SAAS;AAEjE,YAAMuE,cAAc5D,GAAG6D,iBAAiBC,SAAS,KAAKvE,SAAS;AAE/D,UAAIqE,aAAa;AACf,eAAO5D;MACT,OAAO;AAGLA,WAAG+D,MAAK;MACV;IACF;EACF;;;;;;EAOA,MAAchE,SAAYiE,UAAsF;AAE9G,UAAMhE,KAAK,MAAM,KAAKmD,oBAAmB;AACzC,QAAInD,IAAI;AACN,UAAI;AAEF,eAAO,MAAMgE,SAAShE,EAAAA;MACxB,UAAA;AAEEA,WAAG+D,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","extractFields","indexName","slice","split","IndexSeparator","map","field","toLowerCase","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","exists","cursor","index","openCursor","IDBKeyRange","only","advance","every","push","continue","done","Promise","all","PayloadBuilder","build","startHandler","indexFields","indexNames","queryKeys","Set","keys","bestMatch","matchCount","has","tryGetInitializedDb","dbs","indexedDB","databases","dbExists","some","name","version","openDB","storeExists","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 type { IndexDescription } from '@xyo-network/archivist-model'\nimport type { DivinerConfig } from '@xyo-network/diviner-model'\n\nimport { IndexedDbPayloadDivinerSchema } from './Schema.ts'\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 type { Hash } from '@xylabs/hex'\nimport type { AnyObject } from '@xylabs/object'\nimport { removeFields } from '@xylabs/object'\nimport { IndexedDbArchivist } from '@xyo-network/archivist-indexeddb'\nimport { IndexSeparator } from '@xyo-network/archivist-model'\nimport type { DivinerInstance, DivinerModuleEventData } from '@xyo-network/diviner-model'\nimport { PayloadDiviner } from '@xyo-network/diviner-payload-abstract'\nimport type { PayloadDivinerQueryPayload } from '@xyo-network/diviner-payload-model'\nimport { isPayloadDivinerQueryPayload } from '@xyo-network/diviner-payload-model'\nimport { PayloadBuilder } from '@xyo-network/payload-builder'\nimport type {\n Payload, Schema, WithMeta,\n} from '@xyo-network/payload-model'\nimport type { IDBPDatabase, IDBPObjectStore } from 'idb'\nimport { openDB } from 'idb'\n\nimport { IndexedDbPayloadDivinerConfigSchema } from './Config.ts'\nimport type { IndexedDbPayloadDivinerParams } from './Params.ts'\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\n// Function to extract fields from an index name\nconst extractFields = (indexName: string): string[] => {\n return indexName\n .slice(3)\n .split(IndexSeparator)\n .map(field => field.toLowerCase())\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 {\n schemas, limit, offset, order, ...props\n } = 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\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 // 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;AAGvB,SAASC,oBAAoB;AAC7B,SAASC,0BAA0B;AACnC,SAASC,sBAAsB;AAE/B,SAASC,sBAAsB;AAE/B,SAASC,oCAAoC;AAC7C,SAASC,sBAAsB;AAK/B,SAASC,cAAc;AAavB,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;AAW3B,IAAMO,gBAAgB,wBAACC,cAAAA;AACrB,SAAOA,UACJC,MAAM,CAAA,EACNC,MAAMC,cAAAA,EACNC,IAAIC,CAAAA,UAASA,MAAMC,YAAW,CAAA;AACnC,GALsB;AAOf,IAAMC,0BAAN,cASGC,eAAAA;EAzDV,OAyDUA;;;EACR,OAAyBC,gBAA0B;OAAI,MAAMA;IAAeC;;EAC5E,OAAyBC,sBAA8BD;EAE/CE;;;;;;;;EASR,IAAIC,SAAS;AACX,WAAO,KAA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D;MAAE7E,WAAW;MAAI8E,YAAY;IAAE;AAE1F,eAAW9E,aAAayE,YAAY;AAClC,YAAMD,cAAczE,cAAcC,SAAAA;AAClC,YAAM8E,aAAaN,YAAY1B,OAAOzC,CAAAA,UAASqE,UAAUK,IAAI1E,KAAAA,CAAAA,EAAQuC;AACrE,UAAIkC,aAAaD,UAAUC,YAAY;AACrCD,oBAAY;UAAE7E;UAAW8E;QAAW;MACtC;IACF;AACA,WAAOD,UAAUC,aAAa,IAAID,UAAU7E,YAAY;EAC1D;;;;;EAMA,MAAcgF,sBAAuE;AAEnF,UAAMC,MAAM,MAAMC,UAAUC,UAAS;AAErC,UAAMC,WAAWH,IAAII,KAAK,CAACxD,OAAAA;AACzB,aAAOA,GAAGyD,SAAS,KAAKzE,UAAUgB,GAAG0D,YAAY,KAAKrE;IACxD,CAAA;AAEA,QAAIkE,UAAU;AAEZ,YAAMvD,KAAK,MAAM2D,OAAqB,KAAK3E,QAAQ,KAAKK,SAAS;AAEjE,YAAMuE,cAAc5D,GAAG6D,iBAAiBC,SAAS,KAAKvE,SAAS;AAE/D,UAAIqE,aAAa;AACf,eAAO5D;MACT,OAAO;AAGLA,WAAG+D,MAAK;MACV;IACF;EACF;;;;;;EAOA,MAAchE,SAAYiE,UAAsF;AAE9G,UAAMhE,KAAK,MAAM,KAAKmD,oBAAmB;AACzC,QAAInD,IAAI;AACN,UAAI;AAEF,eAAO,MAAMgE,SAAShE,EAAAA;MACxB,UAAA;AAEEA,WAAG+D,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","extractFields","indexName","slice","split","IndexSeparator","map","field","toLowerCase","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","exists","cursor","index","openCursor","IDBKeyRange","only","advance","every","push","continue","done","Promise","all","PayloadBuilder","build","startHandler","indexFields","indexNames","queryKeys","Set","keys","bestMatch","matchCount","has","tryGetInitializedDb","dbs","indexedDB","databases","dbExists","some","name","version","openDB","storeExists","objectStoreNames","contains","close","callback"]}
package/package.json CHANGED
@@ -1,40 +1,24 @@
1
1
  {
2
2
  "name": "@xyo-network/diviner-payload-indexeddb",
3
- "author": {
4
- "email": "support@xyo.network",
5
- "name": "XYO Development Team",
6
- "url": "https://xyo.network"
7
- },
3
+ "version": "3.0.8",
4
+ "description": "Primary SDK for using XYO Protocol 2.0",
5
+ "homepage": "https://xyo.network",
8
6
  "bugs": {
9
- "email": "support@xyo.network",
10
- "url": "git+https://github.com/XYOracleNetwork/sdk-xyo-client-js/issues"
7
+ "url": "git+https://github.com/XYOracleNetwork/sdk-xyo-client-js/issues",
8
+ "email": "support@xyo.network"
11
9
  },
12
- "dependencies": {
13
- "@xylabs/array": "^4.0.2",
14
- "@xylabs/assert": "^4.0.2",
15
- "@xylabs/exists": "^4.0.2",
16
- "@xylabs/hex": "^4.0.2",
17
- "@xylabs/object": "^4.0.2",
18
- "@xyo-network/archivist-indexeddb": "^3.0.6",
19
- "@xyo-network/archivist-model": "^3.0.6",
20
- "@xyo-network/diviner-model": "^3.0.6",
21
- "@xyo-network/diviner-payload-abstract": "^3.0.6",
22
- "@xyo-network/diviner-payload-model": "^3.0.6",
23
- "@xyo-network/module-model": "^3.0.6",
24
- "@xyo-network/payload-builder": "^3.0.6",
25
- "@xyo-network/payload-model": "^3.0.6",
26
- "idb": "^8.0.0"
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://github.com/XYOracleNetwork/sdk-xyo-client-js.git"
27
13
  },
28
- "devDependencies": {
29
- "@xylabs/ts-scripts-yarn3": "^4.0.0-rc.30",
30
- "@xylabs/tsconfig": "^4.0.0-rc.30",
31
- "@xyo-network/archivist-indexeddb": "^3.0.6",
32
- "@xyo-network/node-memory": "^3.0.6",
33
- "fake-indexeddb": "^6.0.0",
34
- "typescript": "^5.5.4"
14
+ "license": "LGPL-3.0-only",
15
+ "author": {
16
+ "name": "XYO Development Team",
17
+ "email": "support@xyo.network",
18
+ "url": "https://xyo.network"
35
19
  },
36
- "description": "Primary SDK for using XYO Protocol 2.0",
37
- "types": "dist/browser/index.d.ts",
20
+ "sideEffects": false,
21
+ "type": "module",
38
22
  "exports": {
39
23
  ".": {
40
24
  "types": "./dist/browser/index.d.ts",
@@ -43,16 +27,32 @@
43
27
  "./package.json": "./package.json"
44
28
  },
45
29
  "module": "dist/browser/index.mjs",
46
- "homepage": "https://xyo.network",
47
- "license": "LGPL-3.0-only",
48
- "publishConfig": {
49
- "access": "public"
30
+ "types": "dist/browser/index.d.ts",
31
+ "dependencies": {
32
+ "@xylabs/array": "^4.0.3",
33
+ "@xylabs/assert": "^4.0.3",
34
+ "@xylabs/exists": "^4.0.3",
35
+ "@xylabs/hex": "^4.0.3",
36
+ "@xylabs/object": "^4.0.3",
37
+ "@xyo-network/archivist-indexeddb": "^3.0.8",
38
+ "@xyo-network/archivist-model": "^3.0.8",
39
+ "@xyo-network/diviner-model": "^3.0.8",
40
+ "@xyo-network/diviner-payload-abstract": "^3.0.8",
41
+ "@xyo-network/diviner-payload-model": "^3.0.8",
42
+ "@xyo-network/module-model": "^3.0.8",
43
+ "@xyo-network/payload-builder": "^3.0.8",
44
+ "@xyo-network/payload-model": "^3.0.8",
45
+ "idb": "^8.0.0"
50
46
  },
51
- "repository": {
52
- "type": "git",
53
- "url": "git+https://github.com/XYOracleNetwork/sdk-xyo-client-js.git"
47
+ "devDependencies": {
48
+ "@xylabs/ts-scripts-yarn3": "^4.0.7",
49
+ "@xylabs/tsconfig": "^4.0.7",
50
+ "@xyo-network/archivist-indexeddb": "^3.0.8",
51
+ "@xyo-network/node-memory": "^3.0.8",
52
+ "fake-indexeddb": "^6.0.0",
53
+ "typescript": "^5.5.4"
54
54
  },
55
- "sideEffects": false,
56
- "version": "3.0.6",
57
- "type": "module"
55
+ "publishConfig": {
56
+ "access": "public"
57
+ }
58
58
  }
package/src/Diviner.ts CHANGED
@@ -11,7 +11,9 @@ import { PayloadDiviner } from '@xyo-network/diviner-payload-abstract'
11
11
  import type { PayloadDivinerQueryPayload } from '@xyo-network/diviner-payload-model'
12
12
  import { isPayloadDivinerQueryPayload } from '@xyo-network/diviner-payload-model'
13
13
  import { PayloadBuilder } from '@xyo-network/payload-builder'
14
- import type { Payload, Schema, WithMeta } from '@xyo-network/payload-model'
14
+ import type {
15
+ Payload, Schema, WithMeta,
16
+ } from '@xyo-network/payload-model'
15
17
  import type { IDBPDatabase, IDBPObjectStore } from 'idb'
16
18
  import { openDB } from 'idb'
17
19
 
@@ -89,7 +91,9 @@ export class IndexedDbPayloadDiviner<
89
91
  const query = payloads?.find(isPayloadDivinerQueryPayload) as TIn
90
92
  if (!query) return []
91
93
  const result = await this.tryUseDb(async (db) => {
92
- const { schemas, limit, offset, order, ...props } = removeFields(query as unknown as WithMeta<TIn> & { sources?: Hash[] }, [
94
+ const {
95
+ schemas, limit, offset, order, ...props
96
+ } = removeFields(query as unknown as WithMeta<TIn> & { sources?: Hash[] }, [
93
97
  'hash',
94
98
  'schema',
95
99
  '$meta',