@xyo-network/diviner-payload-indexeddb 5.2.27 → 5.3.1
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/Config.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import type { IndexDescription } from '@xyo-network/archivist-model';
|
|
2
2
|
import type { DivinerConfig } from '@xyo-network/diviner-model';
|
|
3
|
-
export declare const IndexedDbPayloadDivinerConfigSchema:
|
|
3
|
+
export declare const IndexedDbPayloadDivinerConfigSchema: string & {
|
|
4
|
+
readonly __schema: true;
|
|
5
|
+
};
|
|
4
6
|
export type IndexedDbPayloadDivinerConfigSchema = typeof IndexedDbPayloadDivinerConfigSchema;
|
|
5
7
|
export type IndexedDbPayloadDivinerConfig = DivinerConfig<{
|
|
6
8
|
/**
|
|
@@ -1 +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;
|
|
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;AAK/D,eAAO,MAAM,mCAAmC;;CAA4D,CAAA;AAC5G,MAAM,MAAM,mCAAmC,GAAG,OAAO,mCAAmC,CAAA;AAE5F,MAAM,MAAM,6BAA6B,GAAG,aAAa,CAAC;IACxD;;OAEG;IACH,MAAM,CAAC,EAAE,MAAM,CAAA;IACf;;OAEG;IACH,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,MAAM,EAAE,mCAAmC,CAAA;IAC3C;;;OAGG;IACH,OAAO,CAAC,EAAE;QACR;;WAEG;QACH,OAAO,CAAC,EAAE,gBAAgB,EAAE,CAAA;KAC7B,CAAA;IACD;;OAEG;IACH,SAAS,CAAC,EAAE,MAAM,CAAA;CACnB,CAAC,CAAA"}
|
package/dist/browser/Schema.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
-
export declare const IndexedDbPayloadDivinerSchema: "network.xyo.diviner.payload
|
|
1
|
+
export declare const IndexedDbPayloadDivinerSchema: `${"network.xyo.diviner.payload" & {
|
|
2
|
+
readonly __schema: true;
|
|
3
|
+
}}.indexeddb`;
|
|
2
4
|
export type IndexedDbPayloadDivinerSchema = typeof IndexedDbPayloadDivinerSchema;
|
|
3
5
|
//# sourceMappingURL=Schema.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Schema.d.ts","sourceRoot":"","sources":["../../src/Schema.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,6BAA6B
|
|
1
|
+
{"version":3,"file":"Schema.d.ts","sourceRoot":"","sources":["../../src/Schema.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,6BAA6B;;aAA+C,CAAA;AACzF,MAAM,MAAM,6BAA6B,GAAG,OAAO,6BAA6B,CAAA"}
|
package/dist/browser/index.mjs
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
|
+
// src/Config.ts
|
|
2
|
+
import { asSchema } from "@xyo-network/payload-model";
|
|
3
|
+
|
|
1
4
|
// src/Schema.ts
|
|
2
5
|
import { PayloadDivinerSchema } from "@xyo-network/diviner-payload-model";
|
|
3
6
|
var IndexedDbPayloadDivinerSchema = `${PayloadDivinerSchema}.indexeddb`;
|
|
4
7
|
|
|
5
8
|
// src/Config.ts
|
|
6
|
-
var IndexedDbPayloadDivinerConfigSchema = `${IndexedDbPayloadDivinerSchema}.config
|
|
9
|
+
var IndexedDbPayloadDivinerConfigSchema = asSchema(`${IndexedDbPayloadDivinerSchema}.config`, true);
|
|
7
10
|
|
|
8
11
|
// src/Diviner.ts
|
|
9
12
|
import {
|
|
@@ -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","/// <reference lib=\"dom\" />\n\nimport {\n assertEx, containsAll,\n exists, removeFields,\n} from '@xylabs/sdk-js'\nimport { IndexedDbArchivist } from '@xyo-network/archivist-indexeddb'\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 type {\n AnyPayload,\n Payload, Schema, Sequence,\n} from '@xyo-network/payload-model'\nimport type { IDBPCursorWithValue, IDBPDatabase } 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 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 /**\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)\n if (!query) return []\n const result = await this.tryUseDb(async (db) => {\n const {\n schemas, limit, cursor, order, ...props\n } = removeFields(query, ['schema'])\n const tx = db.transaction(this.storeName, 'readonly')\n const store = tx.objectStore(this.storeName)\n const results: TOut[] = []\n let parsedCursor = cursor\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 valueFilters: ValueFilter[] = Object.entries(filter)\n .map(([key, value]) => payloadValueFilter(key, value))\n .filter(exists)\n const direction: IDBCursorDirection = order === 'desc' ? 'prev' : 'next'\n\n // Iterate all records using the sequence index\n const sequenceIndex = assertEx(store.index(IndexedDbArchivist.sequenceIndexName), () => 'Failed to get sequence index')\n let dbCursor: IDBPCursorWithValue<PayloadStore, [string], string, string, 'readonly'> | null\n = await sequenceIndex.openCursor(null, direction)\n\n // If a cursor was supplied\n if (parsedCursor !== undefined) {\n let currentSequence: Sequence | undefined\n // Skip records until the supplied cursor offset is reached\n while (dbCursor && currentSequence !== parsedCursor) {\n // Find the sequence of the current record\n currentSequence = await dbCursor.value?.sequence\n // Advance one record beyond the cursor\n dbCursor = await dbCursor.advance(1)\n }\n }\n\n // Collect results up to the limit\n while (dbCursor && results.length < parsedLimit) {\n const value = dbCursor.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 dbCursor = await dbCursor.continue()\n } catch {\n break\n }\n }\n await tx.done\n // Remove any metadata before returning to the client\n return results\n })\n return result ?? []\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\n const dbExists = dbs.some((db) => {\n return db.name === this.dbName\n })\n // If the DB exists\n if (dbExists) {\n // If the db does exist, open it\n const db = await openDB<PayloadStore>(this.dbName)\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,SAAS,4BAA4B;AAE9B,IAAM,gCAAgC,GAAG,oBAAoB;;;ACG7D,IAAM,sCAAsC,GAAG,6BAA6B;;;ACHnF;AAAA,EACE;AAAA,EAAU;AAAA,EACV;AAAA,EAAQ;AAAA,OACH;AACP,SAAS,0BAA0B;AAEnC,SAAS,sBAAsB;AAE/B,SAAS,oCAAoC;AAM7C,SAAS,cAAc;AAWvB,IAAM,qBAAqB,CAAC,KAAuB,UAAyD;AAC1G,MAAI,CAAC,MAAO,QAAO;AACnB,SAAO,CAAC,YAAY;AAClB,QAAI,CAAC,QAAS,QAAO;AACrB,UAAM,cAAc,UAAU,GAAG;AACjC,QAAI,gBAAgB,OAAW,QAAO;AACtC,WAAO,MAAM,QAAQ,WAAW,KAAK,MAAM,QAAQ,KAAK,IAAI,YAAY,aAAa,KAAK,IAAI,eAAe;AAAA,EAC/G;AACF;AAEO,IAAM,0BAAN,cASG,eAA+C;AAAA,EACvD,OAAyB,gBAA0B,CAAC,GAAG,MAAM,eAAe,mCAAmC;AAAA,EAC/G,OAAyB,sBAA8B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASvD,IAAI,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.ts","../../src/Schema.ts","../../src/Diviner.ts"],"sourcesContent":["import type { IndexDescription } from '@xyo-network/archivist-model'\nimport type { DivinerConfig } from '@xyo-network/diviner-model'\nimport { asSchema } from '@xyo-network/payload-model'\n\nimport { IndexedDbPayloadDivinerSchema } from './Schema.ts'\n\nexport const IndexedDbPayloadDivinerConfigSchema = asSchema(`${IndexedDbPayloadDivinerSchema}.config`, true)\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 { PayloadDivinerSchema } from '@xyo-network/diviner-payload-model'\n\nexport const IndexedDbPayloadDivinerSchema = `${PayloadDivinerSchema}.indexeddb` as const\nexport type IndexedDbPayloadDivinerSchema = typeof IndexedDbPayloadDivinerSchema\n","/// <reference lib=\"dom\" />\n\nimport {\n assertEx, containsAll,\n exists, removeFields,\n} from '@xylabs/sdk-js'\nimport { IndexedDbArchivist } from '@xyo-network/archivist-indexeddb'\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 type {\n AnyPayload,\n Payload, Schema, Sequence,\n} from '@xyo-network/payload-model'\nimport type { IDBPCursorWithValue, IDBPDatabase } 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 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 /**\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)\n if (!query) return []\n const result = await this.tryUseDb(async (db) => {\n const {\n schemas, limit, cursor, order, ...props\n } = removeFields(query, ['schema'])\n const tx = db.transaction(this.storeName, 'readonly')\n const store = tx.objectStore(this.storeName)\n const results: TOut[] = []\n let parsedCursor = cursor\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 valueFilters: ValueFilter[] = Object.entries(filter)\n .map(([key, value]) => payloadValueFilter(key, value))\n .filter(exists)\n const direction: IDBCursorDirection = order === 'desc' ? 'prev' : 'next'\n\n // Iterate all records using the sequence index\n const sequenceIndex = assertEx(store.index(IndexedDbArchivist.sequenceIndexName), () => 'Failed to get sequence index')\n let dbCursor: IDBPCursorWithValue<PayloadStore, [string], string, string, 'readonly'> | null\n = await sequenceIndex.openCursor(null, direction)\n\n // If a cursor was supplied\n if (parsedCursor !== undefined) {\n let currentSequence: Sequence | undefined\n // Skip records until the supplied cursor offset is reached\n while (dbCursor && currentSequence !== parsedCursor) {\n // Find the sequence of the current record\n currentSequence = await dbCursor.value?.sequence\n // Advance one record beyond the cursor\n dbCursor = await dbCursor.advance(1)\n }\n }\n\n // Collect results up to the limit\n while (dbCursor && results.length < parsedLimit) {\n const value = dbCursor.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 dbCursor = await dbCursor.continue()\n } catch {\n break\n }\n }\n await tx.done\n // Remove any metadata before returning to the client\n return results\n })\n return result ?? []\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\n const dbExists = dbs.some((db) => {\n return db.name === this.dbName\n })\n // If the DB exists\n if (dbExists) {\n // If the db does exist, open it\n const db = await openDB<PayloadStore>(this.dbName)\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":";AAEA,SAAS,gBAAgB;;;ACFzB,SAAS,4BAA4B;AAE9B,IAAM,gCAAgC,GAAG,oBAAoB;;;ADI7D,IAAM,sCAAsC,SAAS,GAAG,6BAA6B,WAAW,IAAI;;;AEJ3G;AAAA,EACE;AAAA,EAAU;AAAA,EACV;AAAA,EAAQ;AAAA,OACH;AACP,SAAS,0BAA0B;AAEnC,SAAS,sBAAsB;AAE/B,SAAS,oCAAoC;AAM7C,SAAS,cAAc;AAWvB,IAAM,qBAAqB,CAAC,KAAuB,UAAyD;AAC1G,MAAI,CAAC,MAAO,QAAO;AACnB,SAAO,CAAC,YAAY;AAClB,QAAI,CAAC,QAAS,QAAO;AACrB,UAAM,cAAc,UAAU,GAAG;AACjC,QAAI,gBAAgB,OAAW,QAAO;AACtC,WAAO,MAAM,QAAQ,WAAW,KAAK,MAAM,QAAQ,KAAK,IAAI,YAAY,aAAa,KAAK,IAAI,eAAe;AAAA,EAC/G;AACF;AAEO,IAAM,0BAAN,cASG,eAA+C;AAAA,EACvD,OAAyB,gBAA0B,CAAC,GAAG,MAAM,eAAe,mCAAmC;AAAA,EAC/G,OAAyB,sBAA8B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASvD,IAAI,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6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xyo-network/diviner-payload-indexeddb",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.3.1",
|
|
4
4
|
"description": "Primary SDK for using XYO Protocol 2.0",
|
|
5
5
|
"homepage": "https://xyo.network",
|
|
6
6
|
"bugs": {
|
|
@@ -37,22 +37,22 @@
|
|
|
37
37
|
],
|
|
38
38
|
"dependencies": {
|
|
39
39
|
"@xylabs/sdk-js": "~5.0.64",
|
|
40
|
-
"@xyo-network/archivist-indexeddb": "~5.
|
|
41
|
-
"@xyo-network/archivist-model": "~5.
|
|
42
|
-
"@xyo-network/diviner-model": "~5.
|
|
43
|
-
"@xyo-network/diviner-payload-abstract": "~5.
|
|
44
|
-
"@xyo-network/diviner-payload-model": "~5.
|
|
45
|
-
"@xyo-network/module-model": "~5.
|
|
46
|
-
"@xyo-network/payload-model": "~5.
|
|
40
|
+
"@xyo-network/archivist-indexeddb": "~5.3.1",
|
|
41
|
+
"@xyo-network/archivist-model": "~5.3.1",
|
|
42
|
+
"@xyo-network/diviner-model": "~5.3.1",
|
|
43
|
+
"@xyo-network/diviner-payload-abstract": "~5.3.1",
|
|
44
|
+
"@xyo-network/diviner-payload-model": "~5.3.1",
|
|
45
|
+
"@xyo-network/module-model": "~5.3.1",
|
|
46
|
+
"@xyo-network/payload-model": "~5.3.1",
|
|
47
47
|
"idb": "~8.0.3"
|
|
48
48
|
},
|
|
49
49
|
"devDependencies": {
|
|
50
50
|
"@xylabs/ts-scripts-yarn3": "~7.3.2",
|
|
51
51
|
"@xylabs/tsconfig": "~7.3.2",
|
|
52
52
|
"@xylabs/vitest-extended": "~5.0.64",
|
|
53
|
-
"@xyo-network/archivist-indexeddb": "~5.
|
|
54
|
-
"@xyo-network/node-memory": "~5.
|
|
55
|
-
"@xyo-network/payload-builder": "~5.
|
|
53
|
+
"@xyo-network/archivist-indexeddb": "~5.3.1",
|
|
54
|
+
"@xyo-network/node-memory": "~5.3.1",
|
|
55
|
+
"@xyo-network/payload-builder": "~5.3.1",
|
|
56
56
|
"fake-indexeddb": "~6.2.5",
|
|
57
57
|
"typescript": "~5.9.3",
|
|
58
58
|
"vitest": "~4.0.18"
|
package/src/Config.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import type { IndexDescription } from '@xyo-network/archivist-model'
|
|
2
2
|
import type { DivinerConfig } from '@xyo-network/diviner-model'
|
|
3
|
+
import { asSchema } from '@xyo-network/payload-model'
|
|
3
4
|
|
|
4
5
|
import { IndexedDbPayloadDivinerSchema } from './Schema.ts'
|
|
5
6
|
|
|
6
|
-
export const IndexedDbPayloadDivinerConfigSchema = `${IndexedDbPayloadDivinerSchema}.config
|
|
7
|
+
export const IndexedDbPayloadDivinerConfigSchema = asSchema(`${IndexedDbPayloadDivinerSchema}.config`, true)
|
|
7
8
|
export type IndexedDbPayloadDivinerConfigSchema = typeof IndexedDbPayloadDivinerConfigSchema
|
|
8
9
|
|
|
9
10
|
export type IndexedDbPayloadDivinerConfig = DivinerConfig<{
|