@event-driven-io/pongo 0.17.0-beta.41 → 0.17.0-beta.43

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.
Files changed (61) hide show
  1. package/dist/cli.cjs +571 -16
  2. package/dist/cli.cjs.map +1 -1
  3. package/dist/cli.js +560 -3
  4. package/dist/cli.js.map +1 -1
  5. package/dist/cloudflare-CzlFuMmZ.js +38 -0
  6. package/dist/cloudflare-CzlFuMmZ.js.map +1 -0
  7. package/dist/cloudflare-WGIFZXop.cjs +42 -0
  8. package/dist/cloudflare-WGIFZXop.cjs.map +1 -0
  9. package/dist/cloudflare.cjs +1463 -12
  10. package/dist/cloudflare.cjs.map +1 -1
  11. package/dist/cloudflare.d.cts +435 -2
  12. package/dist/cloudflare.d.ts +435 -2
  13. package/dist/cloudflare.js +1455 -3
  14. package/dist/cloudflare.js.map +1 -1
  15. package/dist/{core-CH0SOCr3.js → core-BW9XZXW8.js} +33 -46
  16. package/dist/core-BW9XZXW8.js.map +1 -0
  17. package/dist/{core-CkmE5dkK.cjs → core-BicrzIKX.cjs} +38 -52
  18. package/dist/core-BicrzIKX.cjs.map +1 -0
  19. package/dist/index.cjs +1477 -52
  20. package/dist/index.cjs.map +1 -1
  21. package/dist/index.d.cts +717 -1
  22. package/dist/index.d.ts +717 -1
  23. package/dist/index.js +1429 -4
  24. package/dist/index.js.map +1 -1
  25. package/dist/pg-BdtFllz7.cjs +399 -0
  26. package/dist/pg-BdtFllz7.cjs.map +1 -0
  27. package/dist/pg-BtUtOO8Y.js +395 -0
  28. package/dist/pg-BtUtOO8Y.js.map +1 -0
  29. package/dist/pg.cjs +1155 -38
  30. package/dist/pg.cjs.map +1 -1
  31. package/dist/pg.d.cts +430 -2
  32. package/dist/pg.d.ts +430 -2
  33. package/dist/pg.js +1145 -27
  34. package/dist/pg.js.map +1 -1
  35. package/dist/shim.cjs +554 -6
  36. package/dist/shim.cjs.map +1 -1
  37. package/dist/shim.d.cts +445 -1
  38. package/dist/shim.d.ts +445 -1
  39. package/dist/shim.js +551 -2
  40. package/dist/shim.js.map +1 -1
  41. package/dist/sqlite3-CFwH2ot3.cjs +44 -0
  42. package/dist/sqlite3-CFwH2ot3.cjs.map +1 -0
  43. package/dist/sqlite3-Dv0y-lg6.js +40 -0
  44. package/dist/sqlite3-Dv0y-lg6.js.map +1 -0
  45. package/dist/sqlite3.cjs +1463 -12
  46. package/dist/sqlite3.cjs.map +1 -1
  47. package/dist/sqlite3.d.cts +435 -2
  48. package/dist/sqlite3.d.ts +435 -2
  49. package/dist/sqlite3.js +1455 -3
  50. package/dist/sqlite3.js.map +1 -1
  51. package/package.json +2 -2
  52. package/dist/core-BHdOCUrr.js +0 -1429
  53. package/dist/core-BHdOCUrr.js.map +0 -1
  54. package/dist/core-C9SB3XMx.cjs +0 -1717
  55. package/dist/core-C9SB3XMx.cjs.map +0 -1
  56. package/dist/core-CH0SOCr3.js.map +0 -1
  57. package/dist/core-CkmE5dkK.cjs.map +0 -1
  58. package/dist/index-C3pnS1S_.d.cts +0 -720
  59. package/dist/index-CZOmOsQt.d.ts +0 -10
  60. package/dist/index-FXnldVnn.d.cts +0 -10
  61. package/dist/index-r7V4paf_.d.ts +0 -720
package/dist/sqlite3.cjs CHANGED
@@ -1,16 +1,1467 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
- const require_core = require('./core-C9SB3XMx.cjs');
3
- const require_cli = require('./cli.cjs');
4
- const require_core$1 = require('./core-CkmE5dkK.cjs');
5
2
  let _event_driven_io_dumbo = require("@event-driven-io/dumbo");
3
+ let _event_driven_io_dumbo_sqlite = require("@event-driven-io/dumbo/sqlite");
4
+ let lru_cache = require("lru-cache");
5
+ let uuid = require("uuid");
6
6
  let _event_driven_io_dumbo_sqlite3 = require("@event-driven-io/dumbo/sqlite3");
7
7
 
