@xyo-network/diviner-boundwitness-indexeddb 3.5.2 → 3.6.0-rc.2
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/Diviner.d.ts.map +1 -1
- package/dist/browser/index.mjs +1 -4
- package/dist/browser/index.mjs.map +1 -1
- package/package.json +19 -19
- package/src/Diviner.ts +1 -4
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Diviner.d.ts","sourceRoot":"","sources":["../../src/Diviner.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,iCAAiC,CAAA;AAEnE,OAAO,EAAE,mBAAmB,EAAE,MAAM,4CAA4C,CAAA;AAChF,OAAO,KAAK,EAAE,+BAA+B,EAAE,MAAM,yCAAyC,CAAA;
|
|
1
|
+
{"version":3,"file":"Diviner.d.ts","sourceRoot":"","sources":["../../src/Diviner.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,iCAAiC,CAAA;AAEnE,OAAO,EAAE,mBAAmB,EAAE,MAAM,4CAA4C,CAAA;AAChF,OAAO,KAAK,EAAE,+BAA+B,EAAE,MAAM,yCAAyC,CAAA;AAE9F,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,4BAA4B,CAAA;AAKxD,OAAO,KAAK,EAAE,kCAAkC,EAAE,MAAM,aAAa,CAAA;AAmBrE,qBAAa,4BAA4B,CACvC,OAAO,SAAS,kCAAkC,GAAG,kCAAkC,EACvF,GAAG,SAAS,+BAA+B,GAAG,+BAA+B,EAC7E,IAAI,SAAS,YAAY,GAAG,YAAY,CACxC,SAAQ,mBAAmB,CAAC,OAAO,EAAE,GAAG,EAAE,IAAI,CAAC;IAC/C,gBAAyB,aAAa,EAAE,MAAM,EAAE,CAAqE;IACrH,gBAAyB,mBAAmB,EAAE,MAAM,CAA2C;IAE/F;;;;;;OAMG;IACH,IAAI,MAAM,WAET;IAED;;OAEG;IACH,IAAI,SAAS,WAEZ;IAED;;;OAGG;IACH,IAAI,SAAS,WAEZ;cAEwB,aAAa,CAAC,QAAQ,CAAC,EAAE,GAAG,EAAE,GAAG,OAAO,CAAC,IAAI,EAAE,CAAC;cA8DhD,YAAY;IAQrC;;;OAGG;YACW,mBAAmB;IAwBjC;;;;OAIG;YACW,QAAQ;CAcvB"}
|
package/dist/browser/index.mjs
CHANGED
|
@@ -15,7 +15,6 @@ import { IndexedDbArchivist } from "@xyo-network/archivist-indexeddb";
|
|
|
15
15
|
import { BoundWitnessSchema, isBoundWitness } from "@xyo-network/boundwitness-model";
|
|
16
16
|
import { BoundWitnessDiviner } from "@xyo-network/diviner-boundwitness-abstract";
|
|
17
17
|
import { isBoundWitnessDivinerQueryPayload } from "@xyo-network/diviner-boundwitness-model";
|
|
18
|
-
import { PayloadBuilder } from "@xyo-network/payload-builder";
|
|
19
18
|
import { openDB } from "idb";
|
|
20
19
|
var bwValueFilter = /* @__PURE__ */ __name((key, values) => {
|
|
21
20
|
if (!values || values?.length === 0) return void 0;
|
|
@@ -97,9 +96,7 @@ var IndexedDbBoundWitnessDiviner = class extends BoundWitnessDiviner {
|
|
|
97
96
|
}
|
|
98
97
|
}
|
|
99
98
|
await tx.done;
|
|
100
|
-
return await Promise.all(results.filter(isBoundWitness)
|
|
101
|
-
return PayloadBuilder.build(bw);
|
|
102
|
-
}));
|
|
99
|
+
return await Promise.all(results.filter(isBoundWitness));
|
|
103
100
|
});
|
|
104
101
|
return result ?? [];
|
|
105
102
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/Schema.ts","../../src/Config.ts","../../src/Diviner.ts"],"sourcesContent":["import { BoundWitnessDivinerSchema } from '@xyo-network/diviner-boundwitness-model'\n\nexport const IndexedDbBoundWitnessDivinerSchema = `${BoundWitnessDivinerSchema}.indexeddb` as const\nexport type IndexedDbBoundWitnessDivinerSchema = typeof IndexedDbBoundWitnessDivinerSchema\n","import type { IndexDescription } from '@xyo-network/archivist-model'\nimport type { DivinerConfig } from '@xyo-network/diviner-model'\n\nimport { IndexedDbBoundWitnessDivinerSchema } from './Schema.ts'\n\nexport const IndexedDbBoundWitnessDivinerConfigSchema = `${IndexedDbBoundWitnessDivinerSchema}.config` as const\nexport type IndexedDbBoundWitnessDivinerConfigSchema = typeof IndexedDbBoundWitnessDivinerConfigSchema\n\nexport type IndexedDbBoundWitnessDivinerConfig = 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: IndexedDbBoundWitnessDivinerConfigSchema\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 { exists } from '@xylabs/exists'\nimport { IndexedDbArchivist } from '@xyo-network/archivist-indexeddb'\nimport type { BoundWitness } from '@xyo-network/boundwitness-model'\nimport { BoundWitnessSchema, isBoundWitness } from '@xyo-network/boundwitness-model'\nimport { BoundWitnessDiviner } from '@xyo-network/diviner-boundwitness-abstract'\nimport type { BoundWitnessDivinerQueryPayload } from '@xyo-network/diviner-boundwitness-model'\nimport { isBoundWitnessDivinerQueryPayload } from '@xyo-network/diviner-boundwitness-model'\nimport { PayloadBuilder } from '@xyo-network/payload-builder'\nimport type { Schema } from '@xyo-network/payload-model'\nimport type { IDBPDatabase } from 'idb'\nimport { openDB } from 'idb'\n\nimport { IndexedDbBoundWitnessDivinerConfigSchema } from './Config.ts'\nimport type { IndexedDbBoundWitnessDivinerParams } from './Params.ts'\n\ninterface BoundWitnessStore {\n [s: string]: BoundWitness\n}\n\ntype ValueFilter = (bw?: BoundWitness | null) => boolean\n\nconst bwValueFilter = (\n key: keyof Pick<BoundWitness, 'addresses' | 'payload_hashes' | 'payload_schemas'>,\n values?: string[],\n): ValueFilter | undefined => {\n if (!values || values?.length === 0) return undefined\n return (bw) => {\n if (!bw) return false\n return containsAll(bw[key], values)\n }\n}\n\nexport class IndexedDbBoundWitnessDiviner<\n TParams extends IndexedDbBoundWitnessDivinerParams = IndexedDbBoundWitnessDivinerParams,\n TIn extends BoundWitnessDivinerQueryPayload = BoundWitnessDivinerQueryPayload,\n TOut extends BoundWitness = BoundWitness,\n> extends BoundWitnessDiviner<TParams, TIn, TOut> {\n static override readonly configSchemas: Schema[] = [...super.configSchemas, IndexedDbBoundWitnessDivinerConfigSchema]\n static override readonly defaultConfigSchema: Schema = IndexedDbBoundWitnessDivinerConfigSchema\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 1.\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?.filter(isBoundWitnessDivinerQueryPayload)?.pop()\n if (!query) return []\n\n const result = await this.tryUseDb(async (db) => {\n const {\n addresses, payload_hashes, payload_schemas, limit, offset, order,\n } = query\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 const direction: IDBCursorDirection = order === 'desc' ? 'prev' : 'next'\n const valueFilters: ValueFilter[] = [\n bwValueFilter('addresses', addresses),\n bwValueFilter('payload_hashes', payload_hashes),\n bwValueFilter('payload_schemas', payload_schemas),\n ].filter(exists)\n // Only iterate over BWs\n let cursor = await store.index(IndexedDbArchivist.schemaIndexName).openCursor(IDBKeyRange.only(BoundWitnessSchema), direction)\n\n // If we're filtering on more than just the schema, we need to\n // iterate through all the results\n if (valueFilters.length === 0) {\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 }\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(\n results.filter(isBoundWitness).map((bw) => {\n return PayloadBuilder.build(bw)\n }),\n )\n })\n return result ?? []\n }\n\n protected override async startHandler() {\n await super.startHandler()\n // NOTE: Do not eager initialize the DB here. It will cause the\n // DB to be created by this process and then the DB will be\n // in a bad state for other processes that need to create the DB.\n return true\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<BoundWitnessStore> | 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<BoundWitnessStore>(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<BoundWitnessStore>) => 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,iCAAiC;AAEnC,IAAMC,qCAAqC,GAAGD,yBAAAA;;;ACG9C,IAAME,2CAA2C,GAAGC,kCAAAA;;;ACL3D,SAASC,mBAAmB;AAC5B,SAASC,cAAc;AACvB,SAASC,0BAA0B;AAEnC,SAASC,oBAAoBC,sBAAsB;AACnD,SAASC,2BAA2B;AAEpC,SAASC,yCAAyC;AAClD,SAASC,sBAAsB;AAG/B,SAASC,cAAc;AAWvB,IAAMC,gBAAgB,wBACpBC,KACAC,WAAAA;AAEA,MAAI,CAACA,UAAUA,QAAQC,WAAW,EAAG,QAAOC;AAC5C,SAAO,CAACC,OAAAA;AACN,QAAI,CAACA,GAAI,QAAO;AAChB,WAAOC,YAAYD,GAAGJ,GAAAA,GAAMC,MAAAA;EAC9B;AACF,GATsB;AAWf,IAAMK,+BAAN,cAIGC,oBAAAA;EArCV,OAqCUA;;;EACR,OAAyBC,gBAA0B;OAAI,MAAMA;IAAeC;;EAC5E,OAAyBC,sBAA8BD;;;;;;;;EASvD,IAAIE,SAAS;AACX,WAAO,KAAKC,QAAQD,UAAU,KAAKC,QAAQC,aAAaC,mBAAmBC;EAC7E;;;;EAKA,IAAIC,YAAY;AACd,WAAO,KAAKJ,QAAQI,aAAaF,mBAAmBG;EACtD;;;;;EAMA,IAAIC,YAAY;AACd,WAAO,KAAKN,QAAQM,aAAaJ,mBAAmBK;EACtD;EAEA,MAAyBC,cAAcC,UAAmC;AACxE,UAAMC,QAAQD,UAAUE,OAAOC,iCAAAA,GAAoCC,IAAAA;AACnE,QAAI,CAACH,MAAO,QAAO,CAAA;AAEnB,UAAMI,SAAS,MAAM,KAAKC,SAAS,OAAOC,OAAAA;AACxC,Y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WAAOsB,UAAU,CAAA;EACnB;EAEA,MAAyBsC,eAAe;AACtC,UAAM,MAAMA,aAAAA;AAIZ,WAAO;EACT;;;;;EAMA,MAAcC,sBAA4E;AAExF,UAAMC,MAAM,MAAMC,UAAUC,UAAS;AAErC,UAAMC,WAAWH,IAAII,KAAK,CAAC1C,OAAAA;AACzB,aAAOA,GAAG2C,SAAS,KAAK5D,UAAUiB,GAAG4C,YAAY,KAAKxD;IACxD,CAAA;AAEA,QAAIqD,UAAU;AAEZ,YAAMzC,KAAK,MAAM6C,OAA0B,KAAK9D,QAAQ,KAAKK,SAAS;AAEtE,YAAM0D,cAAc9C,GAAG+C,iBAAiBC,SAAS,KAAK1D,SAAS;AAE/D,UAAIwD,aAAa;AACf,eAAO9C;MACT,OAAO;AAGLA,WAAGiD,MAAK;MACV;IACF;EACF;;;;;;EAOA,MAAclD,SAAYmD,UAA2F;AAEnH,UAAMlD,KAAK,MAAM,KAAKqC,oBAAmB;AACzC,QAAIrC,IAAI;AACN,UAAI;AAEF,eAAO,MAAMkD,SAASlD,EAAAA;MACxB,UAAA;AAEEA,WAAGiD,MAAK;MACV;IACF;AACA,WAAO1E;EACT;AACF;","names":["BoundWitnessDivinerSchema","IndexedDbBoundWitnessDivinerSchema","IndexedDbBoundWitnessDivinerConfigSchema","IndexedDbBoundWitnessDivinerSchema","containsAll","exists","IndexedDbArchivist","BoundWitnessSchema","isBoundWitness","BoundWitnessDiviner","isBoundWitnessDivinerQueryPayload","PayloadBuilder","openDB","bwValueFilter","key","values","length","undefined","bw","containsAll","IndexedDbBoundWitnessDiviner","BoundWitnessDiviner","configSchemas","IndexedDbBoundWitnessDivinerConfigSchema","defaultConfigSchema","dbName","config","archivist","IndexedDbArchivist","defaultDbName","dbVersion","defaultDbVersion","storeName","defaultStoreName","divineHandler","payloads","query","filter","isBoundWitnessDivinerQueryPayload","pop","result","tryUseDb","db","addresses","payload_hashes","payload_schemas","limit","offset","order","tx","transaction","store","objectStore","results","parsedOffset","parsedLimit","direction","valueFilters","exists","cursor","index","schemaIndexName","openCursor","IDBKeyRange","only","BoundWitnessSchema","advance","value","every","push","continue","done","Promise","all","isBoundWitness","map","PayloadBuilder","build","startHandler","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 { BoundWitnessDivinerSchema } from '@xyo-network/diviner-boundwitness-model'\n\nexport const IndexedDbBoundWitnessDivinerSchema = `${BoundWitnessDivinerSchema}.indexeddb` as const\nexport type IndexedDbBoundWitnessDivinerSchema = typeof IndexedDbBoundWitnessDivinerSchema\n","import type { IndexDescription } from '@xyo-network/archivist-model'\nimport type { DivinerConfig } from '@xyo-network/diviner-model'\n\nimport { IndexedDbBoundWitnessDivinerSchema } from './Schema.ts'\n\nexport const IndexedDbBoundWitnessDivinerConfigSchema = `${IndexedDbBoundWitnessDivinerSchema}.config` as const\nexport type IndexedDbBoundWitnessDivinerConfigSchema = typeof IndexedDbBoundWitnessDivinerConfigSchema\n\nexport type IndexedDbBoundWitnessDivinerConfig = 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: IndexedDbBoundWitnessDivinerConfigSchema\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 { exists } from '@xylabs/exists'\nimport { IndexedDbArchivist } from '@xyo-network/archivist-indexeddb'\nimport type { BoundWitness } from '@xyo-network/boundwitness-model'\nimport { BoundWitnessSchema, isBoundWitness } from '@xyo-network/boundwitness-model'\nimport { BoundWitnessDiviner } from '@xyo-network/diviner-boundwitness-abstract'\nimport type { BoundWitnessDivinerQueryPayload } from '@xyo-network/diviner-boundwitness-model'\nimport { isBoundWitnessDivinerQueryPayload } from '@xyo-network/diviner-boundwitness-model'\nimport type { Schema } from '@xyo-network/payload-model'\nimport type { IDBPDatabase } from 'idb'\nimport { openDB } from 'idb'\n\nimport { IndexedDbBoundWitnessDivinerConfigSchema } from './Config.ts'\nimport type { IndexedDbBoundWitnessDivinerParams } from './Params.ts'\n\ninterface BoundWitnessStore {\n [s: string]: BoundWitness\n}\n\ntype ValueFilter = (bw?: BoundWitness | null) => boolean\n\nconst bwValueFilter = (\n key: keyof Pick<BoundWitness, 'addresses' | 'payload_hashes' | 'payload_schemas'>,\n values?: string[],\n): ValueFilter | undefined => {\n if (!values || values?.length === 0) return undefined\n return (bw) => {\n if (!bw) return false\n return containsAll(bw[key], values)\n }\n}\n\nexport class IndexedDbBoundWitnessDiviner<\n TParams extends IndexedDbBoundWitnessDivinerParams = IndexedDbBoundWitnessDivinerParams,\n TIn extends BoundWitnessDivinerQueryPayload = BoundWitnessDivinerQueryPayload,\n TOut extends BoundWitness = BoundWitness,\n> extends BoundWitnessDiviner<TParams, TIn, TOut> {\n static override readonly configSchemas: Schema[] = [...super.configSchemas, IndexedDbBoundWitnessDivinerConfigSchema]\n static override readonly defaultConfigSchema: Schema = IndexedDbBoundWitnessDivinerConfigSchema\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 1.\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?.filter(isBoundWitnessDivinerQueryPayload)?.pop()\n if (!query) return []\n\n const result = await this.tryUseDb(async (db) => {\n const {\n addresses, payload_hashes, payload_schemas, limit, offset, order,\n } = query\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 const direction: IDBCursorDirection = order === 'desc' ? 'prev' : 'next'\n const valueFilters: ValueFilter[] = [\n bwValueFilter('addresses', addresses),\n bwValueFilter('payload_hashes', payload_hashes),\n bwValueFilter('payload_schemas', payload_schemas),\n ].filter(exists)\n // Only iterate over BWs\n let cursor = await store.index(IndexedDbArchivist.schemaIndexName).openCursor(IDBKeyRange.only(BoundWitnessSchema), direction)\n\n // If we're filtering on more than just the schema, we need to\n // iterate through all the results\n if (valueFilters.length === 0) {\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 }\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(\n results.filter(isBoundWitness),\n )\n })\n return result ?? []\n }\n\n protected override async startHandler() {\n await super.startHandler()\n // NOTE: Do not eager initialize the DB here. It will cause the\n // DB to be created by this process and then the DB will be\n // in a bad state for other processes that need to create the DB.\n return true\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<BoundWitnessStore> | 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<BoundWitnessStore>(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<BoundWitnessStore>) => 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,iCAAiC;AAEnC,IAAMC,qCAAqC,GAAGD,yBAAAA;;;ACG9C,IAAME,2CAA2C,GAAGC,kCAAAA;;;ACL3D,SAASC,mBAAmB;AAC5B,SAASC,cAAc;AACvB,SAASC,0BAA0B;AAEnC,SAASC,oBAAoBC,sBAAsB;AACnD,SAASC,2BAA2B;AAEpC,SAASC,yCAAyC;AAGlD,SAASC,cAAc;AAWvB,IAAMC,gBAAgB,wBACpBC,KACAC,WAAAA;AAEA,MAAI,CAACA,UAAUA,QAAQC,WAAW,EAAG,QAAOC;AAC5C,SAAO,CAACC,OAAAA;AACN,QAAI,CAACA,GAAI,QAAO;AAChB,WAAOC,YAAYD,GAAGJ,GAAAA,GAAMC,MAAAA;EAC9B;AACF,GATsB;AAWf,IAAMK,+BAAN,cAIGC,oBAAAA;EApCV,OAoCUA;;;EACR,OAAyBC,gBAA0B;OAAI,MAAMA;IAAeC;;EAC5E,OAAyBC,sBAA8BD;;;;;;;;EASvD,IAAIE,SAAS;AACX,WAAO,KAAKC,QAAQD,UAAU,K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B,KAAAA,CAAAA,GAAS;AAE/Cd,sBAAQgB,KAAKF,KAAAA;YACf;UACF,OAAO;AAELd,oBAAQgB,KAAKF,KAAAA;UACf;QACF;AACA,YAAI;AACFR,mBAAS,MAAMA,OAAOW,SAAQ;QAChC,QAAQ;AACN;QACF;MACF;AACA,YAAMrB,GAAGsB;AAET,aAAO,MAAMC,QAAQC,IACnBpB,QAAQhB,OAAOqC,cAAAA,CAAAA;IAEnB,CAAA;AACA,WAAOlC,UAAU,CAAA;EACnB;EAEA,MAAyBmC,eAAe;AACtC,UAAM,MAAMA,aAAAA;AAIZ,WAAO;EACT;;;;;EAMA,MAAcC,sBAA4E;AAExF,UAAMC,MAAM,MAAMC,UAAUC,UAAS;AAErC,UAAMC,WAAWH,IAAII,KAAK,CAACvC,OAAAA;AACzB,aAAOA,GAAGwC,SAAS,KAAKzD,UAAUiB,GAAGyC,YAAY,KAAKrD;IACxD,CAAA;AAEA,QAAIkD,UAAU;AAEZ,YAAMtC,KAAK,MAAM0C,OAA0B,KAAK3D,QAAQ,KAAKK,SAAS;AAEtE,YAAMuD,cAAc3C,GAAG4C,iBAAiBC,SAAS,KAAKvD,SAAS;AAE/D,UAAIqD,aAAa;AACf,eAAO3C;MACT,OAAO;AAGLA,WAAG8C,MAAK;MACV;IACF;EACF;;;;;;EAOA,MAAc/C,SAAYgD,UAA2F;AAEnH,UAAM/C,KAAK,MAAM,KAAKkC,oBAAmB;AACzC,QAAIlC,IAAI;AACN,UAAI;AAEF,eAAO,MAAM+C,SAAS/C,EAAAA;MACxB,UAAA;AAEEA,WAAG8C,MAAK;MACV;IACF;AACA,WAAOvE;EACT;AACF;","names":["BoundWitnessDivinerSchema","IndexedDbBoundWitnessDivinerSchema","IndexedDbBoundWitnessDivinerConfigSchema","IndexedDbBoundWitnessDivinerSchema","containsAll","exists","IndexedDbArchivist","BoundWitnessSchema","isBoundWitness","BoundWitnessDiviner","isBoundWitnessDivinerQueryPayload","openDB","bwValueFilter","key","values","length","undefined","bw","containsAll","IndexedDbBoundWitnessDiviner","BoundWitnessDiviner","configSchemas","IndexedDbBoundWitnessDivinerConfigSchema","defaultConfigSchema","dbName","config","archivist","IndexedDbArchivist","defaultDbName","dbVersion","defaultDbVersion","storeName","defaultStoreName","divineHandler","payloads","query","filter","isBoundWitnessDivinerQueryPayload","pop","result","tryUseDb","db","addresses","payload_hashes","payload_schemas","limit","offset","order","tx","transaction","store","objectStore","results","parsedOffset","parsedLimit","direction","valueFilters","exists","cursor","index","schemaIndexName","openCursor","IDBKeyRange","only","BoundWitnessSchema","advance","value","every","push","continue","done","Promise","all","isBoundWitness","startHandler","tryGetInitializedDb","dbs","indexedDB","databases","dbExists","some","name","version","openDB","storeExists","objectStoreNames","contains","close","callback"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xyo-network/diviner-boundwitness-indexeddb",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.6.0-rc.2",
|
|
4
4
|
"description": "Primary SDK for using XYO Protocol 2.0",
|
|
5
5
|
"homepage": "https://xyo.network",
|
|
6
6
|
"bugs": {
|
|
@@ -29,32 +29,32 @@
|
|
|
29
29
|
"module": "dist/browser/index.mjs",
|
|
30
30
|
"types": "dist/browser/index.d.ts",
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@xylabs/array": "^4.4.
|
|
33
|
-
"@xylabs/exists": "^4.4.
|
|
34
|
-
"@xyo-network/archivist-indexeddb": "^3.
|
|
35
|
-
"@xyo-network/archivist-model": "^3.
|
|
36
|
-
"@xyo-network/boundwitness-model": "^3.
|
|
37
|
-
"@xyo-network/diviner-boundwitness-abstract": "^3.
|
|
38
|
-
"@xyo-network/diviner-boundwitness-model": "^3.
|
|
39
|
-
"@xyo-network/diviner-model": "^3.
|
|
40
|
-
"@xyo-network/module-model": "^3.
|
|
41
|
-
"@xyo-network/payload-
|
|
42
|
-
"@xyo-network/payload-model": "^3.5.2",
|
|
32
|
+
"@xylabs/array": "^4.4.12",
|
|
33
|
+
"@xylabs/exists": "^4.4.12",
|
|
34
|
+
"@xyo-network/archivist-indexeddb": "^3.6.0-rc.2",
|
|
35
|
+
"@xyo-network/archivist-model": "^3.6.0-rc.2",
|
|
36
|
+
"@xyo-network/boundwitness-model": "^3.6.0-rc.2",
|
|
37
|
+
"@xyo-network/diviner-boundwitness-abstract": "^3.6.0-rc.2",
|
|
38
|
+
"@xyo-network/diviner-boundwitness-model": "^3.6.0-rc.2",
|
|
39
|
+
"@xyo-network/diviner-model": "^3.6.0-rc.2",
|
|
40
|
+
"@xyo-network/module-model": "^3.6.0-rc.2",
|
|
41
|
+
"@xyo-network/payload-model": "^3.6.0-rc.2",
|
|
43
42
|
"idb": "^8.0.0"
|
|
44
43
|
},
|
|
45
44
|
"devDependencies": {
|
|
46
45
|
"@xylabs/ts-scripts-yarn3": "^4.2.4",
|
|
47
46
|
"@xylabs/tsconfig": "^4.2.4",
|
|
48
|
-
"@xylabs/vitest-extended": "^4.4.
|
|
49
|
-
"@xyo-network/archivist-indexeddb": "^3.
|
|
50
|
-
"@xyo-network/boundwitness-builder": "^3.
|
|
51
|
-
"@xyo-network/node-memory": "^3.
|
|
52
|
-
"@xyo-network/payload-builder": "^3.
|
|
47
|
+
"@xylabs/vitest-extended": "^4.4.12",
|
|
48
|
+
"@xyo-network/archivist-indexeddb": "^3.6.0-rc.2",
|
|
49
|
+
"@xyo-network/boundwitness-builder": "^3.6.0-rc.2",
|
|
50
|
+
"@xyo-network/node-memory": "^3.6.0-rc.2",
|
|
51
|
+
"@xyo-network/payload-builder": "^3.6.0-rc.2",
|
|
53
52
|
"fake-indexeddb": "^6.0.0",
|
|
54
53
|
"typescript": "^5.7.2",
|
|
55
|
-
"vitest": "^2.1.
|
|
54
|
+
"vitest": "^2.1.8"
|
|
56
55
|
},
|
|
57
56
|
"publishConfig": {
|
|
58
57
|
"access": "public"
|
|
59
|
-
}
|
|
58
|
+
},
|
|
59
|
+
"stableVersion": "3.5.2"
|
|
60
60
|
}
|
package/src/Diviner.ts
CHANGED
|
@@ -6,7 +6,6 @@ import { BoundWitnessSchema, isBoundWitness } from '@xyo-network/boundwitness-mo
|
|
|
6
6
|
import { BoundWitnessDiviner } from '@xyo-network/diviner-boundwitness-abstract'
|
|
7
7
|
import type { BoundWitnessDivinerQueryPayload } from '@xyo-network/diviner-boundwitness-model'
|
|
8
8
|
import { isBoundWitnessDivinerQueryPayload } from '@xyo-network/diviner-boundwitness-model'
|
|
9
|
-
import { PayloadBuilder } from '@xyo-network/payload-builder'
|
|
10
9
|
import type { Schema } from '@xyo-network/payload-model'
|
|
11
10
|
import type { IDBPDatabase } from 'idb'
|
|
12
11
|
import { openDB } from 'idb'
|
|
@@ -121,9 +120,7 @@ export class IndexedDbBoundWitnessDiviner<
|
|
|
121
120
|
await tx.done
|
|
122
121
|
// Remove any metadata before returning to the client
|
|
123
122
|
return await Promise.all(
|
|
124
|
-
results.filter(isBoundWitness)
|
|
125
|
-
return PayloadBuilder.build(bw)
|
|
126
|
-
}),
|
|
123
|
+
results.filter(isBoundWitness),
|
|
127
124
|
)
|
|
128
125
|
})
|
|
129
126
|
return result ?? []
|