@xyo-network/archivist-mongodb 3.1.4 → 3.1.6

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.
@@ -1,5 +1,6 @@
1
1
  import type { Hash } from '@xylabs/hex';
2
2
  import { AbstractArchivist } from '@xyo-network/archivist-abstract';
3
+ import type { ArchivistNextOptions } from '@xyo-network/archivist-model';
3
4
  import type { Payload, Schema, WithMeta } from '@xyo-network/payload-model';
4
5
  import type { PayloadWithMongoMeta } from '@xyo-network/payload-mongodb';
5
6
  declare const MongoDBArchivistBase: (abstract new (...args: any[]) => {
@@ -67,9 +68,15 @@ export declare class MongoDBArchivist extends MongoDBArchivistBase {
67
68
  static readonly configSchemas: Schema[];
68
69
  static readonly defaultConfigSchema: Schema;
69
70
  readonly queries: string[];
71
+ /**
72
+ * The amount of time to allow the aggregate query to execute
73
+ */
74
+ protected readonly aggregateTimeoutMs = 10000;
70
75
  head(): Promise<Payload | undefined>;
76
+ protected findOneByHash(hash: Hash): Promise<import("mongodb").WithId<PayloadWithMongoMeta> | undefined>;
71
77
  protected getHandler(hashes: Hash[]): Promise<WithMeta<Payload>[]>;
72
78
  protected insertHandler(payloads: Payload[]): Promise<WithMeta<Payload>[]>;
79
+ protected nextHandler(options?: ArchivistNextOptions): Promise<WithMeta<Payload>[]>;
73
80
  protected startHandler(): Promise<boolean>;
74
81
  }
75
82
  export {};
@@ -1 +1 @@
1
- {"version":3,"file":"Archivist.d.ts","sourceRoot":"","sources":["../../src/Archivist.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,aAAa,CAAA;AACvC,OAAO,EAAE,iBAAiB,EAAE,MAAM,iCAAiC,CAAA;AAKnE,OAAO,KAAK,EACV,OAAO,EAAE,MAAM,EAAE,QAAQ,EAC1B,MAAM,4BAA4B,CAAA;AACnC,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,8BAA8B,CAAA;AAMxE,QAAA,MAAM,o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,CAAC,EAAE,CAAC;cAkBhE,WAAW,CAAC,OAAO,CAAC,EAAE,oBAAoB,GAAG,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE,CAAC;cAkEzE,YAAY;CAKtC"}
@@ -4,12 +4,13 @@ var __name = (target, value) => __defProp(target, "name", { value, configurable:
4
4
  // src/Archivist.ts
5
5
  import { exists } from "@xylabs/exists";
6
6
  import { AbstractArchivist } from "@xyo-network/archivist-abstract";
7
- import { ArchivistInsertQuerySchema } from "@xyo-network/archivist-model";
7
+ import { ArchivistInsertQuerySchema, ArchivistNextQuerySchema } from "@xyo-network/archivist-model";
8
8
  import { MongoDBArchivistConfigSchema } from "@xyo-network/archivist-model-mongodb";
9
9
  import { MongoDBModuleMixin } from "@xyo-network/module-abstract-mongodb";
10
10
  import { PayloadBuilder } from "@xyo-network/payload-builder";
11
11
  import { fromDbRepresentation, toDbRepresentation } from "@xyo-network/payload-mongodb";
12
12
  import { PayloadWrapper as PayloadWrapper2 } from "@xyo-network/payload-wrapper";
13
+ import { ObjectId } from "mongodb";
13
14
 
14
15
  // src/lib/validByType.ts
15
16
  import { isBoundWitness, isQueryBoundWitness } from "@xyo-network/boundwitness-model";
@@ -54,14 +55,47 @@ var MongoDBArchivist = class extends MongoDBArchivistBase {
54
55
  static defaultConfigSchema = MongoDBArchivistConfigSchema;
55
56
  queries = [
56
57
  ArchivistInsertQuerySchema,
58
+ ArchivistNextQuerySchema,
57
59
  ...super.queries
58
60
  ];
61
+ /**
62
+ * The amount of time to allow the aggregate query to execute
63
+ */
64
+ aggregateTimeoutMs = 1e4;
59
65
  async head() {
60
- const head = await (await this.payloads.find({})).sort({
61
- _timestamp: -1
62
- }).limit(1).toArray();
66
+ const head = await this.next({
67
+ limit: 1,
68
+ order: "desc"
69
+ });
63
70
  return head[0] ? PayloadWrapper2.wrap(head[0]).payload : void 0;
64
71
  }
72
+ async findOneByHash(hash) {
73
+ const dataPayload = await this.payloads.findOne({
74
+ _$hash: hash
75
+ });
76
+ if (dataPayload) {
77
+ return dataPayload;
78
+ } else {
79
+ const dataBw = await this.boundWitnesses.findOne({
80
+ _$hash: hash
81
+ });
82
+ if (dataBw) {
83
+ return dataBw;
84
+ } else {
85
+ const payload = await this.payloads.findOne({
86
+ _hash: hash
87
+ });
88
+ if (payload) {
89
+ return payload;
90
+ } else {
91
+ const bw = await this.boundWitnesses.findOne({
92
+ _hash: hash
93
+ });
94
+ return bw ?? void 0;
95
+ }
96
+ }
97
+ }
98
+ }
65
99
  async getHandler(hashes) {
66
100
  let remainingHashes = [
67
101
  ...hashes
@@ -110,7 +144,82 @@ var MongoDBArchivist = class extends MongoDBArchivistBase {
110
144
  return await PayloadBuilder.build([
111
145
  ...boundWitnessesWithExternalMeta,
112
146
  ...payloadsWithExternalMeta
113
- ]);
147
+ ].map(fromDbRepresentation));
148
+ }
149
+ async nextHandler(options) {
150
+ let { limit, offset, order } = options ?? {
151
+ limit: 10,
152
+ order: "desc"
153
+ };
154
+ if (!limit) limit = 10;
155
+ if (limit > 100) limit = 100;
156
+ if (order != "asc") order = "desc";
157
+ let id;
158
+ if (offset) {
159
+ const payload = await this.findOneByHash(offset);
160
+ if (payload) id = payload._id;
161
+ } else {
162
+ id = order === "asc" ? ObjectId.createFromTime(0) : ObjectId.createFromTime((Date.now() + 1e4) / 1e3);
163
+ }
164
+ if (!id) return [];
165
+ const sort = order === "asc" ? 1 : -1;
166
+ const match = order === "asc" ? {
167
+ _id: {
168
+ $gt: id
169
+ }
170
+ } : {
171
+ _id: {
172
+ $lt: id
173
+ }
174
+ };
175
+ const foundPayloads = await this.payloads.useCollection((collection) => {
176
+ return collection.aggregate([
177
+ // Pre-filter payloads collection
178
+ {
179
+ $match: match
180
+ },
181
+ // Sort payloads by _id
182
+ {
183
+ $sort: {
184
+ _id: sort
185
+ }
186
+ },
187
+ // Limit payloads to the first N payloads
188
+ {
189
+ $limit: limit
190
+ },
191
+ // Combine with filtered boundWitnesses collection
192
+ {
193
+ $unionWith: {
194
+ coll: this.boundWitnessSdkConfig.collection,
195
+ pipeline: [
196
+ {
197
+ $match: match
198
+ },
199
+ {
200
+ $sort: {
201
+ _id: sort
202
+ }
203
+ },
204
+ {
205
+ $limit: limit
206
+ }
207
+ ]
208
+ }
209
+ },
210
+ // Sort the combined result by _id
211
+ {
212
+ $sort: {
213
+ _id: sort
214
+ }
215
+ },
216
+ // Limit the final result to N documents
217
+ {
218
+ $limit: limit
219
+ }
220
+ ]).maxTimeMS(this.aggregateTimeoutMs).toArray();
221
+ });
222
+ return await PayloadBuilder.build(foundPayloads.map(fromDbRepresentation));
114
223
  }
115
224
  async startHandler() {
116
225
  await super.startHandler();
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/Archivist.ts","../../src/lib/validByType.ts","../../src/index.ts"],"sourcesContent":["import { exists } from '@xylabs/exists'\nimport type { Hash } from '@xylabs/hex'\nimport { AbstractArchivist } from '@xyo-network/archivist-abstract'\nimport { ArchivistInsertQuerySchema } from '@xyo-network/archivist-model'\nimport { MongoDBArchivistConfigSchema } from '@xyo-network/archivist-model-mongodb'\nimport { MongoDBModuleMixin } from '@xyo-network/module-abstract-mongodb'\nimport { PayloadBuilder } from '@xyo-network/payload-builder'\nimport type {\n Payload, Schema, WithMeta,\n} from '@xyo-network/payload-model'\nimport type { PayloadWithMongoMeta } from '@xyo-network/payload-mongodb'\nimport { fromDbRepresentation, toDbRepresentation } from '@xyo-network/payload-mongodb'\nimport { PayloadWrapper } from '@xyo-network/payload-wrapper'\n\nimport { validByType } from './lib/index.js'\n\nconst MongoDBArchivistBase = MongoDBModuleMixin(AbstractArchivist)\n\nexport class MongoDBArchivist extends MongoDBArchivistBase {\n static override readonly configSchemas: Schema[] = [...super.configSchemas, MongoDBArchivistConfigSchema]\n static override readonly defaultConfigSchema: Schema = MongoDBArchivistConfigSchema\n\n override readonly queries: string[] = [ArchivistInsertQuerySchema, ...super.queries]\n\n override async head(): Promise<Payload | undefined> {\n const head = await (await this.payloads.find({})).sort({ _timestamp: -1 }).limit(1).toArray()\n return head[0] ? PayloadWrapper.wrap(head[0]).payload : undefined\n }\n\n protected override async getHandler(hashes: Hash[]): Promise<WithMeta<Payload>[]> {\n let remainingHashes = [...hashes]\n\n const dataPayloads = (await Promise.all(remainingHashes.map(_$hash => this.payloads.findOne({ _$hash })))).filter(exists)\n const dataPayloadsHashes = new Set(dataPayloads.map(payload => payload._$hash))\n remainingHashes = remainingHashes.filter(hash => !dataPayloadsHashes.has(hash))\n\n const dataBws = (await Promise.all(remainingHashes.map(_$hash => this.boundWitnesses.findOne({ _$hash })))).filter(exists)\n const dataBwsHashes = new Set(dataBws.map(payload => payload._$hash))\n remainingHashes = remainingHashes.filter(hash => !dataBwsHashes.has(hash))\n\n const payloads = (await Promise.all(remainingHashes.map(_hash => this.payloads.findOne({ _hash })))).filter(exists)\n const payloadsHashes = new Set(payloads.map(payload => payload._hash))\n remainingHashes = remainingHashes.filter(hash => !payloadsHashes.has(hash))\n\n const bws = (await Promise.all(remainingHashes.map(_hash => this.boundWitnesses.findOne({ _hash })))).filter(exists)\n const bwsHashes = new Set(bws.map(payload => payload._hash))\n remainingHashes = remainingHashes.filter(hash => !bwsHashes.has(hash))\n\n const foundPayloads = [...dataPayloads, ...dataBws, ...payloads, ...bws] as PayloadWithMongoMeta<Payload & { _$hash: Hash; _$meta?: unknown }>[]\n const result = await PayloadBuilder.build(foundPayloads.map(fromDbRepresentation))\n // console.log(`getHandler: ${JSON.stringify(hashes, null, 2)}:${JSON.stringify(result, null, 2)}`)\n return result\n }\n\n protected override async insertHandler(payloads: Payload[]): Promise<WithMeta<Payload>[]> {\n const [bw, p] = await validByType(payloads)\n const payloadsWithExternalMeta = await Promise.all(p.map((value, index) => toDbRepresentation(value, index)))\n if (payloadsWithExternalMeta.length > 0) {\n const payloadsResult = await this.payloads.insertMany(payloadsWithExternalMeta)\n if (!payloadsResult.acknowledged || payloadsResult.insertedCount !== payloadsWithExternalMeta.length)\n throw new Error('MongoDBDeterministicArchivist: Error inserting Payloads')\n }\n const boundWitnessesWithExternalMeta = await Promise.all(bw.map((value, index) => toDbRepresentation(value, index)))\n if (boundWitnessesWithExternalMeta.length > 0) {\n const boundWitnessesResult = await this.boundWitnesses.insertMany(boundWitnessesWithExternalMeta)\n if (!boundWitnessesResult.acknowledged || boundWitnessesResult.insertedCount !== boundWitnessesWithExternalMeta.length)\n throw new Error('MongoDBDeterministicArchivist: Error inserting BoundWitnesses')\n }\n\n return await PayloadBuilder.build([...boundWitnessesWithExternalMeta, ...payloadsWithExternalMeta])\n }\n\n protected override async startHandler() {\n await super.startHandler()\n await this.ensureIndexes()\n return true\n }\n}\n","import type { BoundWitness } from '@xyo-network/boundwitness-model'\nimport { isBoundWitness, isQueryBoundWitness } from '@xyo-network/boundwitness-model'\nimport { BoundWitnessWrapper, QueryBoundWitnessWrapper } from '@xyo-network/boundwitness-wrapper'\nimport type { Payload } from '@xyo-network/payload-model'\nimport { PayloadWrapper } from '@xyo-network/payload-wrapper'\n\nexport const validByType = async (payloads: Payload[] = []) => {\n const results: [BoundWitness[], Payload[]] = [[], []]\n await Promise.all(\n payloads.map(async (payload) => {\n if (isBoundWitness(payload)) {\n const wrapper = isQueryBoundWitness(payload) ? QueryBoundWitnessWrapper : BoundWitnessWrapper\n const bw = wrapper.parse(payload)\n if (await bw.getValid()) {\n results[0].push(bw.payload)\n } else {\n const errors = await bw.getErrors()\n console.log(`validByType.Error: ${JSON.stringify(errors, null, 2)}`)\n }\n } else {\n const payloadWrapper = PayloadWrapper.wrap(payload)\n if (await payloadWrapper.getValid()) {\n results[1].push(payloadWrapper.payload)\n }\n }\n return\n }),\n )\n return results\n}\n","export * from './Archivist.js'\nexport * from '@xyo-network/archivist-model-mongodb'\n"],"mappings":";;;;AAAA,SAASA,cAAc;AAEvB,SAASC,yBAAyB;AAClC,SAASC,kCAAkC;AAC3C,SAASC,oCAAoC;AAC7C,SAASC,0BAA0B;AACnC,SAASC,sBAAsB;AAK/B,SAASC,sBAAsBC,0BAA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","MongoDBArchivistConfigSchema","MongoDBModuleMixin","PayloadBuilder","fromDbRepresentation","toDbRepresentation","PayloadWrapper","isBoundWitness","isQueryBoundWitness","BoundWitnessWrapper","QueryBoundWitnessWrapper","PayloadWrapper","validByType","payloads","results","Promise","all","map","payload","isBoundWitness","wrapper","isQueryBoundWitness","QueryBoundWitnessWrapper","BoundWitnessWrapper","bw","parse","getValid","push","errors","getErrors","console","log","JSON","stringify","payloadWrapper","PayloadWrapper","wrap","MongoDBArchivistBase","MongoDBModuleMixin","AbstractArchivist","MongoDBArchivist","configSchemas","MongoDBArchivistConfigSchema","defaultConfigSchema","queries","ArchivistInsertQuerySchema","head","payloads","find","sort","_timestamp","limit","toArray","PayloadWrapper","wrap","payload","undefined","getHandler","hashes","remainingHashes","dataPayloads","Promise","all","map","_$hash","findOne","filter","exists","dataPayloadsHashes","Set","hash","has","dataBws","boundWitnesses","dataBwsHashes","_hash","payloadsHashes","bws","bwsHashes","foundPayloads","result","PayloadBuilder","build","fromDbRepresentation","insertHandler","bw","p","validByType","payloadsWithExternalMeta","value","index","toDbRepresentation","length","payloadsResult","insertMany","acknowledged","insertedCount","Error","boundWitnessesWithExternalMeta","boundWitnessesResult","startHandler","ensureIndexes"]}
1
+ {"version":3,"sources":["../../src/Archivist.ts","../../src/lib/validByType.ts","../../src/index.ts"],"sourcesContent":["import { exists } from '@xylabs/exists'\nimport type { Hash } from '@xylabs/hex'\nimport { AbstractArchivist } from '@xyo-network/archivist-abstract'\nimport type { ArchivistNextOptions } from '@xyo-network/archivist-model'\nimport { ArchivistInsertQuerySchema, ArchivistNextQuerySchema } from '@xyo-network/archivist-model'\nimport { MongoDBArchivistConfigSchema } from '@xyo-network/archivist-model-mongodb'\nimport { MongoDBModuleMixin } from '@xyo-network/module-abstract-mongodb'\nimport { PayloadBuilder } from '@xyo-network/payload-builder'\nimport type {\n Payload, Schema, WithMeta,\n} from '@xyo-network/payload-model'\nimport type { PayloadWithMongoMeta } from '@xyo-network/payload-mongodb'\nimport { fromDbRepresentation, toDbRepresentation } from '@xyo-network/payload-mongodb'\nimport { PayloadWrapper } from '@xyo-network/payload-wrapper'\nimport { ObjectId } from 'mongodb'\n\nimport { validByType } from './lib/index.js'\n\nconst MongoDBArchivistBase = MongoDBModuleMixin(AbstractArchivist)\n\nexport class MongoDBArchivist extends MongoDBArchivistBase {\n static override readonly configSchemas: Schema[] = [...super.configSchemas, MongoDBArchivistConfigSchema]\n static override readonly defaultConfigSchema: Schema = MongoDBArchivistConfigSchema\n\n override readonly queries: string[] = [ArchivistInsertQuerySchema, ArchivistNextQuerySchema, ...super.queries]\n\n /**\n * The amount of time to allow the aggregate query to execute\n */\n protected readonly aggregateTimeoutMs = 10_000\n\n override async head(): Promise<Payload | undefined> {\n const head = await this.next({ limit: 1, order: 'desc' })\n return head[0] ? PayloadWrapper.wrap(head[0]).payload : undefined\n }\n\n protected async findOneByHash(hash: Hash) {\n const dataPayload = (await this.payloads.findOne({ _$hash: hash }))\n if (dataPayload) {\n return dataPayload\n } else {\n const dataBw = (await this.boundWitnesses.findOne({ _$hash: hash }))\n if (dataBw) {\n return dataBw\n } else {\n const payload = (await this.payloads.findOne({ _hash: hash }))\n if (payload) {\n return payload\n } else {\n const bw = (await this.boundWitnesses.findOne({ _hash: hash }))\n return bw ?? undefined\n }\n }\n }\n }\n\n protected override async getHandler(hashes: Hash[]): Promise<WithMeta<Payload>[]> {\n let remainingHashes = [...hashes]\n\n const dataPayloads = (await Promise.all(remainingHashes.map(_$hash => this.payloads.findOne({ _$hash })))).filter(exists)\n const dataPayloadsHashes = new Set(dataPayloads.map(payload => payload._$hash))\n remainingHashes = remainingHashes.filter(hash => !dataPayloadsHashes.has(hash))\n\n const dataBws = (await Promise.all(remainingHashes.map(_$hash => this.boundWitnesses.findOne({ _$hash })))).filter(exists)\n const dataBwsHashes = new Set(dataBws.map(payload => payload._$hash))\n remainingHashes = remainingHashes.filter(hash => !dataBwsHashes.has(hash))\n\n const payloads = (await Promise.all(remainingHashes.map(_hash => this.payloads.findOne({ _hash })))).filter(exists)\n const payloadsHashes = new Set(payloads.map(payload => payload._hash))\n remainingHashes = remainingHashes.filter(hash => !payloadsHashes.has(hash))\n\n const bws = (await Promise.all(remainingHashes.map(_hash => this.boundWitnesses.findOne({ _hash })))).filter(exists)\n const bwsHashes = new Set(bws.map(payload => payload._hash))\n remainingHashes = remainingHashes.filter(hash => !bwsHashes.has(hash))\n\n const foundPayloads = [...dataPayloads, ...dataBws, ...payloads, ...bws] as PayloadWithMongoMeta<Payload & { _$hash: Hash; _$meta?: unknown }>[]\n const result = await PayloadBuilder.build(foundPayloads.map(fromDbRepresentation))\n // console.log(`getHandler: ${JSON.stringify(hashes, null, 2)}:${JSON.stringify(result, null, 2)}`)\n return result\n }\n\n protected override async insertHandler(payloads: Payload[]): Promise<WithMeta<Payload>[]> {\n const [bw, p] = await validByType(payloads)\n const payloadsWithExternalMeta = await Promise.all(p.map((value, index) => toDbRepresentation(value, index)))\n if (payloadsWithExternalMeta.length > 0) {\n const payloadsResult = await this.payloads.insertMany(payloadsWithExternalMeta)\n if (!payloadsResult.acknowledged || payloadsResult.insertedCount !== payloadsWithExternalMeta.length)\n throw new Error('MongoDBDeterministicArchivist: Error inserting Payloads')\n }\n const boundWitnessesWithExternalMeta = await Promise.all(bw.map((value, index) => toDbRepresentation(value, index)))\n if (boundWitnessesWithExternalMeta.length > 0) {\n const boundWitnessesResult = await this.boundWitnesses.insertMany(boundWitnessesWithExternalMeta)\n if (!boundWitnessesResult.acknowledged || boundWitnessesResult.insertedCount !== boundWitnessesWithExternalMeta.length)\n throw new Error('MongoDBDeterministicArchivist: Error inserting BoundWitnesses')\n }\n\n return await PayloadBuilder.build([...boundWitnessesWithExternalMeta, ...payloadsWithExternalMeta].map(fromDbRepresentation))\n }\n\n protected override async nextHandler(options?: ArchivistNextOptions): Promise<WithMeta<Payload>[]> {\n // Sanitize inputs and set defaults\n let {\n limit, offset, order,\n } = options ?? { limit: 10, order: 'desc' }\n\n if (!limit) limit = 10\n if (limit > 100) limit = 100\n\n if (order != 'asc') order = 'desc'\n\n let id: ObjectId | undefined\n if (offset) {\n const payload = await this.findOneByHash(offset)\n // TODO: Should we throw an error if the requested payload is not found?\n if (payload) id = payload._id\n } else {\n id = order === 'asc'\n // If ascending, start from the beginning of time\n ? ObjectId.createFromTime(0)\n // If descending, start from now (plus a bit more in the future to ensure\n // them most recent ObjectIds are included)\n : ObjectId.createFromTime((Date.now() + 10_000) / 1000)\n }\n if (!id) return []\n\n // Create aggregate criteria\n const sort = order === 'asc' ? 1 : -1\n // TODO: How to handle random component of ID across multiple collections\n // to ensure we don't skip some payloads\n const match = order === 'asc' ? { _id: { $gt: id } } : { _id: { $lt: id } }\n\n // Run the aggregate query\n const foundPayloads = await this.payloads.useCollection((collection) => {\n return collection\n .aggregate<PayloadWithMongoMeta>([\n // Pre-filter payloads collection\n { $match: match },\n // Sort payloads by _id\n { $sort: { _id: sort } },\n // Limit payloads to the first N payloads\n { $limit: limit },\n // Combine with filtered boundWitnesses collection\n {\n $unionWith: {\n coll: this.boundWitnessSdkConfig.collection,\n pipeline: [\n { $match: match }, // Pre-filter boundWitnesses\n { $sort: { _id: sort } }, // Sort boundWitnesses by _id\n { $limit: limit }, // Limit boundWitnesses to the first N boundWitnesses\n ],\n },\n },\n // Sort the combined result by _id\n { $sort: { _id: sort } },\n // Limit the final result to N documents\n { $limit: limit },\n ])\n .maxTimeMS(this.aggregateTimeoutMs)\n .toArray()\n })\n\n // Convert from DB representation to Payloads\n return await PayloadBuilder.build(foundPayloads.map(fromDbRepresentation))\n }\n\n protected override async startHandler() {\n await super.startHandler()\n await this.ensureIndexes()\n return true\n }\n}\n","import type { BoundWitness } from '@xyo-network/boundwitness-model'\nimport { isBoundWitness, isQueryBoundWitness } from '@xyo-network/boundwitness-model'\nimport { BoundWitnessWrapper, QueryBoundWitnessWrapper } from '@xyo-network/boundwitness-wrapper'\nimport type { Payload } from '@xyo-network/payload-model'\nimport { PayloadWrapper } from '@xyo-network/payload-wrapper'\n\nexport const validByType = async (payloads: Payload[] = []) => {\n const results: [BoundWitness[], Payload[]] = [[], []]\n await Promise.all(\n payloads.map(async (payload) => {\n if (isBoundWitness(payload)) {\n const wrapper = isQueryBoundWitness(payload) ? QueryBoundWitnessWrapper : BoundWitnessWrapper\n const bw = wrapper.parse(payload)\n if (await bw.getValid()) {\n results[0].push(bw.payload)\n } else {\n const errors = await bw.getErrors()\n console.log(`validByType.Error: ${JSON.stringify(errors, null, 2)}`)\n }\n } else {\n const payloadWrapper = PayloadWrapper.wrap(payload)\n if (await payloadWrapper.getValid()) {\n results[1].push(payloadWrapper.payload)\n }\n }\n return\n }),\n )\n return results\n}\n","export * from './Archivist.js'\nexport * from '@xyo-network/archivist-model-mongodb'\n"],"mappings":";;;;AAAA,SAASA,cAAc;AAEvB,SAASC,yBAAyB;AAElC,SAASC,4BAA4BC,gCAAgC;AACrE,SAASC,oCAAoC;AAC7C,SAASC,0BAA0B;AACnC,SAASC,sBAAsB;AAK/B,SAASC,sBAAsBC,0BAA0B;AACzD,SAASC,kBAAAA,uBAAsB;AAC/B,SAASC,gBAAgB;;;ACbzB,SAASC,gBAAgBC,2BAA2B;AACpD,SAASC,qBAAqBC,gCAAgC;AAE9D,SAASC,sBAAsB;AAExB,IAAMC,cAAc,8BAAOC,WAAsB,CAAA,MAAE;AACxD,QAAMC,UAAuC;IAAC,CAAA;IAAI,CAAA;;AAClD,QAAMC,QAAQC,IACZH,SAASI,IAAI,OAAOC,YAAAA;AAClB,QAAIC,eAAeD,OAAAA,GAAU;AAC3B,YAAME,UAAUC,oBAAoBH,OAAAA,IAAWI,2BAA2BC;AAC1E,YAAMC,KAAKJ,QAAQK,MAAMP,OAAAA;AACzB,UAAI,MAAMM,GAAGE,SAAQ,GAAI;AACvBZ,gBAAQ,CAAA,EAAGa,KAAKH,GAAGN,OAAO;MAC5B,OAAO;AACL,cAAMU,SAAS,MAAMJ,GAAGK,UAAS;AACjCC,gBAAQC,IAA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chema","MongoDBModuleMixin","PayloadBuilder","fromDbRepresentation","toDbRepresentation","PayloadWrapper","ObjectId","isBoundWitness","isQueryBoundWitness","BoundWitnessWrapper","QueryBoundWitnessWrapper","PayloadWrapper","validByType","payloads","results","Promise","all","map","payload","isBoundWitness","wrapper","isQueryBoundWitness","QueryBoundWitnessWrapper","BoundWitnessWrapper","bw","parse","getValid","push","errors","getErrors","console","log","JSON","stringify","payloadWrapper","PayloadWrapper","wrap","MongoDBArchivistBase","MongoDBModuleMixin","AbstractArchivist","MongoDBArchivist","configSchemas","MongoDBArchivistConfigSchema","defaultConfigSchema","queries","ArchivistInsertQuerySchema","ArchivistNextQuerySchema","aggregateTimeoutMs","head","next","limit","order","PayloadWrapper","wrap","payload","undefined","findOneByHash","hash","dataPayload","payloads","findOne","_$hash","dataBw","boundWitnesses","_hash","bw","getHandler","hashes","remainingHashes","dataPayloads","Promise","all","map","filter","exists","dataPayloadsHashes","Set","has","dataBws","dataBwsHashes","payloadsHashes","bws","bwsHashes","foundPayloads","result","PayloadBuilder","build","fromDbRepresentation","insertHandler","p","validByType","payloadsWithExternalMeta","value","index","toDbRepresentation","length","payloadsResult","insertMany","acknowledged","insertedCount","Error","boundWitnessesWithExternalMeta","boundWitnessesResult","nextHandler","options","offset","id","_id","ObjectId","createFromTime","Date","now","sort","match","$gt","$lt","useCollection","collection","aggregate","$match","$sort","$limit","$unionWith","coll","boundWitnessSdkConfig","pipeline","maxTimeMS","toArray","startHandler","ensureIndexes"]}
@@ -1,5 +1,6 @@
1
1
  import type { Hash } from '@xylabs/hex';
2
2
  import { AbstractArchivist } from '@xyo-network/archivist-abstract';
3
+ import type { ArchivistNextOptions } from '@xyo-network/archivist-model';
3
4
  import type { Payload, Schema, WithMeta } from '@xyo-network/payload-model';
4
5
  import type { PayloadWithMongoMeta } from '@xyo-network/payload-mongodb';
5
6
  declare const MongoDBArchivistBase: (abstract new (...args: any[]) => {
@@ -67,9 +68,15 @@ export declare class MongoDBArchivist extends MongoDBArchivistBase {
67
68
  static readonly configSchemas: Schema[];
68
69
  static readonly defaultConfigSchema: Schema;
69
70
  readonly queries: string[];
71
+ /**
72
+ * The amount of time to allow the aggregate query to execute
73
+ */
74
+ protected readonly aggregateTimeoutMs = 10000;
70
75
  head(): Promise<Payload | undefined>;
76
+ protected findOneByHash(hash: Hash): Promise<import("mongodb").WithId<PayloadWithMongoMeta> | undefined>;
71
77
  protected getHandler(hashes: Hash[]): Promise<WithMeta<Payload>[]>;
72
78
  protected insertHandler(payloads: Payload[]): Promise<WithMeta<Payload>[]>;
79
+ protected nextHandler(options?: ArchivistNextOptions): Promise<WithMeta<Payload>[]>;
73
80
  protected startHandler(): Promise<boolean>;
74
81
  }
75
82
  export {};
@@ -1 +1 @@
1
- {"version":3,"file":"Archivist.d.ts","sourceRoot":"","sources":["../../src/Archivist.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,aAAa,CAAA;AACvC,OAAO,EAAE,iBAAiB,EAAE,MAAM,iCAAiC,CAAA;AAKnE,OAAO,KAAK,EACV,OAAO,EAAE,MAAM,EAAE,QAAQ,EAC1B,MAAM,4BAA4B,CAAA;AACnC,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,8BAA8B,CAAA;AAMxE,QAAA,MAAM,oBAAoB;;;;;8BAgCoD,8BAA8B;;;;oBAOtE,aACnC;;+CA/B0E,+BACnE;0CAGQ,+BAA+B;uBACtB,mCAEJ;;;;;;;qBAwB2D,2BAA2B;;yBA7CrF,4BAA4B;;mCACG,4BAEnC;;uBAE6B,4BAExC,wBAAwB,4BACjC,8BAA8B,2BAA2B,iCAAiC,2BACjF,8BAA8B,4BAA4B,wBAE/D,4BAA4B,8BAA8B,2BAE7D,iCAAiC,2BAC7B;iCAAsD,CAAC,iBAAiB,+BACnE;4BAGR,CAAD,iBAAiB,+BAA+B;2BACtB,mCAEJ;;;;;;;8CAE+B,2BAA2B;;wCAEV,+BAA+B,6CAClF,+BAA+B;0BAC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pC,2BAA2B;+CACxB,2BAGrE,oEAAoE,2BAA2B;2CAGrC,2BAA2B,0CAC7D,2BAEvB;wCAGG,2BACD,mFAIS,4BACL,uBACK,2BAA2B;4BACoB,4BACnD;uCAGM,2BAA2B,mFAET,4BAA2B,uBAEnD,2BADmB,gDACJ,4BAA4B;2BAIvC,4BACL,4BAA4B,4BACd;yCACT,2BAA2B,mEACsB,4BAErD,uBACJ,2BAAuB,gDAAiD,4BAE3E;;;4BApHiE,CAAA;AAElE,qBAAa,gBAAiB,SAAQ,oBAAoB;IACxD,gBAAyB,aAAa,EAAE,MAAM,EAAE,CAAyD;IACzG,gBAAyB,mBAAmB,EAAE,MAAM,CAA+B;IAEnF,SAAkB,OAAO,EAAE,MAAM,EAAE,CAA2E;IAE9G;;OAEG;IACH,SAAS,CAAC,QAAQ,CAAC,kBAAkB,SAAS;IAE/B,IAAI,IAAI,OAAO,CAAC,OAAO,GAAG,SAAS,CAAC;cAKnC,aAAa,CAAC,IAAI,EAAE,IAAI;cAoBf,UAAU,CAAC,MAAM,EAAE,IAAI,EAAE,GAAG,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE,CAAC;cAyBxD,aAAa,CAAC,QAAQ,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE,CAAC;cAkBhE,WAAW,CAAC,OAAO,CAAC,EAAE,oBAAoB,GAAG,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE,CAAC;cAkEzE,YAAY;CAKtC"}
@@ -4,12 +4,13 @@ var __name = (target, value) => __defProp(target, "name", { value, configurable:
4
4
  // src/Archivist.ts
5
5
  import { exists } from "@xylabs/exists";
6
6
  import { AbstractArchivist } from "@xyo-network/archivist-abstract";
7
- import { ArchivistInsertQuerySchema } from "@xyo-network/archivist-model";
7
+ import { ArchivistInsertQuerySchema, ArchivistNextQuerySchema } from "@xyo-network/archivist-model";
8
8
  import { MongoDBArchivistConfigSchema } from "@xyo-network/archivist-model-mongodb";
9
9
  import { MongoDBModuleMixin } from "@xyo-network/module-abstract-mongodb";
10
10
  import { PayloadBuilder } from "@xyo-network/payload-builder";
11
11
  import { fromDbRepresentation, toDbRepresentation } from "@xyo-network/payload-mongodb";
12
12
  import { PayloadWrapper as PayloadWrapper2 } from "@xyo-network/payload-wrapper";
13
+ import { ObjectId } from "mongodb";
13
14
 
14
15
  // src/lib/validByType.ts
15
16
  import { isBoundWitness, isQueryBoundWitness } from "@xyo-network/boundwitness-model";
@@ -54,14 +55,47 @@ var MongoDBArchivist = class extends MongoDBArchivistBase {
54
55
  static defaultConfigSchema = MongoDBArchivistConfigSchema;
55
56
  queries = [
56
57
  ArchivistInsertQuerySchema,
58
+ ArchivistNextQuerySchema,
57
59
  ...super.queries
58
60
  ];
61
+ /**
62
+ * The amount of time to allow the aggregate query to execute
63
+ */
64
+ aggregateTimeoutMs = 1e4;
59
65
  async head() {
60
- const head = await (await this.payloads.find({})).sort({
61
- _timestamp: -1
62
- }).limit(1).toArray();
66
+ const head = await this.next({
67
+ limit: 1,
68
+ order: "desc"
69
+ });
63
70
  return head[0] ? PayloadWrapper2.wrap(head[0]).payload : void 0;
64
71
  }
72
+ async findOneByHash(hash) {
73
+ const dataPayload = await this.payloads.findOne({
74
+ _$hash: hash
75
+ });
76
+ if (dataPayload) {
77
+ return dataPayload;
78
+ } else {
79
+ const dataBw = await this.boundWitnesses.findOne({
80
+ _$hash: hash
81
+ });
82
+ if (dataBw) {
83
+ return dataBw;
84
+ } else {
85
+ const payload = await this.payloads.findOne({
86
+ _hash: hash
87
+ });
88
+ if (payload) {
89
+ return payload;
90
+ } else {
91
+ const bw = await this.boundWitnesses.findOne({
92
+ _hash: hash
93
+ });
94
+ return bw ?? void 0;
95
+ }
96
+ }
97
+ }
98
+ }
65
99
  async getHandler(hashes) {
66
100
  let remainingHashes = [
67
101
  ...hashes
@@ -110,7 +144,82 @@ var MongoDBArchivist = class extends MongoDBArchivistBase {
110
144
  return await PayloadBuilder.build([
111
145
  ...boundWitnessesWithExternalMeta,
112
146
  ...payloadsWithExternalMeta
113
- ]);
147
+ ].map(fromDbRepresentation));
148
+ }
149
+ async nextHandler(options) {
150
+ let { limit, offset, order } = options ?? {
151
+ limit: 10,
152
+ order: "desc"
153
+ };
154
+ if (!limit) limit = 10;
155
+ if (limit > 100) limit = 100;
156
+ if (order != "asc") order = "desc";
157
+ let id;
158
+ if (offset) {
159
+ const payload = await this.findOneByHash(offset);
160
+ if (payload) id = payload._id;
161
+ } else {
162
+ id = order === "asc" ? ObjectId.createFromTime(0) : ObjectId.createFromTime((Date.now() + 1e4) / 1e3);
163
+ }
164
+ if (!id) return [];
165
+ const sort = order === "asc" ? 1 : -1;
166
+ const match = order === "asc" ? {
167
+ _id: {
168
+ $gt: id
169
+ }
170
+ } : {
171
+ _id: {
172
+ $lt: id
173
+ }
174
+ };
175
+ const foundPayloads = await this.payloads.useCollection((collection) => {
176
+ return collection.aggregate([
177
+ // Pre-filter payloads collection
178
+ {
179
+ $match: match
180
+ },
181
+ // Sort payloads by _id
182
+ {
183
+ $sort: {
184
+ _id: sort
185
+ }
186
+ },
187
+ // Limit payloads to the first N payloads
188
+ {
189
+ $limit: limit
190
+ },
191
+ // Combine with filtered boundWitnesses collection
192
+ {
193
+ $unionWith: {
194
+ coll: this.boundWitnessSdkConfig.collection,
195
+ pipeline: [
196
+ {
197
+ $match: match
198
+ },
199
+ {
200
+ $sort: {
201
+ _id: sort
202
+ }
203
+ },
204
+ {
205
+ $limit: limit
206
+ }
207
+ ]
208
+ }
209
+ },
210
+ // Sort the combined result by _id
211
+ {
212
+ $sort: {
213
+ _id: sort
214
+ }
215
+ },
216
+ // Limit the final result to N documents
217
+ {
218
+ $limit: limit
219
+ }
220
+ ]).maxTimeMS(this.aggregateTimeoutMs).toArray();
221
+ });
222
+ return await PayloadBuilder.build(foundPayloads.map(fromDbRepresentation));
114
223
  }
115
224
  async startHandler() {
116
225
  await super.startHandler();
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/Archivist.ts","../../src/lib/validByType.ts","../../src/index.ts"],"sourcesContent":["import { exists } from '@xylabs/exists'\nimport type { Hash } from '@xylabs/hex'\nimport { AbstractArchivist } from '@xyo-network/archivist-abstract'\nimport { ArchivistInsertQuerySchema } from '@xyo-network/archivist-model'\nimport { MongoDBArchivistConfigSchema } from '@xyo-network/archivist-model-mongodb'\nimport { MongoDBModuleMixin } from '@xyo-network/module-abstract-mongodb'\nimport { PayloadBuilder } from '@xyo-network/payload-builder'\nimport type {\n Payload, Schema, WithMeta,\n} from '@xyo-network/payload-model'\nimport type { PayloadWithMongoMeta } from '@xyo-network/payload-mongodb'\nimport { fromDbRepresentation, toDbRepresentation } from '@xyo-network/payload-mongodb'\nimport { PayloadWrapper } from '@xyo-network/payload-wrapper'\n\nimport { validByType } from './lib/index.js'\n\nconst MongoDBArchivistBase = MongoDBModuleMixin(AbstractArchivist)\n\nexport class MongoDBArchivist extends MongoDBArchivistBase {\n static override readonly configSchemas: Schema[] = [...super.configSchemas, MongoDBArchivistConfigSchema]\n static override readonly defaultConfigSchema: Schema = MongoDBArchivistConfigSchema\n\n override readonly queries: string[] = [ArchivistInsertQuerySchema, ...super.queries]\n\n override async head(): Promise<Payload | undefined> {\n const head = await (await this.payloads.find({})).sort({ _timestamp: -1 }).limit(1).toArray()\n return head[0] ? PayloadWrapper.wrap(head[0]).payload : undefined\n }\n\n protected override async getHandler(hashes: Hash[]): Promise<WithMeta<Payload>[]> {\n let remainingHashes = [...hashes]\n\n const dataPayloads = (await Promise.all(remainingHashes.map(_$hash => this.payloads.findOne({ _$hash })))).filter(exists)\n const dataPayloadsHashes = new Set(dataPayloads.map(payload => payload._$hash))\n remainingHashes = remainingHashes.filter(hash => !dataPayloadsHashes.has(hash))\n\n const dataBws = (await Promise.all(remainingHashes.map(_$hash => this.boundWitnesses.findOne({ _$hash })))).filter(exists)\n const dataBwsHashes = new Set(dataBws.map(payload => payload._$hash))\n remainingHashes = remainingHashes.filter(hash => !dataBwsHashes.has(hash))\n\n const payloads = (await Promise.all(remainingHashes.map(_hash => this.payloads.findOne({ _hash })))).filter(exists)\n const payloadsHashes = new Set(payloads.map(payload => payload._hash))\n remainingHashes = remainingHashes.filter(hash => !payloadsHashes.has(hash))\n\n const bws = (await Promise.all(remainingHashes.map(_hash => this.boundWitnesses.findOne({ _hash })))).filter(exists)\n const bwsHashes = new Set(bws.map(payload => payload._hash))\n remainingHashes = remainingHashes.filter(hash => !bwsHashes.has(hash))\n\n const foundPayloads = [...dataPayloads, ...dataBws, ...payloads, ...bws] as PayloadWithMongoMeta<Payload & { _$hash: Hash; _$meta?: unknown }>[]\n const result = await PayloadBuilder.build(foundPayloads.map(fromDbRepresentation))\n // console.log(`getHandler: ${JSON.stringify(hashes, null, 2)}:${JSON.stringify(result, null, 2)}`)\n return result\n }\n\n protected override async insertHandler(payloads: Payload[]): Promise<WithMeta<Payload>[]> {\n const [bw, p] = await validByType(payloads)\n const payloadsWithExternalMeta = await Promise.all(p.map((value, index) => toDbRepresentation(value, index)))\n if (payloadsWithExternalMeta.length > 0) {\n const payloadsResult = await this.payloads.insertMany(payloadsWithExternalMeta)\n if (!payloadsResult.acknowledged || payloadsResult.insertedCount !== payloadsWithExternalMeta.length)\n throw new Error('MongoDBDeterministicArchivist: Error inserting Payloads')\n }\n const boundWitnessesWithExternalMeta = await Promise.all(bw.map((value, index) => toDbRepresentation(value, index)))\n if (boundWitnessesWithExternalMeta.length > 0) {\n const boundWitnessesResult = await this.boundWitnesses.insertMany(boundWitnessesWithExternalMeta)\n if (!boundWitnessesResult.acknowledged || boundWitnessesResult.insertedCount !== boundWitnessesWithExternalMeta.length)\n throw new Error('MongoDBDeterministicArchivist: Error inserting BoundWitnesses')\n }\n\n return await PayloadBuilder.build([...boundWitnessesWithExternalMeta, ...payloadsWithExternalMeta])\n }\n\n protected override async startHandler() {\n await super.startHandler()\n await this.ensureIndexes()\n return true\n }\n}\n","import type { BoundWitness } from '@xyo-network/boundwitness-model'\nimport { isBoundWitness, isQueryBoundWitness } from '@xyo-network/boundwitness-model'\nimport { BoundWitnessWrapper, QueryBoundWitnessWrapper } from '@xyo-network/boundwitness-wrapper'\nimport type { Payload } from '@xyo-network/payload-model'\nimport { PayloadWrapper } from '@xyo-network/payload-wrapper'\n\nexport const validByType = async (payloads: Payload[] = []) => {\n const results: [BoundWitness[], Payload[]] = [[], []]\n await Promise.all(\n payloads.map(async (payload) => {\n if (isBoundWitness(payload)) {\n const wrapper = isQueryBoundWitness(payload) ? QueryBoundWitnessWrapper : BoundWitnessWrapper\n const bw = wrapper.parse(payload)\n if (await bw.getValid()) {\n results[0].push(bw.payload)\n } else {\n const errors = await bw.getErrors()\n console.log(`validByType.Error: ${JSON.stringify(errors, null, 2)}`)\n }\n } else {\n const payloadWrapper = PayloadWrapper.wrap(payload)\n if (await payloadWrapper.getValid()) {\n results[1].push(payloadWrapper.payload)\n }\n }\n return\n }),\n )\n return results\n}\n","export * from './Archivist.js'\nexport * from '@xyo-network/archivist-model-mongodb'\n"],"mappings":";;;;AAAA,SAASA,cAAc;AAEvB,SAASC,yBAAyB;AAClC,SAASC,kCAAkC;AAC3C,SAASC,oCAAoC;AAC7C,SAASC,0BAA0B;AACnC,SAASC,sBAAsB;AAK/B,SAASC,sBAAsBC,0BAA0B;AACzD,SAASC,kBAAAA,uBAAsB;;;ACX/B,SAASC,gBAAgBC,2BAA2B;AACpD,SAASC,qBAAqBC,gCAAgC;AAE9D,SAASC,sBAAsB;AAExB,IAAMC,cAAc,8BAAOC,WAAsB,CAAA,MAAE;AACxD,QAAMC,UAAuC;IAAC,CAAA;IAAI,CAAA;;AAClD,QAAMC,QAAQC,IACZH,SAASI,IAAI,OAAOC,YAAAA;AAClB,QAAIC,eAAeD,OAAAA,GAAU;AAC3B,YAAME,UAAUC,oBAAoBH,OAAAA,IAAWI,2BAA2BC;AAC1E,YAAMC,KAAKJ,QAAQK,MAAMP,OAAAA;AACzB,UAAI,MAAMM,GAAGE,SAAQ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AEvB,IAAI,CAAC0B,OAAOC,UAAUC,mBAAmBF,OAAOC,KAAAA,CAAAA,CAAAA;AACrG,QAAIF,yBAAyBI,SAAS,GAAG;AACvC,YAAMC,iBAAiB,MAAM,KAAK9C,SAAS+C,WAAWN,wBAAAA;AACtD,UAAI,CAACK,eAAeE,gBAAgBF,eAAeG,kBAAkBR,yBAAyBI,OAC5F,OAAM,IAAIK,MAAM,yDAAA;IACpB;AACA,UAAMC,iCAAiC,MAAMrC,QAAQC,IAAIuB,GAAGtB,IAAI,CAAC0B,OAAOC,UAAUC,mBAAmBF,OAAOC,KAAAA,CAAAA,CAAAA;AAC5G,QAAIQ,+BAA+BN,SAAS,GAAG;AAC7C,YAAMO,uBAAuB,MAAM,KAAK1B,eAAeqB,WAAWI,8BAAAA;AAClE,UAAI,CAACC,qBAAqBJ,gBAAgBI,qBAAqBH,kBAAkBE,+BAA+BN,OAC9G,OAAM,IAAIK,MAAM,+DAAA;IACpB;AAEA,WAAO,MAAMhB,eAAeC,MAAM;SAAIgB;SAAmCV;KAAyB;EACpG;EAEA,MAAyBY,eAAe;AACtC,UAAM,MAAMA,aAAAA;AACZ,UAAM,KAAKC,cAAa;AACxB,WAAO;EACT;AACF;;;AE5EA,cAAc;","names":["exists","AbstractArchivist","ArchivistInsertQuerySchema","MongoDBArchivistConfigSchema","MongoDBModuleMixin","PayloadBuilder","fromDbRepresentation","toDbRepresentation","PayloadWrapper","isBoundWitness","isQueryBoundWitness","BoundWitnessWrapper","QueryBoundWitnessWrapper","PayloadWrapper","validByType","payloads","results","Promise","all","map","payload","isBoundWitness","wrapper","isQueryBoundWitness","QueryBoundWitnessWrapper","BoundWitnessWrapper","bw","parse","getValid","push","errors","getErrors","console","log","JSON","stringify","payloadWrapper","PayloadWrapper","wrap","MongoDBArchivistBase","MongoDBModuleMixin","AbstractArchivist","MongoDBArchivist","configSchemas","MongoDBArchivistConfigSchema","defaultConfigSchema","queries","ArchivistInsertQuerySchema","head","payloads","find","sort","_timestamp","limit","toArray","PayloadWrapper","wrap","payload","undefined","getHandler","hashes","remainingHashes","dataPayloads","Promise","all","map","_$hash","findOne","filter","exists","dataPayloadsHashes","Set","hash","has","dataBws","boundWitnesses","dataBwsHashes","_hash","payloadsHashes","bws","bwsHashes","foundPayloads","result","PayloadBuilder","build","fromDbRepresentation","insertHandler","bw","p","validByType","payloadsWithExternalMeta","value","index","toDbRepresentation","length","payloadsResult","insertMany","acknowledged","insertedCount","Error","boundWitnessesWithExternalMeta","boundWitnessesResult","startHandler","ensureIndexes"]}
1
+ {"version":3,"sources":["../../src/Archivist.ts","../../src/lib/validByType.ts","../../src/index.ts"],"sourcesContent":["import { exists } from '@xylabs/exists'\nimport type { Hash } from '@xylabs/hex'\nimport { AbstractArchivist } from '@xyo-network/archivist-abstract'\nimport type { ArchivistNextOptions } from '@xyo-network/archivist-model'\nimport { ArchivistInsertQuerySchema, ArchivistNextQuerySchema } from '@xyo-network/archivist-model'\nimport { MongoDBArchivistConfigSchema } from '@xyo-network/archivist-model-mongodb'\nimport { MongoDBModuleMixin } from '@xyo-network/module-abstract-mongodb'\nimport { PayloadBuilder } from '@xyo-network/payload-builder'\nimport type {\n Payload, Schema, WithMeta,\n} from '@xyo-network/payload-model'\nimport type { PayloadWithMongoMeta } from '@xyo-network/payload-mongodb'\nimport { fromDbRepresentation, toDbRepresentation } from '@xyo-network/payload-mongodb'\nimport { PayloadWrapper } from '@xyo-network/payload-wrapper'\nimport { ObjectId } from 'mongodb'\n\nimport { validByType } from './lib/index.js'\n\nconst MongoDBArchivistBase = MongoDBModuleMixin(AbstractArchivist)\n\nexport class MongoDBArchivist extends MongoDBArchivistBase {\n static override readonly configSchemas: Schema[] = [...super.configSchemas, MongoDBArchivistConfigSchema]\n static override readonly defaultConfigSchema: Schema = MongoDBArchivistConfigSchema\n\n override readonly queries: string[] = [ArchivistInsertQuerySchema, ArchivistNextQuerySchema, ...super.queries]\n\n /**\n * The amount of time to allow the aggregate query to execute\n */\n protected readonly aggregateTimeoutMs = 10_000\n\n override async head(): Promise<Payload | undefined> {\n const head = await this.next({ limit: 1, order: 'desc' })\n return head[0] ? PayloadWrapper.wrap(head[0]).payload : undefined\n }\n\n protected async findOneByHash(hash: Hash) {\n const dataPayload = (await this.payloads.findOne({ _$hash: hash }))\n if (dataPayload) {\n return dataPayload\n } else {\n const dataBw = (await this.boundWitnesses.findOne({ _$hash: hash }))\n if (dataBw) {\n return dataBw\n } else {\n const payload = (await this.payloads.findOne({ _hash: hash }))\n if (payload) {\n return payload\n } else {\n const bw = (await this.boundWitnesses.findOne({ _hash: hash }))\n return bw ?? undefined\n }\n }\n }\n }\n\n protected override async getHandler(hashes: Hash[]): Promise<WithMeta<Payload>[]> {\n let remainingHashes = [...hashes]\n\n const dataPayloads = (await Promise.all(remainingHashes.map(_$hash => this.payloads.findOne({ _$hash })))).filter(exists)\n const dataPayloadsHashes = new Set(dataPayloads.map(payload => payload._$hash))\n remainingHashes = remainingHashes.filter(hash => !dataPayloadsHashes.has(hash))\n\n const dataBws = (await Promise.all(remainingHashes.map(_$hash => this.boundWitnesses.findOne({ _$hash })))).filter(exists)\n const dataBwsHashes = new Set(dataBws.map(payload => payload._$hash))\n remainingHashes = remainingHashes.filter(hash => !dataBwsHashes.has(hash))\n\n const payloads = (await Promise.all(remainingHashes.map(_hash => this.payloads.findOne({ _hash })))).filter(exists)\n const payloadsHashes = new Set(payloads.map(payload => payload._hash))\n remainingHashes = remainingHashes.filter(hash => !payloadsHashes.has(hash))\n\n const bws = (await Promise.all(remainingHashes.map(_hash => this.boundWitnesses.findOne({ _hash })))).filter(exists)\n const bwsHashes = new Set(bws.map(payload => payload._hash))\n remainingHashes = remainingHashes.filter(hash => !bwsHashes.has(hash))\n\n const foundPayloads = [...dataPayloads, ...dataBws, ...payloads, ...bws] as PayloadWithMongoMeta<Payload & { _$hash: Hash; _$meta?: unknown }>[]\n const result = await PayloadBuilder.build(foundPayloads.map(fromDbRepresentation))\n // console.log(`getHandler: ${JSON.stringify(hashes, null, 2)}:${JSON.stringify(result, null, 2)}`)\n return result\n }\n\n protected override async insertHandler(payloads: Payload[]): Promise<WithMeta<Payload>[]> {\n const [bw, p] = await validByType(payloads)\n const payloadsWithExternalMeta = await Promise.all(p.map((value, index) => toDbRepresentation(value, index)))\n if (payloadsWithExternalMeta.length > 0) {\n const payloadsResult = await this.payloads.insertMany(payloadsWithExternalMeta)\n if (!payloadsResult.acknowledged || payloadsResult.insertedCount !== payloadsWithExternalMeta.length)\n throw new Error('MongoDBDeterministicArchivist: Error inserting Payloads')\n }\n const boundWitnessesWithExternalMeta = await Promise.all(bw.map((value, index) => toDbRepresentation(value, index)))\n if (boundWitnessesWithExternalMeta.length > 0) {\n const boundWitnessesResult = await this.boundWitnesses.insertMany(boundWitnessesWithExternalMeta)\n if (!boundWitnessesResult.acknowledged || boundWitnessesResult.insertedCount !== boundWitnessesWithExternalMeta.length)\n throw new Error('MongoDBDeterministicArchivist: Error inserting BoundWitnesses')\n }\n\n return await PayloadBuilder.build([...boundWitnessesWithExternalMeta, ...payloadsWithExternalMeta].map(fromDbRepresentation))\n }\n\n protected override async nextHandler(options?: ArchivistNextOptions): Promise<WithMeta<Payload>[]> {\n // Sanitize inputs and set defaults\n let {\n limit, offset, order,\n } = options ?? { limit: 10, order: 'desc' }\n\n if (!limit) limit = 10\n if (limit > 100) limit = 100\n\n if (order != 'asc') order = 'desc'\n\n let id: ObjectId | undefined\n if (offset) {\n const payload = await this.findOneByHash(offset)\n // TODO: Should we throw an error if the requested payload is not found?\n if (payload) id = payload._id\n } else {\n id = order === 'asc'\n // If ascending, start from the beginning of time\n ? ObjectId.createFromTime(0)\n // If descending, start from now (plus a bit more in the future to ensure\n // them most recent ObjectIds are included)\n : ObjectId.createFromTime((Date.now() + 10_000) / 1000)\n }\n if (!id) return []\n\n // Create aggregate criteria\n const sort = order === 'asc' ? 1 : -1\n // TODO: How to handle random component of ID across multiple collections\n // to ensure we don't skip some payloads\n const match = order === 'asc' ? { _id: { $gt: id } } : { _id: { $lt: id } }\n\n // Run the aggregate query\n const foundPayloads = await this.payloads.useCollection((collection) => {\n return collection\n .aggregate<PayloadWithMongoMeta>([\n // Pre-filter payloads collection\n { $match: match },\n // Sort payloads by _id\n { $sort: { _id: sort } },\n // Limit payloads to the first N payloads\n { $limit: limit },\n // Combine with filtered boundWitnesses collection\n {\n $unionWith: {\n coll: this.boundWitnessSdkConfig.collection,\n pipeline: [\n { $match: match }, // Pre-filter boundWitnesses\n { $sort: { _id: sort } }, // Sort boundWitnesses by _id\n { $limit: limit }, // Limit boundWitnesses to the first N boundWitnesses\n ],\n },\n },\n // Sort the combined result by _id\n { $sort: { _id: sort } },\n // Limit the final result to N documents\n { $limit: limit },\n ])\n .maxTimeMS(this.aggregateTimeoutMs)\n .toArray()\n })\n\n // Convert from DB representation to Payloads\n return await PayloadBuilder.build(foundPayloads.map(fromDbRepresentation))\n }\n\n protected override async startHandler() {\n await super.startHandler()\n await this.ensureIndexes()\n return true\n }\n}\n","import type { BoundWitness } from '@xyo-network/boundwitness-model'\nimport { isBoundWitness, isQueryBoundWitness } from '@xyo-network/boundwitness-model'\nimport { BoundWitnessWrapper, QueryBoundWitnessWrapper } from '@xyo-network/boundwitness-wrapper'\nimport type { Payload } from '@xyo-network/payload-model'\nimport { PayloadWrapper } from '@xyo-network/payload-wrapper'\n\nexport const validByType = async (payloads: Payload[] = []) => {\n const results: [BoundWitness[], Payload[]] = [[], []]\n await Promise.all(\n payloads.map(async (payload) => {\n if (isBoundWitness(payload)) {\n const wrapper = isQueryBoundWitness(payload) ? QueryBoundWitnessWrapper : BoundWitnessWrapper\n const bw = wrapper.parse(payload)\n if (await bw.getValid()) {\n results[0].push(bw.payload)\n } else {\n const errors = await bw.getErrors()\n console.log(`validByType.Error: ${JSON.stringify(errors, null, 2)}`)\n }\n } else {\n const payloadWrapper = PayloadWrapper.wrap(payload)\n if (await payloadWrapper.getValid()) {\n results[1].push(payloadWrapper.payload)\n }\n }\n return\n }),\n )\n return results\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C,KAAK;QAAEQ,KAAKT;MAAG;IAAE;AAG1E,UAAMvB,gBAAgB,MAAM,KAAKxB,SAASyD,cAAc,CAACC,eAAAA;AACvD,aAAOA,WACJC,UAAgC;;QAE/B;UAAEC,QAAQN;QAAM;;QAEhB;UAAEO,OAAO;YAAEb,KAAKK;UAAK;QAAE;;QAEvB;UAAES,QAAQvE;QAAM;;QAEhB;UACEwE,YAAY;YACVC,MAAM,KAAKC,sBAAsBP;YACjCQ,UAAU;cACR;gBAAEN,QAAQN;cAAM;cAChB;gBAAEO,OAAO;kBAAEb,KAAKK;gBAAK;cAAE;cACvB;gBAAES,QAAQvE;cAAM;;UAEpB;QACF;;QAEA;UAAEsE,OAAO;YAAEb,KAAKK;UAAK;QAAE;;QAEvB;UAAES,QAAQvE;QAAM;OACjB,EACA4E,UAAU,KAAK/E,kBAAkB,EACjCgF,QAAO;IACZ,CAAA;AAGA,WAAO,MAAM1C,eAAeC,MAAMH,cAAcX,IAAIe,oBAAAA,CAAAA;EACtD;EAEA,MAAyByC,eAAe;AACtC,UAAM,MAAMA,aAAAA;AACZ,UAAM,KAAKC,cAAa;AACxB,WAAO;EACT;AACF;;;AEzKA,cAAc;","names":["exists","AbstractArchivist","ArchivistInsertQuerySchema","ArchivistNextQuerySchema","MongoDBArchivistConfigSchema","MongoDBModuleMixin","PayloadBuilder","fromDbRepresentation","toDbRepresentation","PayloadWrapper","ObjectId","isBoundWitness","isQueryBoundWitness","BoundWitnessWrapper","QueryBoundWitnessWrapper","PayloadWrapper","validByType","payloads","results","Promise","all","map","payload","isBoundWitness","wrapper","isQueryBoundWitness","QueryBoundWitnessWrapper","BoundWitnessWrapper","bw","parse","getValid","push","errors","getErrors","console","log","JSON","stringify","payloadWrapper","PayloadWrapper","wrap","MongoDBArchivistBase","MongoDBModuleMixin","AbstractArchivist","MongoDBArchivist","configSchemas","MongoDBArchivistConfigSchema","defaultConfigSchema","queries","ArchivistInsertQuerySchema","ArchivistNextQuerySchema","aggregateTimeoutMs","head","next","limit","order","PayloadWrapper","wrap","payload","undefined","findOneByHash","hash","dataPayload","payloads","findOne","_$hash","dataBw","boundWitnesses","_hash","bw","getHandler","hashes","remainingHashes","dataPayloads","Promise","all","map","filter","exists","dataPayloadsHashes","Set","has","dataBws","dataBwsHashes","payloadsHashes","bws","bwsHashes","foundPayloads","result","PayloadBuilder","build","fromDbRepresentation","insertHandler","p","validByType","payloadsWithExternalMeta","value","index","toDbRepresentation","length","payloadsResult","insertMany","acknowledged","insertedCount","Error","boundWitnessesWithExternalMeta","boundWitnessesResult","nextHandler","options","offset","id","_id","ObjectId","createFromTime","Date","now","sort","match","$gt","$lt","useCollection","collection","aggregate","$match","$sort","$limit","$unionWith","coll","boundWitnessSdkConfig","pipeline","maxTimeMS","toArray","startHandler","ensureIndexes"]}
@@ -1,5 +1,6 @@
1
1
  import type { Hash } from '@xylabs/hex';
2
2
  import { AbstractArchivist } from '@xyo-network/archivist-abstract';
3
+ import type { ArchivistNextOptions } from '@xyo-network/archivist-model';
3
4
  import type { Payload, Schema, WithMeta } from '@xyo-network/payload-model';
4
5
  import type { PayloadWithMongoMeta } from '@xyo-network/payload-mongodb';
5
6
  declare const MongoDBArchivistBase: (abstract new (...args: any[]) => {
@@ -67,9 +68,15 @@ export declare class MongoDBArchivist extends MongoDBArchivistBase {
67
68
  static readonly configSchemas: Schema[];
68
69
  static readonly defaultConfigSchema: Schema;
69
70
  readonly queries: string[];
71
+ /**
72
+ * The amount of time to allow the aggregate query to execute
73
+ */
74
+ protected readonly aggregateTimeoutMs = 10000;
70
75
  head(): Promise<Payload | undefined>;
76
+ protected findOneByHash(hash: Hash): Promise<import("mongodb").WithId<PayloadWithMongoMeta> | undefined>;
71
77
  protected getHandler(hashes: Hash[]): Promise<WithMeta<Payload>[]>;
72
78
  protected insertHandler(payloads: Payload[]): Promise<WithMeta<Payload>[]>;
79
+ protected nextHandler(options?: ArchivistNextOptions): Promise<WithMeta<Payload>[]>;
73
80
  protected startHandler(): Promise<boolean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IAEnF,SAAkB,OAAO,EAAE,MAAM,EAAE,CAA2E;IAE9G;;OAEG;IACH,SAAS,CAAC,QAAQ,CAAC,kBAAkB,SAAS;IAE/B,IAAI,IAAI,OAAO,CAAC,OAAO,GAAG,SAAS,CAAC;cAKnC,aAAa,CAAC,IAAI,EAAE,IAAI;cAoBf,UAAU,CAAC,MAAM,EAAE,IAAI,EAAE,GAAG,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE,CAAC;cAyBxD,aAAa,CAAC,QAAQ,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE,CAAC;cAkBhE,WAAW,CAAC,OAAO,CAAC,EAAE,oBAAoB,GAAG,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE,CAAC;cAkEzE,YAAY;CAKtC"}
@@ -4,12 +4,13 @@ var __name = (target, value) => __defProp(target, "name", { value, configurable:
4
4
  // src/Archivist.ts
5
5
  import { exists } from "@xylabs/exists";
6
6
  import { AbstractArchivist } from "@xyo-network/archivist-abstract";
7
- import { ArchivistInsertQuerySchema } from "@xyo-network/archivist-model";
7
+ import { ArchivistInsertQuerySchema, ArchivistNextQuerySchema } from "@xyo-network/archivist-model";
8
8
  import { MongoDBArchivistConfigSchema } from "@xyo-network/archivist-model-mongodb";
9
9
  import { MongoDBModuleMixin } from "@xyo-network/module-abstract-mongodb";
10
10
  import { PayloadBuilder } from "@xyo-network/payload-builder";
11
11
  import { fromDbRepresentation, toDbRepresentation } from "@xyo-network/payload-mongodb";
12
12
  import { PayloadWrapper as PayloadWrapper2 } from "@xyo-network/payload-wrapper";
13
+ import { ObjectId } from "mongodb";
13
14
 
14
15
  // src/lib/validByType.ts
15
16
  import { isBoundWitness, isQueryBoundWitness } from "@xyo-network/boundwitness-model";
@@ -54,14 +55,47 @@ var MongoDBArchivist = class extends MongoDBArchivistBase {
54
55
  static defaultConfigSchema = MongoDBArchivistConfigSchema;
55
56
  queries = [
56
57
  ArchivistInsertQuerySchema,
58
+ ArchivistNextQuerySchema,
57
59
  ...super.queries
58
60
  ];
61
+ /**
62
+ * The amount of time to allow the aggregate query to execute
63
+ */
64
+ aggregateTimeoutMs = 1e4;
59
65
  async head() {
60
- const head = await (await this.payloads.find({})).sort({
61
- _timestamp: -1
62
- }).limit(1).toArray();
66
+ const head = await this.next({
67
+ limit: 1,
68
+ order: "desc"
69
+ });
63
70
  return head[0] ? PayloadWrapper2.wrap(head[0]).payload : void 0;
64
71
  }
72
+ async findOneByHash(hash) {
73
+ const dataPayload = await this.payloads.findOne({
74
+ _$hash: hash
75
+ });
76
+ if (dataPayload) {
77
+ return dataPayload;
78
+ } else {
79
+ const dataBw = await this.boundWitnesses.findOne({
80
+ _$hash: hash
81
+ });
82
+ if (dataBw) {
83
+ return dataBw;
84
+ } else {
85
+ const payload = await this.payloads.findOne({
86
+ _hash: hash
87
+ });
88
+ if (payload) {
89
+ return payload;
90
+ } else {
91
+ const bw = await this.boundWitnesses.findOne({
92
+ _hash: hash
93
+ });
94
+ return bw ?? void 0;
95
+ }
96
+ }
97
+ }
98
+ }
65
99
  async getHandler(hashes) {
66
100
  let remainingHashes = [
67
101
  ...hashes
@@ -110,7 +144,82 @@ var MongoDBArchivist = class extends MongoDBArchivistBase {
110
144
  return await PayloadBuilder.build([
111
145
  ...boundWitnessesWithExternalMeta,
112
146
  ...payloadsWithExternalMeta
113
- ]);
147
+ ].map(fromDbRepresentation));
148
+ }
149
+ async nextHandler(options) {
150
+ let { limit, offset, order } = options ?? {
151
+ limit: 10,
152
+ order: "desc"
153
+ };
154
+ if (!limit) limit = 10;
155
+ if (limit > 100) limit = 100;
156
+ if (order != "asc") order = "desc";
157
+ let id;
158
+ if (offset) {
159
+ const payload = await this.findOneByHash(offset);
160
+ if (payload) id = payload._id;
161
+ } else {
162
+ id = order === "asc" ? ObjectId.createFromTime(0) : ObjectId.createFromTime((Date.now() + 1e4) / 1e3);
163
+ }
164
+ if (!id) return [];
165
+ const sort = order === "asc" ? 1 : -1;
166
+ const match = order === "asc" ? {
167
+ _id: {
168
+ $gt: id
169
+ }
170
+ } : {
171
+ _id: {
172
+ $lt: id
173
+ }
174
+ };
175
+ const foundPayloads = await this.payloads.useCollection((collection) => {
176
+ return collection.aggregate([
177
+ // Pre-filter payloads collection
178
+ {
179
+ $match: match
180
+ },
181
+ // Sort payloads by _id
182
+ {
183
+ $sort: {
184
+ _id: sort
185
+ }
186
+ },
187
+ // Limit payloads to the first N payloads
188
+ {
189
+ $limit: limit
190
+ },
191
+ // Combine with filtered boundWitnesses collection
192
+ {
193
+ $unionWith: {
194
+ coll: this.boundWitnessSdkConfig.collection,
195
+ pipeline: [
196
+ {
197
+ $match: match
198
+ },
199
+ {
200
+ $sort: {
201
+ _id: sort
202
+ }
203
+ },
204
+ {
205
+ $limit: limit
206
+ }
207
+ ]
208
+ }
209
+ },
210
+ // Sort the combined result by _id
211
+ {
212
+ $sort: {
213
+ _id: sort
214
+ }
215
+ },
216
+ // Limit the final result to N documents
217
+ {
218
+ $limit: limit
219
+ }
220
+ ]).maxTimeMS(this.aggregateTimeoutMs).toArray();
221
+ });
222
+ return await PayloadBuilder.build(foundPayloads.map(fromDbRepresentation));
114
223
  }
115
224
  async startHandler() {
116
225
  await super.startHandler();
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/Archivist.ts","../../src/lib/validByType.ts","../../src/index.ts"],"sourcesContent":["import { exists } from '@xylabs/exists'\nimport type { Hash } from '@xylabs/hex'\nimport { AbstractArchivist } from '@xyo-network/archivist-abstract'\nimport { ArchivistInsertQuerySchema } from '@xyo-network/archivist-model'\nimport { MongoDBArchivistConfigSchema } from '@xyo-network/archivist-model-mongodb'\nimport { MongoDBModuleMixin } from '@xyo-network/module-abstract-mongodb'\nimport { PayloadBuilder } from '@xyo-network/payload-builder'\nimport type {\n Payload, Schema, WithMeta,\n} from '@xyo-network/payload-model'\nimport type { PayloadWithMongoMeta } from '@xyo-network/payload-mongodb'\nimport { fromDbRepresentation, toDbRepresentation } from '@xyo-network/payload-mongodb'\nimport { PayloadWrapper } from '@xyo-network/payload-wrapper'\n\nimport { validByType } from './lib/index.js'\n\nconst MongoDBArchivistBase = MongoDBModuleMixin(AbstractArchivist)\n\nexport class MongoDBArchivist extends MongoDBArchivistBase {\n static override readonly configSchemas: Schema[] = [...super.configSchemas, MongoDBArchivistConfigSchema]\n static override readonly defaultConfigSchema: Schema = MongoDBArchivistConfigSchema\n\n override readonly queries: string[] = [ArchivistInsertQuerySchema, ...super.queries]\n\n override async head(): Promise<Payload | undefined> {\n const head = await (await this.payloads.find({})).sort({ _timestamp: -1 }).limit(1).toArray()\n return head[0] ? PayloadWrapper.wrap(head[0]).payload : undefined\n }\n\n protected override async getHandler(hashes: Hash[]): Promise<WithMeta<Payload>[]> {\n let remainingHashes = [...hashes]\n\n const dataPayloads = (await Promise.all(remainingHashes.map(_$hash => this.payloads.findOne({ _$hash })))).filter(exists)\n const dataPayloadsHashes = new Set(dataPayloads.map(payload => payload._$hash))\n remainingHashes = remainingHashes.filter(hash => !dataPayloadsHashes.has(hash))\n\n const dataBws = (await Promise.all(remainingHashes.map(_$hash => this.boundWitnesses.findOne({ _$hash })))).filter(exists)\n const dataBwsHashes = new Set(dataBws.map(payload => payload._$hash))\n remainingHashes = remainingHashes.filter(hash => !dataBwsHashes.has(hash))\n\n const payloads = (await Promise.all(remainingHashes.map(_hash => this.payloads.findOne({ _hash })))).filter(exists)\n const payloadsHashes = new Set(payloads.map(payload => payload._hash))\n remainingHashes = remainingHashes.filter(hash => !payloadsHashes.has(hash))\n\n const bws = (await Promise.all(remainingHashes.map(_hash => this.boundWitnesses.findOne({ _hash })))).filter(exists)\n const bwsHashes = new Set(bws.map(payload => payload._hash))\n remainingHashes = remainingHashes.filter(hash => !bwsHashes.has(hash))\n\n const foundPayloads = [...dataPayloads, ...dataBws, ...payloads, ...bws] as PayloadWithMongoMeta<Payload & { _$hash: Hash; _$meta?: unknown }>[]\n const result = await PayloadBuilder.build(foundPayloads.map(fromDbRepresentation))\n // console.log(`getHandler: ${JSON.stringify(hashes, null, 2)}:${JSON.stringify(result, null, 2)}`)\n return result\n }\n\n protected override async insertHandler(payloads: Payload[]): Promise<WithMeta<Payload>[]> {\n const [bw, p] = await validByType(payloads)\n const payloadsWithExternalMeta = await Promise.all(p.map((value, index) => toDbRepresentation(value, index)))\n if (payloadsWithExternalMeta.length > 0) {\n const payloadsResult = await this.payloads.insertMany(payloadsWithExternalMeta)\n if (!payloadsResult.acknowledged || payloadsResult.insertedCount !== payloadsWithExternalMeta.length)\n throw new Error('MongoDBDeterministicArchivist: Error inserting Payloads')\n }\n const boundWitnessesWithExternalMeta = await Promise.all(bw.map((value, index) => toDbRepresentation(value, index)))\n if (boundWitnessesWithExternalMeta.length > 0) {\n const boundWitnessesResult = await this.boundWitnesses.insertMany(boundWitnessesWithExternalMeta)\n if (!boundWitnessesResult.acknowledged || boundWitnessesResult.insertedCount !== boundWitnessesWithExternalMeta.length)\n throw new Error('MongoDBDeterministicArchivist: Error inserting BoundWitnesses')\n }\n\n return await PayloadBuilder.build([...boundWitnessesWithExternalMeta, ...payloadsWithExternalMeta])\n }\n\n protected override async startHandler() {\n await super.startHandler()\n await this.ensureIndexes()\n return true\n }\n}\n","import type { BoundWitness } from '@xyo-network/boundwitness-model'\nimport { isBoundWitness, isQueryBoundWitness } from '@xyo-network/boundwitness-model'\nimport { BoundWitnessWrapper, QueryBoundWitnessWrapper } from '@xyo-network/boundwitness-wrapper'\nimport type { Payload } from '@xyo-network/payload-model'\nimport { PayloadWrapper } from '@xyo-network/payload-wrapper'\n\nexport const validByType = async (payloads: Payload[] = []) => {\n const results: [BoundWitness[], Payload[]] = [[], []]\n await Promise.all(\n payloads.map(async (payload) => {\n if (isBoundWitness(payload)) {\n const wrapper = isQueryBoundWitness(payload) ? QueryBoundWitnessWrapper : BoundWitnessWrapper\n const bw = wrapper.parse(payload)\n if (await bw.getValid()) {\n results[0].push(bw.payload)\n } else {\n const errors = await bw.getErrors()\n console.log(`validByType.Error: ${JSON.stringify(errors, null, 2)}`)\n }\n } else {\n const payloadWrapper = PayloadWrapper.wrap(payload)\n if (await payloadWrapper.getValid()) {\n results[1].push(payloadWrapper.payload)\n }\n }\n return\n }),\n )\n return results\n}\n","export * from './Archivist.js'\nexport * from '@xyo-network/archivist-model-mongodb'\n"],"mappings":";;;;AAAA,SAASA,cAAc;AAEvB,SAASC,yBAAyB;AAClC,SAASC,kCAAkC;AAC3C,SAASC,oCAAoC;AAC7C,SAASC,0BAA0B;AACnC,SAASC,sBAAsB;AAK/B,SAASC,sBAAsBC,0BAA0B;AACzD,SAASC,kBAAAA,uBAAsB;;;ACX/B,SAASC,gBAAgBC,2BAA2B;AACpD,SAASC,qBAAqBC,gCAAgC;AAE9D,SAASC,sBAAsB;AAExB,IAAMC,cAAc,8BAAOC,WAAsB,CAAA,MAAE;AACxD,QAAMC,UAAuC;IAAC,CAAA;IAAI,CAAA;;AAClD,QAAMC,QAAQC,IACZH,SAASI,IAAI,OAAOC,YAAAA;AAClB,QAAIC,eAAeD,OAAAA,GAAU;AAC3B,YAAME,UAAUC,oBAAoBH,OAAAA,IAAWI,2BAA2BC;AAC1E,YAAMC,KAAKJ,QAAQK,MAAMP,OAAAA;AACzB,UAAI,MAAMM,GAAGE,SAAQ,GAAI;AACvBZ,gBAAQ,CAAA,EAAGa,KAAKH,GAAGN,OAAO;MAC5B,OAAO;AACL,cAAMU,SAAS,MAAMJ,GAAGK,UAAS;AACjCC,gBAAQC,IAAI,sBAAsBC,KAAKC,UAAUL,QAAQ,MAAM,CAAA,CAAA,EAAI;MACrE;IACF,OAAO;AACL,YAAMM,iBAAiBC,eAAeC,KAAKlB,OAAAA;AAC3C,UAAI,MAAMgB,eAAeR,SAAQ,GAAI;AACnCZ,gBAAQ,CAAA,EAAGa,KAAKO,eAAehB,OAAO;MACxC;IACF;AACA;EACF,CAAA,CAAA;AAEF,SAAOJ;AACT,GAvB2B;;;ADU3B,IAAMuB,uBAAu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AAC7C,YAAMO,uBAAuB,MAAM,KAAK1B,eAAeqB,WAAWI,8BAAAA;AAClE,UAAI,CAACC,qBAAqBJ,gBAAgBI,qBAAqBH,kBAAkBE,+BAA+BN,OAC9G,OAAM,IAAIK,MAAM,+DAAA;IACpB;AAEA,WAAO,MAAMhB,eAAeC,MAAM;SAAIgB;SAAmCV;KAAyB;EACpG;EAEA,MAAyBY,eAAe;AACtC,UAAM,MAAMA,aAAAA;AACZ,UAAM,KAAKC,cAAa;AACxB,WAAO;EACT;AACF;;;AE5EA,cAAc;","names":["exists","AbstractArchivist","ArchivistInsertQuerySchema","MongoDBArchivistConfigSchema","MongoDBModuleMixin","PayloadBuilder","fromDbRepresentation","toDbRepresentation","PayloadWrapper","isBoundWitness","isQueryBoundWitness","BoundWitnessWrapper","QueryBoundWitnessWrapper","PayloadWrapper","validByType","payloads","results","Promise","all","map","payload","isBoundWitness","wrapper","isQueryBoundWitness","QueryBoundWitnessWrapper","BoundWitnessWrapper","bw","parse","getValid","push","errors","getErrors","console","log","JSON","stringify","payloadWrapper","PayloadWrapper","wrap","MongoDBArchivistBase","MongoDBModuleMixin","AbstractArchivist","MongoDBArchivist","configSchemas","MongoDBArchivistConfigSchema","defaultConfigSchema","queries","ArchivistInsertQuerySchema","head","payloads","find","sort","_timestamp","limit","toArray","PayloadWrapper","wrap","payload","undefined","getHandler","hashes","remainingHashes","dataPayloads","Promise","all","map","_$hash","findOne","filter","exists","dataPayloadsHashes","Set","hash","has","dataBws","boundWitnesses","dataBwsHashes","_hash","payloadsHashes","bws","bwsHashes","foundPayloads","result","PayloadBuilder","build","fromDbRepresentation","insertHandler","bw","p","validByType","payloadsWithExternalMeta","value","index","toDbRepresentation","length","payloadsResult","insertMany","acknowledged","insertedCount","Error","boundWitnessesWithExternalMeta","boundWitnessesResult","startHandler","ensureIndexes"]}
1
+ {"version":3,"sources":["../../src/Archivist.ts","../../src/lib/validByType.ts","../../src/index.ts"],"sourcesContent":["import { exists } from '@xylabs/exists'\nimport type { Hash } from '@xylabs/hex'\nimport { AbstractArchivist } from '@xyo-network/archivist-abstract'\nimport type { ArchivistNextOptions } from '@xyo-network/archivist-model'\nimport { ArchivistInsertQuerySchema, ArchivistNextQuerySchema } from '@xyo-network/archivist-model'\nimport { MongoDBArchivistConfigSchema } from '@xyo-network/archivist-model-mongodb'\nimport { MongoDBModuleMixin } from '@xyo-network/module-abstract-mongodb'\nimport { PayloadBuilder } from '@xyo-network/payload-builder'\nimport type {\n Payload, Schema, WithMeta,\n} from '@xyo-network/payload-model'\nimport type { PayloadWithMongoMeta } from '@xyo-network/payload-mongodb'\nimport { fromDbRepresentation, toDbRepresentation } from '@xyo-network/payload-mongodb'\nimport { PayloadWrapper } from '@xyo-network/payload-wrapper'\nimport { ObjectId } from 'mongodb'\n\nimport { validByType } from './lib/index.js'\n\nconst MongoDBArchivistBase = MongoDBModuleMixin(AbstractArchivist)\n\nexport class MongoDBArchivist extends MongoDBArchivistBase {\n static override readonly configSchemas: Schema[] = [...super.configSchemas, MongoDBArchivistConfigSchema]\n static override readonly defaultConfigSchema: Schema = MongoDBArchivistConfigSchema\n\n override readonly queries: string[] = [ArchivistInsertQuerySchema, ArchivistNextQuerySchema, ...super.queries]\n\n /**\n * The amount of time to allow the aggregate query to execute\n */\n protected readonly aggregateTimeoutMs = 10_000\n\n override async head(): Promise<Payload | undefined> {\n const head = await this.next({ limit: 1, order: 'desc' })\n return head[0] ? PayloadWrapper.wrap(head[0]).payload : undefined\n }\n\n protected async findOneByHash(hash: Hash) {\n const dataPayload = (await this.payloads.findOne({ _$hash: hash }))\n if (dataPayload) {\n return dataPayload\n } else {\n const dataBw = (await this.boundWitnesses.findOne({ _$hash: hash }))\n if (dataBw) {\n return dataBw\n } else {\n const payload = (await this.payloads.findOne({ _hash: hash }))\n if (payload) {\n return payload\n } else {\n const bw = (await this.boundWitnesses.findOne({ _hash: hash }))\n return bw ?? undefined\n }\n }\n }\n }\n\n protected override async getHandler(hashes: Hash[]): Promise<WithMeta<Payload>[]> {\n let remainingHashes = [...hashes]\n\n const dataPayloads = (await Promise.all(remainingHashes.map(_$hash => this.payloads.findOne({ _$hash })))).filter(exists)\n const dataPayloadsHashes = new Set(dataPayloads.map(payload => payload._$hash))\n remainingHashes = remainingHashes.filter(hash => !dataPayloadsHashes.has(hash))\n\n const dataBws = (await Promise.all(remainingHashes.map(_$hash => this.boundWitnesses.findOne({ _$hash })))).filter(exists)\n const dataBwsHashes = new Set(dataBws.map(payload => payload._$hash))\n remainingHashes = remainingHashes.filter(hash => !dataBwsHashes.has(hash))\n\n const payloads = (await Promise.all(remainingHashes.map(_hash => this.payloads.findOne({ _hash })))).filter(exists)\n const payloadsHashes = new Set(payloads.map(payload => payload._hash))\n remainingHashes = remainingHashes.filter(hash => !payloadsHashes.has(hash))\n\n const bws = (await Promise.all(remainingHashes.map(_hash => this.boundWitnesses.findOne({ _hash })))).filter(exists)\n const bwsHashes = new Set(bws.map(payload => payload._hash))\n remainingHashes = remainingHashes.filter(hash => !bwsHashes.has(hash))\n\n const foundPayloads = [...dataPayloads, ...dataBws, ...payloads, ...bws] as PayloadWithMongoMeta<Payload & { _$hash: Hash; _$meta?: unknown }>[]\n const result = await PayloadBuilder.build(foundPayloads.map(fromDbRepresentation))\n // console.log(`getHandler: ${JSON.stringify(hashes, null, 2)}:${JSON.stringify(result, null, 2)}`)\n return result\n }\n\n protected override async insertHandler(payloads: Payload[]): Promise<WithMeta<Payload>[]> {\n const [bw, p] = await validByType(payloads)\n const payloadsWithExternalMeta = await Promise.all(p.map((value, index) => toDbRepresentation(value, index)))\n if (payloadsWithExternalMeta.length > 0) {\n const payloadsResult = await this.payloads.insertMany(payloadsWithExternalMeta)\n if (!payloadsResult.acknowledged || payloadsResult.insertedCount !== payloadsWithExternalMeta.length)\n throw new Error('MongoDBDeterministicArchivist: Error inserting Payloads')\n }\n const boundWitnessesWithExternalMeta = await Promise.all(bw.map((value, index) => toDbRepresentation(value, index)))\n if (boundWitnessesWithExternalMeta.length > 0) {\n const boundWitnessesResult = await this.boundWitnesses.insertMany(boundWitnessesWithExternalMeta)\n if (!boundWitnessesResult.acknowledged || boundWitnessesResult.insertedCount !== boundWitnessesWithExternalMeta.length)\n throw new Error('MongoDBDeterministicArchivist: Error inserting BoundWitnesses')\n }\n\n return await PayloadBuilder.build([...boundWitnessesWithExternalMeta, ...payloadsWithExternalMeta].map(fromDbRepresentation))\n }\n\n protected override async nextHandler(options?: ArchivistNextOptions): Promise<WithMeta<Payload>[]> {\n // Sanitize inputs and set defaults\n let {\n limit, offset, order,\n } = options ?? { limit: 10, order: 'desc' }\n\n if (!limit) limit = 10\n if (limit > 100) limit = 100\n\n if (order != 'asc') order = 'desc'\n\n let id: ObjectId | undefined\n if (offset) {\n const payload = await this.findOneByHash(offset)\n // TODO: Should we throw an error if the requested payload is not found?\n if (payload) id = payload._id\n } else {\n id = order === 'asc'\n // If ascending, start from the beginning of time\n ? ObjectId.createFromTime(0)\n // If descending, start from now (plus a bit more in the future to ensure\n // them most recent ObjectIds are included)\n : ObjectId.createFromTime((Date.now() + 10_000) / 1000)\n }\n if (!id) return []\n\n // Create aggregate criteria\n const sort = order === 'asc' ? 1 : -1\n // TODO: How to handle random component of ID across multiple collections\n // to ensure we don't skip some payloads\n const match = order === 'asc' ? { _id: { $gt: id } } : { _id: { $lt: id } }\n\n // Run the aggregate query\n const foundPayloads = await this.payloads.useCollection((collection) => {\n return collection\n .aggregate<PayloadWithMongoMeta>([\n // Pre-filter payloads collection\n { $match: match },\n // Sort payloads by _id\n { $sort: { _id: sort } },\n // Limit payloads to the first N payloads\n { $limit: limit },\n // Combine with filtered boundWitnesses collection\n {\n $unionWith: {\n coll: this.boundWitnessSdkConfig.collection,\n pipeline: [\n { $match: match }, // Pre-filter boundWitnesses\n { $sort: { _id: sort } }, // Sort boundWitnesses by _id\n { $limit: limit }, // Limit boundWitnesses to the first N boundWitnesses\n ],\n },\n },\n // Sort the combined result by _id\n { $sort: { _id: sort } },\n // Limit the final result to N documents\n { $limit: limit },\n ])\n .maxTimeMS(this.aggregateTimeoutMs)\n .toArray()\n })\n\n // Convert from DB representation to Payloads\n return await PayloadBuilder.build(foundPayloads.map(fromDbRepresentation))\n }\n\n protected override async startHandler() {\n await super.startHandler()\n await this.ensureIndexes()\n return true\n }\n}\n","import type { BoundWitness } from '@xyo-network/boundwitness-model'\nimport { isBoundWitness, isQueryBoundWitness } from '@xyo-network/boundwitness-model'\nimport { BoundWitnessWrapper, QueryBoundWitnessWrapper } from '@xyo-network/boundwitness-wrapper'\nimport type { Payload } from '@xyo-network/payload-model'\nimport { PayloadWrapper } from '@xyo-network/payload-wrapper'\n\nexport const validByType = async (payloads: Payload[] = []) => {\n const results: [BoundWitness[], Payload[]] = [[], []]\n await Promise.all(\n payloads.map(async (payload) => {\n if (isBoundWitness(payload)) {\n const wrapper = isQueryBoundWitness(payload) ? QueryBoundWitnessWrapper : BoundWitnessWrapper\n const bw = wrapper.parse(payload)\n if (await bw.getValid()) {\n results[0].push(bw.payload)\n } else {\n const errors = await bw.getErrors()\n console.log(`validByType.Error: ${JSON.stringify(errors, null, 2)}`)\n }\n } else {\n const payloadWrapper = PayloadWrapper.wrap(payload)\n if (await payloadWrapper.getValid()) {\n results[1].push(payloadWrapper.payload)\n }\n }\n return\n }),\n )\n return results\n}\n","export * from './Archivist.js'\nexport * from '@xyo-network/archivist-model-mongodb'\n"],"mappings":";;;;AAAA,SAASA,cAAc;AAEvB,SAASC,yBAAyB;AAElC,SAASC,4BAA4BC,gCAAgC;AACrE,SAASC,oCAAoC;AAC7C,SAASC,0BAA0B;AACnC,SAASC,sBAAsB;AAK/B,SAASC,sBAAsBC,0BAA0B;AACzD,SAASC,kBAAAA,uBAAsB;AAC/B,SAASC,gBAAgB;;;ACbzB,SAASC,gBAAgBC,2BAA2B;AACpD,SAASC,qBAAqBC,gCAAgC;AAE9D,SAASC,sBAAsB;AAExB,IAAMC,cAAc,8BAAOC,WAAsB,CAAA,MAAE;AACxD,QAAMC,UA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sE,OAAO;YAAEb,KAAKK;UAAK;QAAE;;QAEvB;UAAES,QAAQvE;QAAM;OACjB,EACA4E,UAAU,KAAK/E,kBAAkB,EACjCgF,QAAO;IACZ,CAAA;AAGA,WAAO,MAAM1C,eAAeC,MAAMH,cAAcX,IAAIe,oBAAAA,CAAAA;EACtD;EAEA,MAAyByC,eAAe;AACtC,UAAM,MAAMA,aAAAA;AACZ,UAAM,KAAKC,cAAa;AACxB,WAAO;EACT;AACF;;;AEzKA,cAAc;","names":["exists","AbstractArchivist","ArchivistInsertQuerySchema","ArchivistNextQuerySchema","MongoDBArchivistConfigSchema","MongoDBModuleMixin","PayloadBuilder","fromDbRepresentation","toDbRepresentation","PayloadWrapper","ObjectId","isBoundWitness","isQueryBoundWitness","BoundWitnessWrapper","QueryBoundWitnessWrapper","PayloadWrapper","validByType","payloads","results","Promise","all","map","payload","isBoundWitness","wrapper","isQueryBoundWitness","QueryBoundWitnessWrapper","BoundWitnessWrapper","bw","parse","getValid","push","errors","getErrors","console","log","JSON","stringify","payloadWrapper","PayloadWrapper","wrap","MongoDBArchivistBase","MongoDBModuleMixin","AbstractArchivist","MongoDBArchivist","configSchemas","MongoDBArchivistConfigSchema","defaultConfigSchema","queries","ArchivistInsertQuerySchema","ArchivistNextQuerySchema","aggregateTimeoutMs","head","next","limit","order","PayloadWrapper","wrap","payload","undefined","findOneByHash","hash","dataPayload","payloads","findOne","_$hash","dataBw","boundWitnesses","_hash","bw","getHandler","hashes","remainingHashes","dataPayloads","Promise","all","map","filter","exists","dataPayloadsHashes","Set","has","dataBws","dataBwsHashes","payloadsHashes","bws","bwsHashes","foundPayloads","result","PayloadBuilder","build","fromDbRepresentation","insertHandler","p","validByType","payloadsWithExternalMeta","value","index","toDbRepresentation","length","payloadsResult","insertMany","acknowledged","insertedCount","Error","boundWitnessesWithExternalMeta","boundWitnessesResult","nextHandler","options","offset","id","_id","ObjectId","createFromTime","Date","now","sort","match","$gt","$lt","useCollection","collection","aggregate","$match","$sort","$limit","$unionWith","coll","boundWitnessSdkConfig","pipeline","maxTimeMS","toArray","startHandler","ensureIndexes"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xyo-network/archivist-mongodb",
3
- "version": "3.1.4",
3
+ "version": "3.1.6",
4
4
  "description": "Primary SDK for using XYO Protocol 2.0",
5
5
  "homepage": "https://xyo.network",
6
6
  "bugs": {
@@ -37,32 +37,35 @@
37
37
  "module": "dist/node/index.mjs",
38
38
  "types": "dist/node/index.d.ts",
39
39
  "dependencies": {
40
- "@xylabs/exists": "^4.3.14",
41
- "@xylabs/hex": "^4.3.14",
42
- "@xyo-network/archivist-abstract": "^3.4.2",
43
- "@xyo-network/archivist-model": "^3.4.2",
44
- "@xyo-network/archivist-model-mongodb": "^3.1.4",
45
- "@xyo-network/boundwitness-model": "^3.4.2",
46
- "@xyo-network/boundwitness-wrapper": "^3.4.2",
47
- "@xyo-network/module-abstract-mongodb": "^3.1.4",
48
- "@xyo-network/payload-builder": "^3.4.2",
49
- "@xyo-network/payload-model": "^3.4.2",
50
- "@xyo-network/payload-mongodb": "^3.1.4",
51
- "@xyo-network/payload-wrapper": "^3.4.2"
40
+ "@xylabs/exists": "^4.4.10",
41
+ "@xylabs/hex": "^4.4.10",
42
+ "@xyo-network/archivist-abstract": "^3.5.2",
43
+ "@xyo-network/archivist-model": "^3.5.2",
44
+ "@xyo-network/archivist-model-mongodb": "^3.1.6",
45
+ "@xyo-network/boundwitness-model": "^3.5.2",
46
+ "@xyo-network/boundwitness-wrapper": "^3.5.2",
47
+ "@xyo-network/module-abstract-mongodb": "^3.1.6",
48
+ "@xyo-network/payload-builder": "^3.5.2",
49
+ "@xyo-network/payload-model": "^3.5.2",
50
+ "@xyo-network/payload-mongodb": "^3.1.6",
51
+ "@xyo-network/payload-wrapper": "^3.5.2",
52
+ "mongodb": "~6.11.0"
52
53
  },
53
54
  "devDependencies": {
54
- "@xylabs/arraybuffer": "^4.3.14",
55
- "@xylabs/jest-helpers": "^4.3.14",
55
+ "@xylabs/assert": "^4.4.10",
56
+ "@xylabs/delay": "^4.4.10",
57
+ "@xylabs/jest-helpers": "^4.4.10",
56
58
  "@xylabs/ts-scripts-yarn3": "^4.2.4",
57
59
  "@xylabs/tsconfig": "^4.2.4",
58
- "@xyo-network/account": "^3.4.2",
59
- "@xyo-network/account-model": "^3.4.2",
60
- "@xyo-network/archivist-wrapper": "^3.4.2",
61
- "@xyo-network/boundwitness-builder": "^3.4.2",
62
- "@xyo-network/sdk-xyo-mongo-js": "^3.1.4",
60
+ "@xylabs/vitest-extended": "^4.4.10",
61
+ "@xyo-network/account": "^3.5.2",
62
+ "@xyo-network/archivist-wrapper": "^3.5.2",
63
+ "@xyo-network/boundwitness-builder": "^3.5.2",
64
+ "@xyo-network/sdk-xyo-mongo-js": "^3.1.6",
63
65
  "fake-indexeddb": "^6.0.0",
64
66
  "jest": "^29.7.0",
65
- "typescript": "^5.6.3"
67
+ "typescript": "^5.7.2",
68
+ "vitest": "^2.1.8"
66
69
  },
67
70
  "publishConfig": {
68
71
  "access": "public"
package/src/Archivist.ts CHANGED
@@ -1,7 +1,8 @@
1
1
  import { exists } from '@xylabs/exists'
2
2
  import type { Hash } from '@xylabs/hex'
3
3
  import { AbstractArchivist } from '@xyo-network/archivist-abstract'
4
- import { ArchivistInsertQuerySchema } from '@xyo-network/archivist-model'
4
+ import type { ArchivistNextOptions } from '@xyo-network/archivist-model'
5
+ import { ArchivistInsertQuerySchema, ArchivistNextQuerySchema } from '@xyo-network/archivist-model'
5
6
  import { MongoDBArchivistConfigSchema } from '@xyo-network/archivist-model-mongodb'
6
7
  import { MongoDBModuleMixin } from '@xyo-network/module-abstract-mongodb'
7
8
  import { PayloadBuilder } from '@xyo-network/payload-builder'
@@ -11,6 +12,7 @@ import type {
11
12
  import type { PayloadWithMongoMeta } from '@xyo-network/payload-mongodb'
12
13
  import { fromDbRepresentation, toDbRepresentation } from '@xyo-network/payload-mongodb'
13
14
  import { PayloadWrapper } from '@xyo-network/payload-wrapper'
15
+ import { ObjectId } from 'mongodb'
14
16
 
15
17
  import { validByType } from './lib/index.js'
16
18
 
@@ -20,13 +22,38 @@ export class MongoDBArchivist extends MongoDBArchivistBase {
20
22
  static override readonly configSchemas: Schema[] = [...super.configSchemas, MongoDBArchivistConfigSchema]
21
23
  static override readonly defaultConfigSchema: Schema = MongoDBArchivistConfigSchema
22
24
 
23
- override readonly queries: string[] = [ArchivistInsertQuerySchema, ...super.queries]
25
+ override readonly queries: string[] = [ArchivistInsertQuerySchema, ArchivistNextQuerySchema, ...super.queries]
26
+
27
+ /**
28
+ * The amount of time to allow the aggregate query to execute
29
+ */
30
+ protected readonly aggregateTimeoutMs = 10_000
24
31
 
25
32
  override async head(): Promise<Payload | undefined> {
26
- const head = await (await this.payloads.find({})).sort({ _timestamp: -1 }).limit(1).toArray()
33
+ const head = await this.next({ limit: 1, order: 'desc' })
27
34
  return head[0] ? PayloadWrapper.wrap(head[0]).payload : undefined
28
35
  }
29
36
 
37
+ protected async findOneByHash(hash: Hash) {
38
+ const dataPayload = (await this.payloads.findOne({ _$hash: hash }))
39
+ if (dataPayload) {
40
+ return dataPayload
41
+ } else {
42
+ const dataBw = (await this.boundWitnesses.findOne({ _$hash: hash }))
43
+ if (dataBw) {
44
+ return dataBw
45
+ } else {
46
+ const payload = (await this.payloads.findOne({ _hash: hash }))
47
+ if (payload) {
48
+ return payload
49
+ } else {
50
+ const bw = (await this.boundWitnesses.findOne({ _hash: hash }))
51
+ return bw ?? undefined
52
+ }
53
+ }
54
+ }
55
+ }
56
+
30
57
  protected override async getHandler(hashes: Hash[]): Promise<WithMeta<Payload>[]> {
31
58
  let remainingHashes = [...hashes]
32
59
 
@@ -67,7 +94,73 @@ export class MongoDBArchivist extends MongoDBArchivistBase {
67
94
  throw new Error('MongoDBDeterministicArchivist: Error inserting BoundWitnesses')
68
95
  }
69
96
 
70
- return await PayloadBuilder.build([...boundWitnessesWithExternalMeta, ...payloadsWithExternalMeta])
97
+ return await PayloadBuilder.build([...boundWitnessesWithExternalMeta, ...payloadsWithExternalMeta].map(fromDbRepresentation))
98
+ }
99
+
100
+ protected override async nextHandler(options?: ArchivistNextOptions): Promise<WithMeta<Payload>[]> {
101
+ // Sanitize inputs and set defaults
102
+ let {
103
+ limit, offset, order,
104
+ } = options ?? { limit: 10, order: 'desc' }
105
+
106
+ if (!limit) limit = 10
107
+ if (limit > 100) limit = 100
108
+
109
+ if (order != 'asc') order = 'desc'
110
+
111
+ let id: ObjectId | undefined
112
+ if (offset) {
113
+ const payload = await this.findOneByHash(offset)
114
+ // TODO: Should we throw an error if the requested payload is not found?
115
+ if (payload) id = payload._id
116
+ } else {
117
+ id = order === 'asc'
118
+ // If ascending, start from the beginning of time
119
+ ? ObjectId.createFromTime(0)
120
+ // If descending, start from now (plus a bit more in the future to ensure
121
+ // them most recent ObjectIds are included)
122
+ : ObjectId.createFromTime((Date.now() + 10_000) / 1000)
123
+ }
124
+ if (!id) return []
125
+
126
+ // Create aggregate criteria
127
+ const sort = order === 'asc' ? 1 : -1
128
+ // TODO: How to handle random component of ID across multiple collections
129
+ // to ensure we don't skip some payloads
130
+ const match = order === 'asc' ? { _id: { $gt: id } } : { _id: { $lt: id } }
131
+
132
+ // Run the aggregate query
133
+ const foundPayloads = await this.payloads.useCollection((collection) => {
134
+ return collection
135
+ .aggregate<PayloadWithMongoMeta>([
136
+ // Pre-filter payloads collection
137
+ { $match: match },
138
+ // Sort payloads by _id
139
+ { $sort: { _id: sort } },
140
+ // Limit payloads to the first N payloads
141
+ { $limit: limit },
142
+ // Combine with filtered boundWitnesses collection
143
+ {
144
+ $unionWith: {
145
+ coll: this.boundWitnessSdkConfig.collection,
146
+ pipeline: [
147
+ { $match: match }, // Pre-filter boundWitnesses
148
+ { $sort: { _id: sort } }, // Sort boundWitnesses by _id
149
+ { $limit: limit }, // Limit boundWitnesses to the first N boundWitnesses
150
+ ],
151
+ },
152
+ },
153
+ // Sort the combined result by _id
154
+ { $sort: { _id: sort } },
155
+ // Limit the final result to N documents
156
+ { $limit: limit },
157
+ ])
158
+ .maxTimeMS(this.aggregateTimeoutMs)
159
+ .toArray()
160
+ })
161
+
162
+ // Convert from DB representation to Payloads
163
+ return await PayloadBuilder.build(foundPayloads.map(fromDbRepresentation))
71
164
  }
72
165
 
73
166
  protected override async startHandler() {