8
+ //#region src/core/cache/pongoCacheWrapper.ts
9
+ const pongoCacheWrapper = (options) => {
10
+ const { provider, hooks } = options;
11
+ const onError = (error, operation) => {
12
+ hooks?.onError?.(error, operation);
13
+ };
14
+ let isClosed = false;
15
+ return {
16
+ cacheType: provider.cacheType,
17
+ async get(key) {
18
+ try {
19
+ const result = await provider.get(key);
20
+ if (result !== void 0) hooks?.onHit?.(key);
21
+ else hooks?.onMiss?.(key);
22
+ return result;
23
+ } catch (error) {
24
+ onError(error, "get");
25
+ hooks?.onMiss?.(key);
26
+ return;
27
+ }
28
+ },
29
+ async getMany(keys) {
30
+ try {
31
+ return await provider.getMany(keys);
32
+ } catch (error) {
33
+ onError(error, "getMany");
34
+ return [];
35
+ }
36
+ },
37
+ async set(key, value) {
38
+ try {
39
+ await provider.set(key, value);
40
+ } catch (error) {
41
+ onError(error, "set");
42
+ }
43
+ },
44
+ async setMany(entries) {
45
+ try {
46
+ await provider.setMany(entries);
47
+ } catch (error) {
48
+ onError(error, "setMany");
49
+ }
50
+ },
51
+ async update(key, updater) {
52
+ try {
53
+ await provider.update(key, updater);
54
+ } catch (error) {
55
+ onError(error, "update");
56
+ }
57
+ },
58
+ async updateMany(keys, updater) {
59
+ try {
60
+ await provider.updateMany(keys, updater);
61
+ } catch (error) {
62
+ onError(error, "updateMany");
63
+ }
64
+ },
65
+ async delete(key) {
66
+ try {
67
+ await provider.delete(key);
68
+ hooks?.onEvict?.(key);
69
+ } catch (error) {
70
+ onError(error, "delete");
71
+ }
72
+ },
73
+ async deleteMany(keys) {
74
+ try {
75
+ await provider.deleteMany(keys);
76
+ for (const key of keys) hooks?.onEvict?.(key);
77
+ } catch (error) {
78
+ onError(error, "deleteMany");
79
+ }
80
+ },
81
+ clear() {
82
+ return provider.clear();
83
+ },
84
+ close() {
85
+ if (isClosed) return;
86
+ isClosed = true;
87
+ return provider.close();
88
+ }
89
+ };
90
+ };
91
+
92
+ //#endregion
93
+ //#region src/core/cache/providers/identityMapCache.ts
94
+ const identityMapCache = () => {
95
+ const store = /* @__PURE__ */ new Map();
96
+ return {
97
+ cacheType: "pongo:cache:identity-map",
98
+ get: (key) => Promise.resolve(store.has(key) ? store.get(key) : void 0),
99
+ getMany: (keys) => keys.map((k) => store.has(k) ? store.get(k) : void 0),
100
+ set: (key, value) => {
101
+ store.set(key, value);
102
+ },
103
+ setMany: (entries) => {
104
+ for (const { key, value } of entries) store.set(key, value);
105
+ },
106
+ update: (key, _updater) => {
107
+ store.delete(key);
108
+ },
109
+ updateMany: (keys, _updater) => {
110
+ for (const key of keys) store.delete(key);
111
+ },
112
+ delete: (key) => {
113
+ store.delete(key);
114
+ },
115
+ deleteMany: (keys) => {
116
+ for (const key of keys) store.delete(key);
117
+ },
118
+ clear: () => {
119
+ store.clear();
120
+ },
121
+ close: () => {
122
+ store.clear();
123
+ }
124
+ };
125
+ };
126
+
127
+ //#endregion
128
+ //#region src/core/cache/providers/lruCache.ts
129
+ const defaultLRUCacheOptions = { max: 1e3 };
130
+ const lruCache = (options) => {
131
+ const cache = new lru_cache.LRUCache({
132
+ ...defaultLRUCacheOptions,
133
+ ...options
134
+ });
135
+ return {
136
+ cacheType: "pongo:cache:lru",
137
+ get: (key) => {
138
+ const entry = cache.get(key);
139
+ if (entry === void 0) return void 0;
140
+ return entry.doc;
141
+ },
142
+ getMany: (keys) => keys.map((k) => {
143
+ const entry = cache.get(k);
144
+ if (entry === void 0) return void 0;
145
+ return entry.doc;
146
+ }),
147
+ set: (key, value) => {
148
+ cache.set(key, { doc: value });
149
+ },
150
+ setMany: (entries) => {
151
+ for (const { key, value } of entries) cache.set(key, { doc: value });
152
+ },
153
+ update: (key, _updater) => {
154
+ cache.delete(key);
155
+ },
156
+ updateMany(keys, _updater) {
157
+ for (const key of keys) cache.delete(key);
158
+ },
159
+ delete: (key) => {
160
+ cache.delete(key);
161
+ },
162
+ deleteMany: (keys) => {
163
+ for (const key of keys) cache.delete(key);
164
+ },
165
+ clear: () => {
166
+ cache.clear();
167
+ },
168
+ close: () => {
169
+ cache.clear();
170
+ }
171
+ };
172
+ };
173
+
174
+ //#endregion
175
+ //#region src/core/cache/providers/noopCache.ts
176
+ const noopCacheProvider = {
177
+ cacheType: "pongo:cache:no-op",
178
+ get: () => void 0,
179
+ set: () => {},
180
+ update: () => {},
181
+ delete: () => {},
182
+ getMany: (keys) => keys.map(() => void 0),
183
+ setMany: () => {},
184
+ updateMany: () => {},
185
+ deleteMany: () => {},
186
+ clear: () => {},
187
+ close: () => {}
188
+ };
189
+
190
+ //#endregion
191
+ //#region src/core/cache/pongoCache.ts
192
+ const DEFAULT_CONFIG = { type: "in-memory" };
193
+ const pongoCache = (options) => {
194
+ if (options === void 0 || options === "disabled") return noopCacheProvider;
195
+ if ("cacheType" in options) return options;
196
+ const config = options ?? DEFAULT_CONFIG;
197
+ if (config.type === "identity-map") return identityMapCache();
198
+ return pongoCacheWrapper({ provider: lruCache(config) });
199
+ };
200
+
201
+ //#endregion
202
+ //#region src/core/collection/filters/filters.ts
203
+ const asPlainObjectWithSingleKey = (filter, key) => filter && typeof filter === "object" && !Array.isArray(filter) && Object.keys(filter).length === 1 && key in filter ? { [key]: filter[key] } : void 0;
204
+ const idFromFilter = (filter) => {
205
+ const idFilter = asPlainObjectWithSingleKey(filter, "_id");
206
+ return typeof idFilter?.["_id"] === "string" ? idFilter["_id"] : void 0;
207
+ };
208
+ const getIdsFromIdOnlyFilter = (filter) => {
209
+ const idFilter = asPlainObjectWithSingleKey(filter, "_id");
210
+ if (!idFilter) return void 0;
211
+ const idValue = idFilter["_id"];
212
+ if (typeof idValue === "string") return [idValue];
213
+ const $in = idValue && typeof idValue === "object" && "$in" in idValue ? idValue["$in"] : void 0;
214
+ return Array.isArray($in) && $in.every((i) => typeof i === "string") ? $in : void 0;
215
+ };
216
+
217
+ //#endregion
218
+ //#region src/core/collection/handle.ts
219
+ function DocumentCommandHandler(deps) {
220
+ const fn = async (input, handle, options) => {
221
+ const results = (await handleDocuments(deps.storage, normalizeInput(input), handle, options)).map(({ change, result: outcome }) => toHandleResult(deps, change, outcome));
222
+ return Array.isArray(input) ? results : results[0];
223
+ };
224
+ return fn;
225
+ }
226
+ async function handleDocuments(storage, inputs, handle, options) {
227
+ if (inputs.length === 0) return [];
228
+ const { parallel, ...operationOptions } = options ?? {};
229
+ await storage.ensureCollectionCreated(operationOptions);
230
+ const docs = await storage.fetchByIds(inputs.map((i) => i._id), operationOptions);
231
+ return await executeStorageChanges(storage, await mapAsync(inputs, (item, i) => handleDocument({
232
+ ...item,
233
+ existing: docs[i] ?? null
234
+ }, handle), { parallel }), operationOptions);
235
+ }
236
+ async function handleDocument(item, handle) {
237
+ const { _id: id, existing, expectedVersion } = item;
238
+ if (hasVersionMismatch(existing, expectedVersion)) return {
239
+ type: "noop",
240
+ existing,
241
+ versionMismatch: true
242
+ };
243
+ const handlers = Array.isArray(handle) ? handle : [handle];
244
+ let result = existing ? { ...existing } : null;
245
+ for (const handler of handlers) result = await handler(result, id);
246
+ return toDocumentChange(id, existing, result);
247
+ }
248
+ async function executeStorageChanges(storage, changes, operationOptions) {
249
+ const toInsert = changes.flatMap((c) => c.type === "insert" ? [c.doc] : []);
250
+ const toReplace = changes.flatMap((c) => {
251
+ if (c.type !== "replace") return [];
252
+ const { _version: _, ...cleanResult } = c.result;
253
+ return [c._version !== void 0 ? {
254
+ ...cleanResult,
255
+ _version: c._version
256
+ } : cleanResult];
257
+ });
258
+ const toDelete = changes.flatMap((c) => c.type === "delete" ? [c._version !== void 0 ? {
259
+ _id: c.docId,
260
+ _version: c._version
261
+ } : { _id: c.docId }] : []);
262
+ const insertedIds = toInsert.length > 0 ? new Set((await storage.insertMany(toInsert, operationOptions)).insertedIds) : /* @__PURE__ */ new Set();
263
+ const replaceResult = toReplace.length > 0 ? await storage.replaceMany(toReplace, operationOptions) : null;
264
+ const deletedIds = toDelete.length > 0 ? (await storage.deleteManyByIds(toDelete, operationOptions)).deletedIds : /* @__PURE__ */ new Set();
265
+ const toDocumentHandlerResult = (change) => {
266
+ if (change.type === "noop") return { succeeded: !change.versionMismatch };
267
+ if (change.type === "insert") return {
268
+ succeeded: insertedIds.has(change.doc._id),
269
+ newVersion: 1n
270
+ };
271
+ if (change.type === "delete") return { succeeded: deletedIds.has(change.docId) };
272
+ const id = change.result._id;
273
+ return {
274
+ succeeded: replaceResult?.modifiedIds.includes(id) ?? false,
275
+ newVersion: replaceResult?.nextExpectedVersions.get(id) ?? 0n
276
+ };
277
+ };
278
+ return changes.map((change) => ({
279
+ change,
280
+ result: toDocumentHandlerResult(change)
281
+ }));
282
+ }
283
+ function normalizeInput(input) {
284
+ if (typeof input === "string") return [{ _id: input }];
285
+ if (!Array.isArray(input)) return [input];
286
+ return input.map((item) => typeof item === "string" ? { _id: item } : item);
287
+ }
288
+ function hasVersionMismatch(existing, version) {
289
+ const expected = expectedVersionValue(version);
290
+ return existing == null && version === "DOCUMENT_EXISTS" || existing == null && expected != null || existing != null && version === "DOCUMENT_DOES_NOT_EXIST" || existing != null && expected !== null && existing._version !== expected;
291
+ }
292
+ function toDocumentChange(docId, existing, result) {
293
+ if (deepEquals(existing, result)) return {
294
+ type: "noop",
295
+ existing
296
+ };
297
+ if (!existing && result) return {
298
+ type: "insert",
299
+ doc: {
300
+ ...result,
301
+ _id: docId
302
+ }
303
+ };
304
+ if (existing && !result) return {
305
+ type: "delete",
306
+ docId,
307
+ _version: existing._version
308
+ };
309
+ return {
310
+ type: "replace",
311
+ existing,
312
+ result: {
313
+ ...result,
314
+ _id: docId
315
+ },
316
+ _version: existing._version
317
+ };
318
+ }
319
+ function toHandleResult(deps, change, { succeeded, newVersion }) {
320
+ const opMeta = {
321
+ operationName: "handle",
322
+ collectionName: deps.collectionName,
323
+ serializer: deps.serializer,
324
+ errors: deps.errors
325
+ };
326
+ const toResult = (op, document) => ({
327
+ ...operationResult(op, opMeta),
328
+ document
329
+ });
330
+ if (change.type === "noop") return toResult({ successful: succeeded }, change.existing);
331
+ if (change.type === "insert") return toResult({
332
+ successful: succeeded,
333
+ insertedId: succeeded ? change.doc._id : null,
334
+ nextExpectedVersion: 1n
335
+ }, succeeded ? {
336
+ ...change.doc,
337
+ _version: 1n
338
+ } : null);
339
+ if (change.type === "delete") return toResult({
340
+ successful: succeeded,
341
+ deletedCount: succeeded ? 1 : 0,
342
+ matchedCount: 1
343
+ }, null);
344
+ return toResult({
345
+ successful: succeeded,
346
+ modifiedCount: succeeded ? 1 : 0,
347
+ matchedCount: 1,
348
+ nextExpectedVersion: newVersion ?? 0n
349
+ }, succeeded ? {
350
+ ...change.result,
351
+ _version: newVersion
352
+ } : change.existing);
353
+ }
354
+
355
+ //#endregion
356
+ //#region src/core/collection/pongoCollection.ts
357
+ const enlistIntoTransactionIfActive = async (db, options) => {
358
+ const transaction = options?.session?.transaction;
359
+ if (!transaction || !transaction.isActive) return null;
360
+ return await transaction.enlistDatabase(db);
361
+ };
362
+ const transactionExecutorOrDefault = async (db, options, defaultSqlExecutor) => {
363
+ return (await enlistIntoTransactionIfActive(db, options))?.execute ?? defaultSqlExecutor;
364
+ };
365
+ const pongoCollection = ({ db, collectionName, pool, schemaComponent, schema, errors, serializer, cache: cacheOptions }) => {
366
+ const SqlFor = schemaComponent.sqlBuilder;
367
+ const sqlExecutor = pool.execute;
368
+ const cache = pongoCache(cacheOptions);
369
+ const columnMapping = { mapping: {
370
+ ...(0, _event_driven_io_dumbo.mapColumnToJSON)("data", serializer),
371
+ ...(0, _event_driven_io_dumbo.mapColumnToBigint)("_version")
372
+ } };
373
+ const command = async (sql, options) => (await transactionExecutorOrDefault(db, options, sqlExecutor)).command(sql, columnMapping);
374
+ const query = async (sql, options) => (await transactionExecutorOrDefault(db, options, sqlExecutor)).query(sql, columnMapping);
375
+ let shouldMigrate = schema?.autoMigration !== "None";
376
+ const createCollection = (options) => {
377
+ shouldMigrate = false;
378
+ if (options?.session) return command(SqlFor.createCollection(), options);
379
+ else return command(SqlFor.createCollection());
380
+ };
381
+ const ensureCollectionCreated = (options) => {
382
+ if (!shouldMigrate) return Promise.resolve();
383
+ return createCollection(options);
384
+ };
385
+ const upcast = schema?.versioning?.upcast ?? ((doc) => doc);
386
+ const downcast = schema?.versioning?.downcast ?? ((doc) => doc);
387
+ const toStored = (document) => ({
388
+ ...downcast(document),
389
+ _id: document._id,
390
+ _version: document._version
391
+ });
392
+ const fromStored = (stored) => ({
393
+ ...upcast(stored),
394
+ _id: stored._id,
395
+ _version: stored._version
396
+ });
397
+ const findOneStoredFromDb = async (filter, options) => {
398
+ const row = (await query(SqlFor.findOne(filter), options)).rows[0];
399
+ return row ? {
400
+ ...row.data,
401
+ _id: row._id,
402
+ _version: row._version
403
+ } : null;
404
+ };
405
+ const cacheKey = (id) => `${db.databaseName}:${collectionName}:${id}`;
406
+ const txCacheFor = (options) => options?.session?.transaction?.cache ?? null;
407
+ const resolveFromCache = async (key, options) => {
408
+ const txCache = txCacheFor(options);
409
+ if (txCache) {
410
+ const cached = await txCache.get(key);
411
+ if (cached !== void 0) return cached;
412
+ }
413
+ return cache.get(key);
414
+ };
415
+ const findManyFromCache = async (keys, options) => {
416
+ const txCache = txCacheFor(options);
417
+ if (!txCache) return cache.getMany(keys);
418
+ const txResults = await txCache.getMany(keys);
419
+ const mainResults = await cache.getMany(keys);
420
+ return keys.map((_, i) => txResults[i] !== void 0 ? txResults[i] : mainResults[i]);
421
+ };
422
+ const fetchByIds = async (ids, options) => {
423
+ const cachedResults = await findManyFromCache(ids.map(cacheKey), options);
424
+ const missIds = ids.filter((_, i) => cachedResults[i] === void 0);
425
+ let dbDocsById = /* @__PURE__ */ new Map();
426
+ if (missIds.length > 0) {
427
+ const dbDocs = (await query(SqlFor.find({ _id: { $in: missIds } }, options))).rows.map((row) => ({
428
+ ...row.data,
429
+ _id: row._id,
430
+ _version: row._version
431
+ }));
432
+ dbDocsById = new Map(dbDocs.map((d) => [d._id, d]));
433
+ const leftovers = missIds.map((id) => [id, dbDocsById.get(id) ?? null]);
434
+ await cacheSetMany(leftovers.map((d) => d[1]).filter((d) => d !== null), options);
435
+ await cacheDeleteMany(leftovers.filter(([, doc]) => doc === null).map(([id]) => id), options);
436
+ }
437
+ return ids.map((id, i) => {
438
+ const cached = cachedResults[i];
439
+ if (cached !== void 0) return cached;
440
+ return dbDocsById.get(id) ?? null;
441
+ });
442
+ };
443
+ const findManyByIds = async (ids, options) => {
444
+ return (await fetchByIds(ids, options)).filter((doc) => doc !== null).map(fromStored);
445
+ };
446
+ const cacheSet = (value, options) => {
447
+ const key = cacheKey(value._id);
448
+ const txCache = txCacheFor(options);
449
+ if (txCache) return txCache.set(key, value, { mainCache: cache });
450
+ return cache.set(key, value);
451
+ };
452
+ const cacheSetMany = (documents, options) => {
453
+ const txCache = txCacheFor(options);
454
+ const entries = documents.map((d) => ({
455
+ key: cacheKey(d._id),
456
+ value: d
457
+ }));
458
+ if (txCache) return txCache.setMany(entries, { mainCache: cache });
459
+ return cache.setMany(entries);
460
+ };
461
+ const cacheDelete = (id, options) => {
462
+ const txCache = txCacheFor(options);
463
+ const key = cacheKey(id);
464
+ if (txCache) return txCache.delete(key, { mainCache: cache });
465
+ return cache.delete(key);
466
+ };
467
+ const cacheDeleteMany = (ids, options) => {
468
+ const txCache = txCacheFor(options);
469
+ const keys = ids.map(cacheKey);
470
+ if (txCache) return txCache.deleteMany(keys, { mainCache: cache });
471
+ return cache.deleteMany(keys);
472
+ };
473
+ const deleteManyByIds = async (ids, options) => {
474
+ await ensureCollectionCreated(options);
475
+ const result = await command(SqlFor.deleteManyByIds(ids), options);
476
+ const deletedIds = new Set(result.rows.filter((row) => (row.deleted ?? 1) > 0).map((row) => row._id));
477
+ if (!options?.skipCache) await cacheDeleteMany([...deletedIds], options);
478
+ return operationResult({
479
+ successful: deletedIds.size > 0,
480
+ deletedCount: deletedIds.size,
481
+ matchedCount: ids.length,
482
+ deletedIds
483
+ }, {
484
+ operationName: "deleteManyByIds",
485
+ collectionName,
486
+ serializer,
487
+ errors
488
+ });
489
+ };
490
+ const executeUpsert = async (storedRows, options) => {
491
+ const result = await command(SqlFor.insertOrReplace(storedRows), options);
492
+ const writtenIds = result.rows.map((r) => r._id);
493
+ const versions = new Map(result.rows.map((r) => [r._id, BigInt(r.version ?? 1n)]));
494
+ if (!options?.skipCache) {
495
+ const writtenSet = new Set(writtenIds);
496
+ const cacheEntries = storedRows.filter((r) => writtenSet.has(r._id)).map((r) => ({
497
+ ...r,
498
+ _version: versions.get(r._id) ?? 1n
499
+ }));
500
+ if (cacheEntries.length > 0) await cacheSetMany(cacheEntries, options);
501
+ }
502
+ return {
503
+ writtenIds,
504
+ versions
505
+ };
506
+ };
507
+ const collection = {
508
+ dbName: db.databaseName,
509
+ collectionName,
510
+ createCollection: async (options) => {
511
+ await createCollection(options);
512
+ },
513
+ insertOne: async (document, options) => {
514
+ await ensureCollectionCreated(options);
515
+ const _id = document._id ?? (0, uuid.v7)();
516
+ const _version = document._version ?? 1n;
517
+ const stored = toStored({
518
+ ...document,
519
+ _id,
520
+ _version
521
+ });
522
+ if (options?.upsert) {
523
+ const { writtenIds, versions } = await executeUpsert([stored], options);
524
+ const successful = writtenIds.length > 0;
525
+ const nextExpectedVersion = versions.get(_id) ?? _version;
526
+ return operationResult({
527
+ successful,
528
+ insertedId: successful ? _id : null,
529
+ nextExpectedVersion
530
+ }, {
531
+ operationName: "insertOne",
532
+ collectionName,
533
+ serializer,
534
+ errors
535
+ });
536
+ }
537
+ const successful = ((await command(SqlFor.insertOne(stored), options)).rowCount ?? 0) > 0;
538
+ if (successful && !options?.skipCache) await cacheSet(stored, options);
539
+ return operationResult({
540
+ successful,
541
+ insertedId: successful ? _id : null,
542
+ nextExpectedVersion: _version
543
+ }, {
544
+ operationName: "insertOne",
545
+ collectionName,
546
+ serializer,
547
+ errors
548
+ });
549
+ },
550
+ insertMany: async (documents, options) => {
551
+ await ensureCollectionCreated(options);
552
+ const rows = documents.map((doc) => doc._id && doc._version ? doc : {
553
+ ...doc,
554
+ _id: doc._id ?? (0, uuid.v7)(),
555
+ _version: doc._version ?? 1n
556
+ }).map((d) => toStored(d));
557
+ if (options?.upsert) {
558
+ const { writtenIds } = await executeUpsert(rows, options);
559
+ return operationResult({
560
+ successful: writtenIds.length === rows.length,
561
+ insertedCount: writtenIds.length,
562
+ insertedIds: writtenIds
563
+ }, {
564
+ operationName: "insertMany",
565
+ collectionName,
566
+ serializer,
567
+ errors
568
+ });
569
+ }
570
+ const result = await command(SqlFor.insertMany(rows), options);
571
+ if (!options?.skipCache) {
572
+ const insertedIdSet = new Set(result.rows.map((d) => d._id));
573
+ await cacheSetMany(rows.filter((r) => insertedIdSet.has(r._id)), options);
574
+ }
575
+ return operationResult({
576
+ successful: result.rowCount === rows.length,
577
+ insertedCount: result.rowCount ?? 0,
578
+ insertedIds: result.rows.map((d) => d._id)
579
+ }, {
580
+ operationName: "insertMany",
581
+ collectionName,
582
+ serializer,
583
+ errors
584
+ });
585
+ },
586
+ updateOne: async (filter, update, options) => {
587
+ await ensureCollectionCreated(options);
588
+ const result = await command(SqlFor.updateOne(filter, update, options), options);
589
+ const opResult = operationResult({
590
+ successful: result.rows.length > 0 && result.rows[0].modified === result.rows[0].matched,
591
+ modifiedCount: Number(result.rows[0]?.modified ?? 0),
592
+ matchedCount: Number(result.rows[0]?.matched ?? 0),
593
+ upsertedId: null,
594
+ upsertedCount: 0,
595
+ nextExpectedVersion: BigInt(result.rows[0]?.version ?? 0n)
596
+ }, {
597
+ operationName: "updateOne",
598
+ collectionName,
599
+ serializer,
600
+ errors
601
+ });
602
+ if (opResult.successful && !options?.skipCache) {
603
+ const id = idFromFilter(filter);
604
+ if (id) await cacheDelete(id, options);
605
+ }
606
+ return opResult;
607
+ },
608
+ replaceOne: async (filter, document, options) => {
609
+ await ensureCollectionCreated(options);
610
+ const noConcurrencyCheck = options?.expectedVersion === void 0 || options.expectedVersion === "NO_CONCURRENCY_CHECK";
611
+ if (options?.upsert && noConcurrencyCheck) {
612
+ const _id = idFromFilter(filter) ?? (0, uuid.v7)();
613
+ const { writtenIds, versions } = await executeUpsert([toStored({
614
+ ...document,
615
+ _id,
616
+ _version: 1n
617
+ })], options);
618
+ const written = writtenIds.length > 0;
619
+ const nextExpectedVersion = versions.get(_id) ?? 1n;
620
+ const inserted = written && nextExpectedVersion === 1n;
621
+ return operationResult({
622
+ successful: written,
623
+ modifiedCount: written && !inserted ? 1 : 0,
624
+ matchedCount: written && !inserted ? 1 : 0,
625
+ upsertedId: inserted ? _id : null,
626
+ upsertedCount: inserted ? 1 : 0,
627
+ nextExpectedVersion
628
+ }, {
629
+ operationName: "replaceOne",
630
+ collectionName,
631
+ serializer,
632
+ errors
633
+ });
634
+ }
635
+ const downcasted = downcast(document);
636
+ const result = await command(SqlFor.replaceOne(filter, downcasted, options), options);
637
+ const opResult = operationResult({
638
+ successful: result.rows.length > 0 && result.rows[0].modified > 0,
639
+ modifiedCount: Number(result.rows[0]?.modified ?? 0),
640
+ matchedCount: Number(result.rows[0]?.matched ?? 0),
641
+ upsertedId: null,
642
+ upsertedCount: 0,
643
+ nextExpectedVersion: BigInt(result.rows[0]?.version ?? 0n)
644
+ }, {
645
+ operationName: "replaceOne",
646
+ collectionName,
647
+ serializer,
648
+ errors
649
+ });
650
+ if (opResult.successful && !options?.skipCache) {
651
+ const _id = idFromFilter(filter);
652
+ if (_id) await cacheSet(toStored({
653
+ ...document,
654
+ _id,
655
+ _version: opResult.nextExpectedVersion
656
+ }), options);
657
+ }
658
+ return opResult;
659
+ },
660
+ updateMany: async (filter, update, options) => {
661
+ await ensureCollectionCreated(options);
662
+ const result = await command(SqlFor.updateMany(filter, update), options);
663
+ return operationResult({
664
+ successful: true,
665
+ modifiedCount: result.rowCount ?? 0,
666
+ matchedCount: result.rowCount ?? 0
667
+ }, {
668
+ operationName: "updateMany",
669
+ collectionName,
670
+ serializer,
671
+ errors
672
+ });
673
+ },
674
+ deleteOne: async (filter, options) => {
675
+ await ensureCollectionCreated(options);
676
+ const result = await command(SqlFor.deleteOne(filter ?? {}, options), options);
677
+ const opResult = operationResult({
678
+ successful: result.rows.length > 0 && result.rows[0].deleted > 0,
679
+ deletedCount: Number(result.rows[0]?.deleted ?? 0),
680
+ matchedCount: Number(result.rows[0]?.matched ?? 0)
681
+ }, {
682
+ operationName: "deleteOne",
683
+ collectionName,
684
+ serializer,
685
+ errors
686
+ });
687
+ if (opResult.successful && !options?.skipCache && filter) {
688
+ const id = idFromFilter(filter);
689
+ if (id) await cacheDelete(id, options);
690
+ }
691
+ return opResult;
692
+ },
693
+ deleteMany: async (filter, options) => {
694
+ const ids = filter ? getIdsFromIdOnlyFilter(filter) : null;
695
+ if (ids) return deleteManyByIds(ids.map((id) => ({ _id: id })), options);
696
+ await ensureCollectionCreated(options);
697
+ const result = await command(SqlFor.deleteMany(filter ?? {}), options);
698
+ return operationResult({
699
+ successful: (result.rowCount ?? 0) > 0,
700
+ deletedCount: result.rowCount ?? 0,
701
+ matchedCount: result.rowCount ?? 0
702
+ }, {
703
+ operationName: "deleteMany",
704
+ collectionName,
705
+ serializer,
706
+ errors
707
+ });
708
+ },
709
+ findOne: async (filter, options) => {
710
+ await ensureCollectionCreated(options);
711
+ const id = filter && !options?.skipCache ? idFromFilter(filter) : null;
712
+ if (id) {
713
+ const cached = await resolveFromCache(cacheKey(id), options);
714
+ if (cached !== void 0) return cached !== null ? fromStored(cached) : null;
715
+ const stored = await findOneStoredFromDb(filter, options);
716
+ if (stored) await cacheSet(stored, options);
717
+ else await cacheDelete(id, options);
718
+ return stored ? fromStored(stored) : null;
719
+ }
720
+ const stored = await findOneStoredFromDb(filter ?? {}, options);
721
+ return stored ? fromStored(stored) : null;
722
+ },
723
+ findOneAndDelete: async (filter, options) => {
724
+ await ensureCollectionCreated(options);
725
+ const existingDoc = await collection.findOne(filter, options);
726
+ if (existingDoc === null) return null;
727
+ await collection.deleteOne(filter, options);
728
+ return existingDoc;
729
+ },
730
+ findOneAndReplace: async (filter, replacement, options) => {
731
+ await ensureCollectionCreated(options);
732
+ const existingDoc = await collection.findOne(filter, options);
733
+ if (existingDoc === null) return null;
734
+ await collection.replaceOne(filter, replacement, options);
735
+ return existingDoc;
736
+ },
737
+ findOneAndUpdate: async (filter, update, options) => {
738
+ await ensureCollectionCreated(options);
739
+ const existingDoc = await collection.findOne(filter, options);
740
+ if (existingDoc === null) return null;
741
+ await collection.updateOne(filter, update, options);
742
+ return existingDoc;
743
+ },
744
+ replaceMany: async (documents, options) => {
745
+ await ensureCollectionCreated(options);
746
+ const rows = documents.map((d) => toStored(d));
747
+ if (options?.upsert) {
748
+ const versioned = documents.map((d) => "_version" in d && d._version !== void 0);
749
+ const hasVersions = versioned.some((v) => v);
750
+ const allVersioned = versioned.every((v) => v);
751
+ if (hasVersions && !allVersioned) throw new Error("replaceMany with upsert cannot mix documents with and without _version in a single batch");
752
+ if (!hasVersions) {
753
+ const { writtenIds, versions } = await executeUpsert(rows, options);
754
+ const conflictIds = documents.map((d) => d._id).filter((id) => !writtenIds.includes(id));
755
+ return operationResult({
756
+ successful: conflictIds.length === 0,
757
+ modifiedCount: writtenIds.length,
758
+ matchedCount: documents.length,
759
+ modifiedIds: [...writtenIds],
760
+ conflictIds: [...conflictIds],
761
+ nextExpectedVersions: versions
762
+ }, {
763
+ operationName: "replaceMany",
764
+ collectionName,
765
+ serializer,
766
+ errors
767
+ });
768
+ }
769
+ }
770
+ const result = await command(SqlFor.replaceMany(rows), options);
771
+ const modifiedIds = result.rows.map((row) => row._id);
772
+ const conflictIds = documents.map((d) => d._id).filter((id) => !modifiedIds.includes(id));
773
+ const versions = new Map(result.rows.map((row) => [row._id, BigInt(row.version ?? 1n)]));
774
+ if (!options?.skipCache) {
775
+ const cacheEntries = rows.filter((r) => modifiedIds.includes(r._id)).map((r) => ({
776
+ ...r,
777
+ _version: versions.get(r._id) ?? r._version ?? 1n
778
+ }));
779
+ if (cacheEntries.length > 0) await cacheSetMany(cacheEntries, options);
780
+ if (conflictIds.length > 0) await cacheDeleteMany([...conflictIds], options);
781
+ }
782
+ return operationResult({
783
+ successful: modifiedIds.length > 0 && conflictIds.length === 0,
784
+ modifiedCount: modifiedIds.length,
785
+ matchedCount: documents.length,
786
+ modifiedIds: [...modifiedIds],
787
+ conflictIds: [...conflictIds],
788
+ nextExpectedVersions: versions
789
+ }, {
790
+ operationName: "replaceMany",
791
+ collectionName,
792
+ serializer,
793
+ errors
794
+ });
795
+ },
796
+ handle: DocumentCommandHandler({
797
+ collectionName,
798
+ serializer,
799
+ errors,
800
+ storage: {
801
+ ensureCollectionCreated,
802
+ fetchByIds: (ids, options) => fetchByIds(ids, options).then((rows) => rows.map((stored) => stored ? fromStored(stored) : null)),
803
+ insertMany: (docs, options) => collection.insertMany(docs, options),
804
+ replaceMany: (docs, options) => collection.replaceMany(docs, options),
805
+ deleteManyByIds
806
+ }
807
+ }),
808
+ find: async (filter, options) => {
809
+ await ensureCollectionCreated(options);
810
+ if (!options?.skipCache && filter) {
811
+ const ids = getIdsFromIdOnlyFilter(filter);
812
+ if (ids && ids.length > 0) return findManyByIds(ids, options);
813
+ }
814
+ return (await query(SqlFor.find(filter ?? {}, options))).rows.map((row) => fromStored({
815
+ ...row.data,
816
+ _id: row._id,
817
+ _version: row._version
818
+ }));
819
+ },
820
+ countDocuments: async (filter, options) => {
821
+ await ensureCollectionCreated(options);
822
+ const { count } = await (0, _event_driven_io_dumbo.single)(query(SqlFor.countDocuments(filter ?? {})));
823
+ return count;
824
+ },
825
+ drop: async (options) => {
826
+ await ensureCollectionCreated(options);
827
+ return ((await command(SqlFor.drop()))?.rowCount ?? 0) > 0;
828
+ },
829
+ rename: async (newName, options) => {
830
+ await ensureCollectionCreated(options);
831
+ await command(SqlFor.rename(newName));
832
+ collectionName = newName;
833
+ return collection;
834
+ },
835
+ close: () => cache.close(),
836
+ sql: {
837
+ async query(sql, options) {
838
+ await ensureCollectionCreated(options);
839
+ return (await query(sql, options)).rows;
840
+ },
841
+ async command(sql, options) {
842
+ await ensureCollectionCreated(options);
843
+ return command(sql, options);
844
+ }
845
+ },
846
+ schema: {
847
+ component: schemaComponent,
848
+ migrate: (options) => (0, _event_driven_io_dumbo.runSQLMigrations)(pool, schemaComponent.migrations, options)
849
+ }
850
+ };
851
+ return collection;
852
+ };
853
+
854
+ //#endregion
855
+ //#region src/core/collection/pongoCollectionSchemaComponent.ts
856
+ const PongoCollectionSchemaComponent = ({ definition, migrationsOrSchemaComponents, sqlBuilder }) => ({
857
+ ...(0, _event_driven_io_dumbo.schemaComponent)(`sc:pongo:collection:${definition.name}`, migrationsOrSchemaComponents),
858
+ sqlBuilder,
859
+ definition,
860
+ collectionName: definition.name
861
+ });
862
+
863
+ //#endregion
864
+ //#region src/core/collection/query.ts
865
+ const QueryOperators = {
866
+ $eq: "$eq",
867
+ $gt: "$gt",
868
+ $gte: "$gte",
869
+ $lt: "$lt",
870
+ $lte: "$lte",
871
+ $ne: "$ne",
872
+ $in: "$in",
873
+ $nin: "$nin",
874
+ $elemMatch: "$elemMatch",
875
+ $all: "$all",
876
+ $size: "$size"
877
+ };
878
+ const OperatorMap = {
879
+ $gt: ">",
880
+ $gte: ">=",
881
+ $lt: "<",
882
+ $lte: "<=",
883
+ $ne: "!="
884
+ };
885
+ const isOperator = (key) => key.startsWith("$");
886
+ const hasOperators = (value) => Object.keys(value).some(isOperator);
887
+
888
+ //#endregion
889
+ //#region src/core/typing/entries.ts
890
+ const objectEntries = (obj) => Object.entries(obj).map(([key, value]) => [key, value]);
891
+
892
+ //#endregion
893
+ //#region src/core/errors/index.ts
894
+ const isNumber = (val) => typeof val === "number" && val === val;
895
+ const isString = (val) => typeof val === "string";
896
+ var PongoError = class PongoError extends Error {
897
+ errorCode;
898
+ constructor(options) {
899
+ const errorCode = options && typeof options === "object" && "errorCode" in options ? options.errorCode : isNumber(options) ? options : 500;
900
+ const message = options && typeof options === "object" && "message" in options ? options.message : isString(options) ? options : `Error with status code '${errorCode}' ocurred during Pongo processing`;
901
+ super(message);
902
+ this.errorCode = errorCode;
903
+ Object.setPrototypeOf(this, PongoError.prototype);
904
+ }
905
+ };
906
+ var ConcurrencyError = class ConcurrencyError extends PongoError {
907
+ constructor(message) {
908
+ super({
909
+ errorCode: 412,
910
+ message: message ?? `Expected document state does not match current one!`
911
+ });
912
+ Object.setPrototypeOf(this, ConcurrencyError.prototype);
913
+ }
914
+ };
915
+
916
+ //#endregion
917
+ //#region src/core/typing/operations.ts
918
+ const DOCUMENT_DOES_NOT_EXIST = "DOCUMENT_DOES_NOT_EXIST";
919
+ const NO_CONCURRENCY_CHECK = "NO_CONCURRENCY_CHECK";
920
+ const isGeneralExpectedDocumentVersion = (version) => version === "DOCUMENT_DOES_NOT_EXIST" || version === "DOCUMENT_EXISTS" || version === "NO_CONCURRENCY_CHECK";
921
+ const expectedVersionValue = (version) => version === void 0 || isGeneralExpectedDocumentVersion(version) ? null : version;
922
+ const expectedVersionPredicate = (version) => {
923
+ if (version === void 0) return { operator: "none" };
924
+ if (isGeneralExpectedDocumentVersion(version)) return version === "DOCUMENT_DOES_NOT_EXIST" ? {
925
+ operator: "=",
926
+ value: 0n
927
+ } : { operator: "none" };
928
+ return {
929
+ operator: "=",
930
+ value: version
931
+ };
932
+ };
933
+ const operationResult = (result, options) => {
934
+ const operationResult = {
935
+ ...result,
936
+ acknowledged: true,
937
+ successful: result.successful,
938
+ assertSuccessful: (errorMessage) => {
939
+ const { successful } = result;
940
+ const { operationName, collectionName } = options;
941
+ if (!successful) throw new ConcurrencyError(errorMessage ?? `${operationName} on ${collectionName} failed. Expected document state does not match current one! Result: ${options.serializer.serialize(result)}!`);
942
+ }
943
+ };
944
+ if (options.errors?.throwOnOperationFailures) operationResult.assertSuccessful();
945
+ return operationResult;
946
+ };
947
+
948
+ //#endregion
949
+ //#region src/core/schema/index.ts
950
+ const pongoCollectionSchema = (name) => ({ name });
951
+ pongoCollectionSchema.from = (collectionNames) => collectionNames.reduce((acc, collectionName) => (acc[collectionName] = pongoSchema.collection(collectionName), acc), {});
952
+ function pongoDbSchema(nameOrCollections, collections) {
953
+ if (collections === void 0) {
954
+ if (typeof nameOrCollections === "string") throw new Error("You need to provide colleciton definition");
955
+ return { collections: nameOrCollections };
956
+ }
957
+ return nameOrCollections && typeof nameOrCollections === "string" ? {
958
+ name: nameOrCollections,
959
+ collections
960
+ } : { collections };
961
+ }
962
+ pongoDbSchema.from = (databaseName, collectionNames) => databaseName ? pongoDbSchema(databaseName, pongoCollectionSchema.from(collectionNames)) : pongoDbSchema(pongoCollectionSchema.from(collectionNames));
963
+ const pongoClientSchema = (dbs) => ({ dbs });
964
+ const pongoSchema = {
965
+ client: pongoClientSchema,
966
+ db: pongoDbSchema,
967
+ collection: pongoCollectionSchema
968
+ };
969
+ const proxyPongoDbWithSchema = (pongoDb, dbSchema, collections) => {
970
+ const collectionNames = Object.keys(dbSchema.collections);
971
+ for (const collectionName of collectionNames) collections.set(collectionName, pongoDb.collection(collectionName));
972
+ return new Proxy(pongoDb, { get(target, prop) {
973
+ return collections.get(prop) ?? target[prop];
974
+ } });
975
+ };
976
+
977
+ //#endregion
978
+ //#region src/core/database/pongoDatabaseSchemaComponent.ts
979
+ const PongoDatabaseSchemaComponent = ({ definition, collectionFactory }) => {
980
+ const collections = Object.values(definition.collections).map(collectionFactory) ?? [];
981
+ return {
982
+ ...(0, _event_driven_io_dumbo.schemaComponent)(`sc:dumbo:database:${definition.name}`, { components: collections }),
983
+ definition,
984
+ collections,
985
+ collection: (schema) => {
986
+ const existing = collections.find((c) => c.collectionName === schema.name);
987
+ if (existing) return existing;
988
+ const newCollection = collectionFactory(pongoSchema.collection(schema.name));
989
+ collections.push(newCollection);
990
+ definition.collections[schema.name] = schema;
991
+ return newCollection;
992
+ }
993
+ };
994
+ };
995
+
996
+ //#endregion
997
+ //#region src/core/database/pongoDb.ts
998
+ const PongoDatabase = (options) => {
999
+ const { databaseName, schemaComponent, pool, cache: cacheOptions, serializer } = options;
1000
+ const cache = cacheOptions === "disabled" || cacheOptions === void 0 ? "disabled" : pongoCache(cacheOptions);
1001
+ const collections = /* @__PURE__ */ new Map();
1002
+ const command = async (sql, options) => (await transactionExecutorOrDefault(db, options, pool.execute)).command(sql, options);
1003
+ const query = async (sql, options) => (await transactionExecutorOrDefault(db, options, pool.execute)).query(sql, options);
1004
+ const db = {
1005
+ driverType: pool.driverType,
1006
+ databaseName,
1007
+ connect: () => Promise.resolve(),
1008
+ close: async () => {
1009
+ await Promise.allSettled([
1010
+ pool.close(),
1011
+ cache !== "disabled" ? cache.close() : Promise.resolve(),
1012
+ ...collections.values().map((collection) => collection.close())
1013
+ ]);
1014
+ },
1015
+ collections: () => [...collections.values()],
1016
+ collection: (collectionName, collectionOptions) => collections.get(collectionName) ?? pongoCollection({
1017
+ collectionName,
1018
+ db,
1019
+ pool,
1020
+ schemaComponent: schemaComponent.collection(pongoSchema.collection(collectionName)),
1021
+ schema: {
1022
+ ...options.schema,
1023
+ ...collectionOptions?.schema
1024
+ },
1025
+ serializer,
1026
+ errors: {
1027
+ ...options.errors,
1028
+ ...collectionOptions?.errors
1029
+ },
1030
+ cache: collectionOptions?.cache !== void 0 ? collectionOptions.cache : cache
1031
+ }),
1032
+ transaction: () => pool.transaction(),
1033
+ withTransaction: (handle) => pool.withTransaction(handle),
1034
+ schema: {
1035
+ component: schemaComponent,
1036
+ migrate: (options) => (0, _event_driven_io_dumbo.runSQLMigrations)(pool, schemaComponent.migrations, options)
1037
+ },
1038
+ sql: {
1039
+ async query(sql, options) {
1040
+ return (await query(sql, options)).rows;
1041
+ },
1042
+ async command(sql, options) {
1043
+ return command(sql, options);
1044
+ }
1045
+ }
1046
+ };
1047
+ const dbSchema = options?.schema?.definition;
1048
+ if (dbSchema) return proxyPongoDbWithSchema(db, dbSchema, collections);
1049
+ return db;
1050
+ };
1051
+
1052
+ //#endregion
1053
+ //#region src/core/drivers/databaseDriver.ts
1054
+ const PongoDriverRegistry = () => {
1055
+ const drivers = /* @__PURE__ */ new Map();
1056
+ const register = (driverType, driver) => {
1057
+ const entry = drivers.get(driverType);
1058
+ if (entry && (typeof entry !== "function" || typeof driver === "function")) return;
1059
+ drivers.set(driverType, driver);
1060
+ };
1061
+ const tryResolve = async (driverType) => {
1062
+ const entry = drivers.get(driverType);
1063
+ if (!entry) return null;
1064
+ if (typeof entry !== "function") return entry;
1065
+ const driver = await entry();
1066
+ register(driverType, driver);
1067
+ return driver;
1068
+ };
1069
+ const tryGet = (driverType) => {
1070
+ const entry = drivers.get(driverType);
1071
+ return entry && typeof entry !== "function" ? entry : null;
1072
+ };
1073
+ const has = (driverType) => drivers.has(driverType);
1074
+ return {
1075
+ register,
1076
+ tryResolve,
1077
+ tryGet,
1078
+ has,
1079
+ get databaseDriverTypes() {
1080
+ return Array.from(drivers.keys());
1081
+ }
1082
+ };
1083
+ };
1084
+ const pongoDriverRegistry = globalThis.pongoDriverRegistry = globalThis.pongoDriverRegistry ?? PongoDriverRegistry();
1085
+
1086
+ //#endregion
1087
+ //#region src/core/utils/deepEquals.ts
1088
+ const deepEquals = (left, right) => {
1089
+ if (isEquatable(left)) return left.equals(right);
1090
+ if (Array.isArray(left)) return Array.isArray(right) && left.length === right.length && left.every((val, index) => deepEquals(val, right[index]));
1091
+ if (typeof left !== "object" || typeof right !== "object" || left === null || right === null) return left === right;
1092
+ if (Array.isArray(right)) return false;
1093
+ const keys1 = Object.keys(left);
1094
+ const keys2 = Object.keys(right);
1095
+ if (keys1.length !== keys2.length || !keys1.every((key) => keys2.includes(key))) return false;
1096
+ for (const key in left) {
1097
+ if (left[key] instanceof Function && right[key] instanceof Function) continue;
1098
+ if (!deepEquals(left[key], right[key])) return false;
1099
+ }
1100
+ return true;
1101
+ };
1102
+ const isEquatable = (left) => {
1103
+ return left && typeof left === "object" && "equals" in left && typeof left["equals"] === "function";
1104
+ };
1105
+
1106
+ //#endregion
1107
+ //#region src/core/utils/mapAsync.ts
1108
+ async function mapSequential(items, fn) {
1109
+ const results = [];
1110
+ for (let i = 0; i < items.length; i++) results.push(await fn(items[i], i));
1111
+ return results;
1112
+ }
1113
+ const mapParallel = (items, fn) => Promise.all(items.map(fn));
1114
+ const mapAsync = (items, fn, options = { parallel: false }) => options?.parallel ? mapParallel(items, fn) : mapSequential(items, fn);
1115
+
1116
+ //#endregion
1117
+ //#region src/storage/sqlite/core/sqlBuilder/filter/queryOperators.ts
1118
+ const handleOperator = (path, operator, value, serializer) => {
1119
+ if (path === "_id" || path === "_version") return handleMetadataOperator(path, operator, value);
1120
+ switch (operator) {
1121
+ case "$eq": {
1122
+ const jsonPath = _event_driven_io_dumbo_sqlite.SQLiteJSON.path(path);
1123
+ return _event_driven_io_dumbo.SQL`(
1124
+ json_extract(data, ${jsonPath}) = ${value}
1125
+ OR (
1126
+ json_type(data, ${jsonPath}) = 'array'
1127
+ AND EXISTS(
1128
+ SELECT 1 FROM json_each(data, ${jsonPath})
1129
+ WHERE json_each.value = ${value}
1130
+ )
1131
+ )
1132
+ )`;
1133
+ }
1134
+ case "$gt":
1135
+ case "$gte":
1136
+ case "$lt":
1137
+ case "$lte":
1138
+ case "$ne": return _event_driven_io_dumbo.SQL`json_extract(data, ${_event_driven_io_dumbo_sqlite.SQLiteJSON.path(path)}) ${_event_driven_io_dumbo.SQL.plain(OperatorMap[operator])} ${value}`;
1139
+ case "$in": {
1140
+ const jsonPath = _event_driven_io_dumbo_sqlite.SQLiteJSON.path(path);
1141
+ const values = value;
1142
+ return _event_driven_io_dumbo.SQL`json_extract(data, ${jsonPath}) IN (${_event_driven_io_dumbo.SQL.merge(values.map((v) => _event_driven_io_dumbo.SQL`${v}`), ", ")})`;
1143
+ }
1144
+ case "$nin": {
1145
+ const jsonPath = _event_driven_io_dumbo_sqlite.SQLiteJSON.path(path);
1146
+ const values = value;
1147
+ return _event_driven_io_dumbo.SQL`json_extract(data, ${jsonPath}) NOT IN (${_event_driven_io_dumbo.SQL.merge(values.map((v) => _event_driven_io_dumbo.SQL`${v}`), ", ")})`;
1148
+ }
1149
+ case "$elemMatch": {
1150
+ const subConditions = objectEntries(value).map(([subKey, subValue]) => _event_driven_io_dumbo.SQL`json_extract(value, ${_event_driven_io_dumbo_sqlite.SQLiteJSON.path(subKey)}) = json(${_event_driven_io_dumbo.JSONParam.value(subValue, serializer)})`);
1151
+ return _event_driven_io_dumbo.SQL`EXISTS(SELECT 1 FROM json_each(data, ${_event_driven_io_dumbo_sqlite.SQLiteJSON.path(path)}) WHERE ${_event_driven_io_dumbo.SQL.merge(subConditions, " AND ")})`;
1152
+ }
1153
+ case "$all": {
1154
+ const jsonPath = _event_driven_io_dumbo_sqlite.SQLiteJSON.path(path);
1155
+ return _event_driven_io_dumbo.SQL`(SELECT COUNT(*) FROM json_each(json(${_event_driven_io_dumbo.JSONParam.value(value, serializer)})) WHERE json_each.value NOT IN (SELECT value FROM json_each(data, ${jsonPath}))) = 0`;
1156
+ }
1157
+ case "$size": return _event_driven_io_dumbo.SQL`json_array_length(json_extract(data, ${_event_driven_io_dumbo_sqlite.SQLiteJSON.path(path)})) = ${value}`;
1158
+ default: throw new Error(`Unsupported operator: ${operator}`);
1159
+ }
1160
+ };
1161
+ const handleMetadataOperator = (fieldName, operator, value) => {
1162
+ switch (operator) {
1163
+ case "$eq": return _event_driven_io_dumbo.SQL`${_event_driven_io_dumbo.SQL.plain(fieldName)} = ${value}`;
1164
+ case "$gt":
1165
+ case "$gte":
1166
+ case "$lt":
1167
+ case "$lte":
1168
+ case "$ne": return _event_driven_io_dumbo.SQL`${_event_driven_io_dumbo.SQL.plain(fieldName)} ${_event_driven_io_dumbo.SQL.plain(OperatorMap[operator])} ${value}`;
1169
+ case "$in": {
1170
+ const values = value;
1171
+ const inClause = _event_driven_io_dumbo.SQL.merge(values.map((v) => _event_driven_io_dumbo.SQL`${v}`), ", ");
1172
+ return _event_driven_io_dumbo.SQL`${_event_driven_io_dumbo.SQL.plain(fieldName)} IN (${inClause})`;
1173
+ }
1174
+ case "$nin": {
1175
+ const values = value;
1176
+ const inClause = _event_driven_io_dumbo.SQL.merge(values.map((v) => _event_driven_io_dumbo.SQL`${v}`), ", ");
1177
+ return _event_driven_io_dumbo.SQL`${_event_driven_io_dumbo.SQL.plain(fieldName)} NOT IN (${inClause})`;
1178
+ }
1179
+ default: throw new Error(`Unsupported operator: ${operator}`);
1180
+ }
1181
+ };
1182
+
1183
+ //#endregion
1184
+ //#region src/storage/sqlite/core/sqlBuilder/filter/index.ts
1185
+ const AND = "AND";
1186
+ const OR = "OR";
1187
+ const unsupportedRootOperators = [
1188
+ "$text",
1189
+ "$where",
1190
+ "$comment"
1191
+ ];
1192
+ const constructFilterQuery = (filter, serializer) => {
1193
+ ensureSupportedRootOperators(filter);
1194
+ const parts = [];
1195
+ const fieldFilterQuery = constructFieldFilterQuery(filter, serializer);
1196
+ if (!_event_driven_io_dumbo.SQL.check.isEmpty(fieldFilterQuery)) parts.push(fieldFilterQuery);
1197
+ const orFilterQuery = constructLogicalFilterQuery(filter.$or, OR, serializer);
1198
+ if (!_event_driven_io_dumbo.SQL.check.isEmpty(orFilterQuery)) parts.push(orFilterQuery);
1199
+ const andFilterQuery = constructLogicalFilterQuery(filter.$and, AND, serializer);
1200
+ if (!_event_driven_io_dumbo.SQL.check.isEmpty(andFilterQuery)) parts.push(andFilterQuery);
1201
+ const norFilterQuery = constructNorFilterQuery(filter.$nor, serializer);
1202
+ if (!_event_driven_io_dumbo.SQL.check.isEmpty(norFilterQuery)) parts.push(norFilterQuery);
1203
+ return _event_driven_io_dumbo.SQL.merge(parts, ` ${AND} `);
1204
+ };
1205
+ const constructFieldFilterQuery = (filter, serializer) => _event_driven_io_dumbo.SQL.merge(objectEntries(filter).flatMap(([key, value]) => isLogicalRootOperator(key) ? [] : [isRecord(value) ? constructComplexFilterQuery(key, value, serializer) : handleOperator(key, QueryOperators.$eq, value, serializer)]), ` ${AND} `);
1206
+ const constructLogicalFilterQuery = (filters, joinOperator, serializer) => {
1207
+ if (!filters?.length) return _event_driven_io_dumbo.SQL.EMPTY;
1208
+ const subFilterQueries = filters.reduce((queries, filter) => {
1209
+ const query = constructFilterQuery(filter, serializer);
1210
+ if (!_event_driven_io_dumbo.SQL.check.isEmpty(query)) queries.push(query);
1211
+ return queries;
1212
+ }, []);
1213
+ if (subFilterQueries.length === 0) return _event_driven_io_dumbo.SQL.EMPTY;
1214
+ if (subFilterQueries.length === 1) return wrapFilterQuery(subFilterQueries[0]);
1215
+ return _event_driven_io_dumbo.SQL`(${_event_driven_io_dumbo.SQL.merge(subFilterQueries.map(wrapFilterQuery), ` ${joinOperator} `)})`;
1216
+ };
1217
+ const constructNorFilterQuery = (filters, serializer) => {
1218
+ if (!filters?.length) return _event_driven_io_dumbo.SQL.EMPTY;
1219
+ const logicalFilterQuery = constructLogicalFilterQuery(filters, OR, serializer);
1220
+ return _event_driven_io_dumbo.SQL.check.isEmpty(logicalFilterQuery) ? _event_driven_io_dumbo.SQL.EMPTY : _event_driven_io_dumbo.SQL`NOT ${logicalFilterQuery}`;
1221
+ };
1222
+ const constructComplexFilterQuery = (key, value, serializer) => {
1223
+ const isEquality = !hasOperators(value);
1224
+ return _event_driven_io_dumbo.SQL.merge(objectEntries(value).map(([nestedKey, val]) => isEquality ? handleOperator(`${key}.${nestedKey}`, QueryOperators.$eq, val, serializer) : handleOperator(key, nestedKey, val, serializer)), ` ${AND} `);
1225
+ };
1226
+ const wrapFilterQuery = (filterQuery) => _event_driven_io_dumbo.SQL`(${filterQuery})`;
1227
+ const ensureSupportedRootOperators = (filter) => {
1228
+ for (const operator of unsupportedRootOperators) if (operator in filter) throw new Error(`Unsupported root operator: ${operator}`);
1229
+ };
1230
+ const isLogicalRootOperator = (key) => key === "$and" || key === "$nor" || key === "$or";
1231
+ const isRecord = (value) => value !== null && typeof value === "object" && !Array.isArray(value);
1232
+
1233
+ //#endregion
1234
+ //#region src/storage/sqlite/core/sqlBuilder/update/index.ts
1235
+ const buildUpdateQuery = (update, serializer) => objectEntries(update).reduce((currentUpdateQuery, [op, value]) => {
1236
+ switch (op) {
1237
+ case "$set": return buildSetQuery(value, currentUpdateQuery, serializer);
1238
+ case "$unset": return buildUnsetQuery(value, currentUpdateQuery);
1239
+ case "$inc": return buildIncQuery(value, currentUpdateQuery);
1240
+ case "$push": return buildPushQuery(value, currentUpdateQuery, serializer);
1241
+ default: return currentUpdateQuery;
1242
+ }
1243
+ }, _event_driven_io_dumbo.SQL`data`);
1244
+ const buildSetQuery = (set, currentUpdateQuery, serializer) => _event_driven_io_dumbo.SQL`json_patch(${currentUpdateQuery}, ${_event_driven_io_dumbo.JSONParam.value(set, serializer)})`;
1245
+ const buildUnsetQuery = (unset, currentUpdateQuery) => {
1246
+ const keys = Object.keys(unset);
1247
+ let query = currentUpdateQuery;
1248
+ for (const key of keys) query = _event_driven_io_dumbo.SQL`json_remove(${query}, ${_event_driven_io_dumbo_sqlite.SQLiteJSON.path(key)})`;
1249
+ return query;
1250
+ };
1251
+ const buildIncQuery = (inc, currentUpdateQuery) => {
1252
+ for (const [key, value] of Object.entries(inc)) currentUpdateQuery = typeof value === "bigint" ? _event_driven_io_dumbo.SQL`json_set(${currentUpdateQuery}, ${_event_driven_io_dumbo_sqlite.SQLiteJSON.path(key)}, CAST((COALESCE(json_extract(${currentUpdateQuery}, ${_event_driven_io_dumbo_sqlite.SQLiteJSON.path(key)}), 0) + ${value}) AS TEXT))` : _event_driven_io_dumbo.SQL`json_set(${currentUpdateQuery}, ${_event_driven_io_dumbo_sqlite.SQLiteJSON.path(key)}, COALESCE(json_extract(${currentUpdateQuery}, ${_event_driven_io_dumbo_sqlite.SQLiteJSON.path(key)}), 0) + ${value})`;
1253
+ return currentUpdateQuery;
1254
+ };
1255
+ const buildPushQuery = (push, currentUpdateQuery, serializer) => {
1256
+ for (const [key, value] of Object.entries(push)) {
1257
+ const serializedValue = _event_driven_io_dumbo.JSONParam.value(value, serializer);
1258
+ currentUpdateQuery = _event_driven_io_dumbo.SQL`json_set(${currentUpdateQuery}, ${_event_driven_io_dumbo_sqlite.SQLiteJSON.path(key)}, CASE
1259
+ WHEN json_type(json_extract(${currentUpdateQuery}, ${_event_driven_io_dumbo_sqlite.SQLiteJSON.path(key)})) = 'array'
1260
+ THEN json_insert(json_extract(${currentUpdateQuery}, ${_event_driven_io_dumbo_sqlite.SQLiteJSON.path(key)}), '$[#]', json(${serializedValue}))
1261
+ ELSE json(${_event_driven_io_dumbo.JSONParam.arrayContaining(value, serializer)})
1262
+ END)`;
1263
+ }
1264
+ return currentUpdateQuery;
1265
+ };
1266
+
1267
+ //#endregion
1268
+ //#region src/storage/sqlite/core/sqlBuilder/index.ts
1269
+ const versionCheckClause = (expectedVersion) => {
1270
+ const predicate = expectedVersionPredicate(expectedVersion);
1271
+ return predicate.operator === "none" ? _event_driven_io_dumbo.SQL.EMPTY : _event_driven_io_dumbo.SQL`AND _version = ${predicate.value}`;
1272
+ };
1273
+ const createCollection = (collectionName) => _event_driven_io_dumbo.SQL`
1274
+ CREATE TABLE IF NOT EXISTS ${_event_driven_io_dumbo.SQL.identifier(collectionName)} (
1275
+ _id TEXT PRIMARY KEY,
1276
+ data JSON NOT NULL,
1277
+ metadata JSON NOT NULL DEFAULT '{}',
1278
+ _version INTEGER NOT NULL DEFAULT 1,
1279
+ _partition TEXT NOT NULL DEFAULT 'png_global',
1280
+ _archived INTEGER NOT NULL DEFAULT 0,
1281
+ _created TEXT NOT NULL DEFAULT (datetime('now')),
1282
+ _updated TEXT NOT NULL DEFAULT (datetime('now'))
1283
+ )`;
1284
+ const pongoCollectionSQLiteMigrations = (collectionName) => [(0, _event_driven_io_dumbo.sqlMigration)(`pongoCollection:${collectionName}:001:createtable`, [createCollection(collectionName)])];
1285
+ const sqliteSQLBuilder = (collectionName, serializer) => ({
1286
+ createCollection: () => createCollection(collectionName),
1287
+ insertOne: (document) => {
1288
+ const serialized = _event_driven_io_dumbo.JSONParam.document(document, serializer);
1289
+ const id = document._id;
1290
+ const version = document._version ?? 1n;
1291
+ return _event_driven_io_dumbo.SQL`
1292
+ INSERT OR IGNORE INTO ${_event_driven_io_dumbo.SQL.identifier(collectionName)} (_id, data, _version)
1293
+ VALUES (${id}, ${serialized}, ${version})
1294
+ RETURNING _id;`;
1295
+ },
1296
+ insertMany: (documents) => {
1297
+ const values = _event_driven_io_dumbo.SQL.merge(documents.map((doc) => _event_driven_io_dumbo.SQL`(${doc._id}, ${_event_driven_io_dumbo.JSONParam.document(doc, serializer)}, ${doc._version ?? 1n})`), ",");
1298
+ return _event_driven_io_dumbo.SQL`
1299
+ INSERT OR IGNORE INTO ${_event_driven_io_dumbo.SQL.identifier(collectionName)} (_id, data, _version) VALUES ${values}
1300
+ RETURNING _id;`;
1301
+ },
1302
+ insertOrReplace: (documents) => {
1303
+ const col = _event_driven_io_dumbo.SQL.identifier(collectionName);
1304
+ return _event_driven_io_dumbo.SQL`
1305
+ INSERT INTO ${col} (_id, data, _version)
1306
+ VALUES ${_event_driven_io_dumbo.SQL.merge(documents.map((d) => _event_driven_io_dumbo.SQL`(${d._id}, json_patch(${_event_driven_io_dumbo.JSONParam.document(d, serializer)}, json_object('_id', ${d._id}, '_version', '1')), 1)`), ",")}
1307
+ ON CONFLICT(_id) DO UPDATE SET
1308
+ data = json_patch(excluded.data, json_object('_id', ${col}._id, '_version', cast(${col}._version + 1 as TEXT))),
1309
+ _version = ${col}._version + 1
1310
+ RETURNING _id, cast(_version as TEXT) as version;`;
1311
+ },
1312
+ updateOne: (filter, update, options) => {
1313
+ const expectedVersionCheck = versionCheckClause(options?.expectedVersion);
1314
+ const filterQuery = (0, _event_driven_io_dumbo.isSQL)(filter) ? filter : constructFilterQuery(filter, serializer);
1315
+ const updateQuery = (0, _event_driven_io_dumbo.isSQL)(update) ? update : buildUpdateQuery(update, serializer);
1316
+ return _event_driven_io_dumbo.SQL`
1317
+ UPDATE ${_event_driven_io_dumbo.SQL.identifier(collectionName)}
1318
+ SET
1319
+ data = json_patch(${updateQuery}, json_object('_id', _id, '_version', cast(_version + 1 as TEXT))),
1320
+ _version = _version + 1,
1321
+ _updated = datetime('now')
1322
+ WHERE _id = (
1323
+ SELECT _id FROM ${_event_driven_io_dumbo.SQL.identifier(collectionName)}
1324
+ ${where(filterQuery)}
1325
+ LIMIT 1
1326
+ ) ${expectedVersionCheck}
1327
+ RETURNING
1328
+ _id,
1329
+ cast(_version as TEXT) as version,
1330
+ 1 as matched,
1331
+ 1 as modified;`;
1332
+ },
1333
+ replaceOne: (filter, document, options) => {
1334
+ const expectedVersionCheck = versionCheckClause(options?.expectedVersion);
1335
+ const filterQuery = (0, _event_driven_io_dumbo.isSQL)(filter) ? filter : constructFilterQuery(filter, serializer);
1336
+ return _event_driven_io_dumbo.SQL`
1337
+ UPDATE ${_event_driven_io_dumbo.SQL.identifier(collectionName)}
1338
+ SET
1339
+ data = json_patch(${_event_driven_io_dumbo.JSONParam.document(document, serializer)}, json_object('_id', _id, '_version', cast(_version + 1 as TEXT))),
1340
+ _version = _version + 1,
1341
+ _updated = datetime('now')
1342
+ WHERE _id = (
1343
+ SELECT _id FROM ${_event_driven_io_dumbo.SQL.identifier(collectionName)}
1344
+ ${where(filterQuery)}
1345
+ LIMIT 1
1346
+ ) ${expectedVersionCheck}
1347
+ RETURNING
1348
+ _id,
1349
+ cast(_version as TEXT) AS version,
1350
+ 1 AS matched,
1351
+ 1 AS modified;`;
1352
+ },
1353
+ updateMany: (filter, update) => {
1354
+ const filterQuery = (0, _event_driven_io_dumbo.isSQL)(filter) ? filter : constructFilterQuery(filter, serializer);
1355
+ const updateQuery = (0, _event_driven_io_dumbo.isSQL)(update) ? update : buildUpdateQuery(update, serializer);
1356
+ return _event_driven_io_dumbo.SQL`
1357
+ UPDATE ${_event_driven_io_dumbo.SQL.identifier(collectionName)}
1358
+ SET
1359
+ data = json_patch(${updateQuery}, json_object('_version', cast(_version + 1 as TEXT))),
1360
+ _version = _version + 1,
1361
+ _updated = datetime('now')
1362
+ ${where(filterQuery)}
1363
+ RETURNING _id;`;
1364
+ },
1365
+ deleteOne: (filter, options) => {
1366
+ const expectedVersionCheck = versionCheckClause(options?.expectedVersion);
1367
+ const filterQuery = (0, _event_driven_io_dumbo.isSQL)(filter) ? filter : constructFilterQuery(filter, serializer);
1368
+ return _event_driven_io_dumbo.SQL`
1369
+ DELETE FROM ${_event_driven_io_dumbo.SQL.identifier(collectionName)}
1370
+ WHERE _id = (
1371
+ SELECT _id FROM ${_event_driven_io_dumbo.SQL.identifier(collectionName)}
1372
+ ${where(filterQuery)}
1373
+ LIMIT 1
1374
+ ) ${expectedVersionCheck}
1375
+ RETURNING
1376
+ _id,
1377
+ 1 AS matched,
1378
+ 1 AS deleted;`;
1379
+ },
1380
+ deleteMany: (filter) => {
1381
+ const filterQuery = (0, _event_driven_io_dumbo.isSQL)(filter) ? filter : constructFilterQuery(filter, serializer);
1382
+ return _event_driven_io_dumbo.SQL`DELETE FROM ${_event_driven_io_dumbo.SQL.identifier(collectionName)} ${where(filterQuery)} RETURNING _id`;
1383
+ },
1384
+ replaceMany: (documents) => {
1385
+ const col = _event_driven_io_dumbo.SQL.identifier(collectionName);
1386
+ if (documents.some((d) => "_version" in d && d._version !== void 0)) return _event_driven_io_dumbo.SQL`
1387
+ WITH replacements(_id, data, expected_version) AS (
1388
+ VALUES ${_event_driven_io_dumbo.SQL.merge(documents.map((d) => {
1389
+ const expectedVersion = d._version;
1390
+ return expectedVersion !== void 0 ? _event_driven_io_dumbo.SQL`(${d._id}, ${_event_driven_io_dumbo.JSONParam.document(d, serializer)}, ${expectedVersion})` : _event_driven_io_dumbo.SQL`(${d._id}, ${_event_driven_io_dumbo.JSONParam.document(d, serializer)}, NULL)`;
1391
+ }), ",")}
1392
+ )
1393
+ UPDATE ${col}
1394
+ SET
1395
+ data = json_patch(r.data, json_object('_id', ${col}._id, '_version', cast(${col}._version + 1 as TEXT))),
1396
+ _version = ${col}._version + 1,
1397
+ _updated = datetime('now')
1398
+ FROM replacements r
1399
+ WHERE ${col}._id = r._id AND (r.expected_version IS NULL OR ${col}._version = r.expected_version)
1400
+ RETURNING ${col}._id, cast(${col}._version as TEXT) as version;`;
1401
+ return _event_driven_io_dumbo.SQL`
1402
+ WITH replacements(_id, data) AS (
1403
+ VALUES ${_event_driven_io_dumbo.SQL.merge(documents.map((d) => _event_driven_io_dumbo.SQL`(${d._id}, ${_event_driven_io_dumbo.JSONParam.document(d, serializer)})`), ",")}
1404
+ )
1405
+ UPDATE ${col}
1406
+ SET
1407
+ data = json_patch(r.data, json_object('_id', ${col}._id, '_version', cast(${col}._version + 1 as TEXT))),
1408
+ _version = ${col}._version + 1,
1409
+ _updated = datetime('now')
1410
+ FROM replacements r
1411
+ WHERE ${col}._id = r._id
1412
+ RETURNING ${col}._id, cast(${col}._version as TEXT) as version;`;
1413
+ },
1414
+ deleteManyByIds: (ids) => {
1415
+ if (ids.some((d) => d._version !== void 0)) return _event_driven_io_dumbo.SQL`
1416
+ WITH targets(_id, expected_version) AS (
1417
+ VALUES ${_event_driven_io_dumbo.SQL.merge(ids.map((d) => _event_driven_io_dumbo.SQL`(${d._id}, ${d._version ?? 0n})`), ",")}
1418
+ )
1419
+ DELETE FROM ${_event_driven_io_dumbo.SQL.identifier(collectionName)}
1420
+ WHERE _id IN (SELECT _id FROM targets)
1421
+ AND _version = (SELECT expected_version FROM targets WHERE targets._id = ${_event_driven_io_dumbo.SQL.identifier(collectionName)}._id)
1422
+ RETURNING _id;`;
1423
+ const idList = _event_driven_io_dumbo.SQL.merge(ids.map((d) => _event_driven_io_dumbo.SQL`${d._id}`), ",");
1424
+ return _event_driven_io_dumbo.SQL`
1425
+ DELETE FROM ${_event_driven_io_dumbo.SQL.identifier(collectionName)}
1426
+ WHERE _id IN (${idList})
1427
+ RETURNING _id;`;
1428
+ },
1429
+ findOne: (filter) => {
1430
+ const filterQuery = (0, _event_driven_io_dumbo.isSQL)(filter) ? filter : constructFilterQuery(filter, serializer);
1431
+ return _event_driven_io_dumbo.SQL`SELECT data, _id, _version FROM ${_event_driven_io_dumbo.SQL.identifier(collectionName)} ${where(filterQuery)} LIMIT 1;`;
1432
+ },
1433
+ find: (filter, options) => {
1434
+ const filterQuery = (0, _event_driven_io_dumbo.isSQL)(filter) ? filter : constructFilterQuery(filter, serializer);
1435
+ const query = [];
1436
+ query.push(_event_driven_io_dumbo.SQL`SELECT data, _id, _version FROM ${_event_driven_io_dumbo.SQL.identifier(collectionName)}`);
1437
+ query.push(where(filterQuery));
1438
+ if (options?.sort && Object.keys(options.sort).length > 0) {
1439
+ const clauses = Object.entries(options.sort).map(([field, dir]) => {
1440
+ const accessor = field === "_id" || field === "_version" ? _event_driven_io_dumbo.SQL`${_event_driven_io_dumbo.SQL.identifier(field)}` : _event_driven_io_dumbo.SQL`json_extract(data, ${_event_driven_io_dumbo_sqlite.SQLiteJSON.path(field)})`;
1441
+ return dir === 1 ? _event_driven_io_dumbo.SQL`${accessor} ASC` : _event_driven_io_dumbo.SQL`${accessor} DESC`;
1442
+ });
1443
+ query.push(_event_driven_io_dumbo.SQL`ORDER BY ${_event_driven_io_dumbo.SQL.merge(clauses, ",")}`);
1444
+ }
1445
+ if (options?.limit) query.push(_event_driven_io_dumbo.SQL`LIMIT ${options.limit}`);
1446
+ if (options?.skip) query.push(_event_driven_io_dumbo.SQL`OFFSET ${options.skip}`);
1447
+ return _event_driven_io_dumbo.SQL.merge([...query, _event_driven_io_dumbo.SQL`;`]);
1448
+ },
1449
+ countDocuments: (filter) => {
1450
+ const filterQuery = _event_driven_io_dumbo.SQL.check.isSQL(filter) ? filter : constructFilterQuery(filter, serializer);
1451
+ return _event_driven_io_dumbo.SQL`SELECT COUNT(1) as count FROM ${_event_driven_io_dumbo.SQL.identifier(collectionName)} ${where(filterQuery)};`;
1452
+ },
1453
+ rename: (newName) => _event_driven_io_dumbo.SQL`ALTER TABLE ${_event_driven_io_dumbo.SQL.identifier(collectionName)} RENAME TO ${_event_driven_io_dumbo.SQL.identifier(newName)};`,
1454
+ drop: (targetName = collectionName) => _event_driven_io_dumbo.SQL`DROP TABLE IF EXISTS ${_event_driven_io_dumbo.SQL.identifier(targetName)}`
1455
+ });
1456
+ const where = (filterQuery) => _event_driven_io_dumbo.SQL.check.isEmpty(filterQuery) ? _event_driven_io_dumbo.SQL.EMPTY : _event_driven_io_dumbo.SQL.merge([_event_driven_io_dumbo.SQL`WHERE `, filterQuery]);
1457
+
1458
+ //#endregion
8
1459
  //#region src/storage/sqlite/sqlite3/index.ts
9
1460
  const sqlite3PongoDriver = {
10
1461
  driverType: _event_driven_io_dumbo_sqlite3.SQLite3DriverType,
11
1462
  databaseFactory: (options) => {
12
1463
  const databaseName = options.databaseName ?? "db:default";
13
- return require_core.PongoDatabase({
1464
+ return PongoDatabase({
14
1465
  ...options,
15
1466
  pool: (0, _event_driven_io_dumbo.dumbo)({
16
1467
  connectionString: options.connectionString,
@@ -18,29 +1469,29 @@ const sqlite3PongoDriver = {
18
1469
  ...options.connectionOptions,
19
1470
  serialization: { serializer: options.serializer }
20
1471
  }),
21
- schemaComponent: require_core.PongoDatabaseSchemaComponent({
1472
+ schemaComponent: PongoDatabaseSchemaComponent({
22
1473
  driverType: _event_driven_io_dumbo_sqlite3.SQLite3DriverType,
23
- collectionFactory: (schema) => require_core.PongoCollectionSchemaComponent({
1474
+ collectionFactory: (schema) => PongoCollectionSchemaComponent({
24
1475
  driverType: _event_driven_io_dumbo_sqlite3.SQLite3DriverType,
25
1476
  definition: schema,
26
- migrationsOrSchemaComponents: { migrations: require_core$1.pongoCollectionSQLiteMigrations(schema.name) },
27
- sqlBuilder: require_core$1.sqliteSQLBuilder(schema.name, options.serialization?.serializer ?? _event_driven_io_dumbo.JSONSerializer)
1477
+ migrationsOrSchemaComponents: { migrations: pongoCollectionSQLiteMigrations(schema.name) },
1478
+ sqlBuilder: sqliteSQLBuilder(schema.name, options.serialization?.serializer ?? _event_driven_io_dumbo.JSONSerializer)
28
1479
  }),
29
- definition: options.schema?.definition ?? require_core.pongoSchema.db(databaseName, {})
1480
+ definition: options.schema?.definition ?? pongoSchema.db(databaseName, {})
30
1481
  }),
31
1482
  databaseName
32
1483
  });
33
1484
  }
34
1485
  };
35
1486
  const useSqlite3PongoDriver = () => {
36
- require_core.pongoDriverRegistry.register(_event_driven_io_dumbo_sqlite3.SQLite3DriverType, sqlite3PongoDriver);
1487
+ pongoDriverRegistry.register(_event_driven_io_dumbo_sqlite3.SQLite3DriverType, sqlite3PongoDriver);
37
1488
  };
38
1489
  useSqlite3PongoDriver();
39
1490
 
40
1491
  //#endregion
41
- exports.pongoCollectionSQLiteMigrations = require_core$1.pongoCollectionSQLiteMigrations;
1492
+ exports.pongoCollectionSQLiteMigrations = pongoCollectionSQLiteMigrations;
42
1493
  exports.pongoDriver = sqlite3PongoDriver;
43
1494
  exports.sqlite3Driver = sqlite3PongoDriver;
44
- exports.sqliteSQLBuilder = require_core$1.sqliteSQLBuilder;
1495
+ exports.sqliteSQLBuilder = sqliteSQLBuilder;
45
1496
  exports.useSqlite3PongoDriver = useSqlite3PongoDriver;
46
1497
  //# sourceMappingURL=sqlite3.cjs.map