@event-driven-io/pongo 0.17.0-beta.42 → 0.17.0-beta.44

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 (59) 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-5npofT4H.js → core-BW9XZXW8.js} +2 -2
  16. package/dist/{core-5npofT4H.js.map → core-BW9XZXW8.js.map} +1 -1
  17. package/dist/{core-q2eF-VHQ.cjs → core-BicrzIKX.cjs} +10 -11
  18. package/dist/{core-q2eF-VHQ.cjs.map → core-BicrzIKX.cjs.map} +1 -1
  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 +1124 -15
  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 +1113 -3
  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/index-C3pnS1S_.d.cts +0 -720
  57. package/dist/index-CZOmOsQt.d.ts +0 -10
  58. package/dist/index-FXnldVnn.d.cts +0 -10
  59. package/dist/index-r7V4paf_.d.ts +0 -720
package/dist/shim.js CHANGED
@@ -1,5 +1,6 @@
1
- import { o as pongoClient, s as pongoSession } from "./core-BHdOCUrr.js";
2
- import { parseConnectionString, toDatabaseDriverType } from "@event-driven-io/dumbo";
1
+ import { JSONSerializer, getDatabaseMetadata, parseConnectionString, toDatabaseDriverType } from "@event-driven-io/dumbo";
2
+ import { LRUCache } from "lru-cache";
3
+ import "uuid";
3
4
 
4
5
  //#region src/mongo/findCursor.ts
5
6
  var FindCursor = class {
@@ -31,6 +32,554 @@ var FindCursor = class {
31
32
  }
32
33
  };
33
34
 
35
+ //#endregion
36
+ //#region src/core/cache/pongoCacheWrapper.ts
37
+ const pongoCacheWrapper = (options) => {
38
+ const { provider, hooks } = options;
39
+ const onError = (error, operation) => {
40
+ hooks?.onError?.(error, operation);
41
+ };
42
+ let isClosed = false;
43
+ return {
44
+ cacheType: provider.cacheType,
45
+ async get(key) {
46
+ try {
47
+ const result = await provider.get(key);
48
+ if (result !== void 0) hooks?.onHit?.(key);
49
+ else hooks?.onMiss?.(key);
50
+ return result;
51
+ } catch (error) {
52
+ onError(error, "get");
53
+ hooks?.onMiss?.(key);
54
+ return;
55
+ }
56
+ },
57
+ async getMany(keys) {
58
+ try {
59
+ return await provider.getMany(keys);
60
+ } catch (error) {
61
+ onError(error, "getMany");
62
+ return [];
63
+ }
64
+ },
65
+ async set(key, value) {
66
+ try {
67
+ await provider.set(key, value);
68
+ } catch (error) {
69
+ onError(error, "set");
70
+ }
71
+ },
72
+ async setMany(entries) {
73
+ try {
74
+ await provider.setMany(entries);
75
+ } catch (error) {
76
+ onError(error, "setMany");
77
+ }
78
+ },
79
+ async update(key, updater) {
80
+ try {
81
+ await provider.update(key, updater);
82
+ } catch (error) {
83
+ onError(error, "update");
84
+ }
85
+ },
86
+ async updateMany(keys, updater) {
87
+ try {
88
+ await provider.updateMany(keys, updater);
89
+ } catch (error) {
90
+ onError(error, "updateMany");
91
+ }
92
+ },
93
+ async delete(key) {
94
+ try {
95
+ await provider.delete(key);
96
+ hooks?.onEvict?.(key);
97
+ } catch (error) {
98
+ onError(error, "delete");
99
+ }
100
+ },
101
+ async deleteMany(keys) {
102
+ try {
103
+ await provider.deleteMany(keys);
104
+ for (const key of keys) hooks?.onEvict?.(key);
105
+ } catch (error) {
106
+ onError(error, "deleteMany");
107
+ }
108
+ },
109
+ clear() {
110
+ return provider.clear();
111
+ },
112
+ close() {
113
+ if (isClosed) return;
114
+ isClosed = true;
115
+ return provider.close();
116
+ }
117
+ };
118
+ };
119
+
120
+ //#endregion
121
+ //#region src/core/cache/providers/identityMapCache.ts
122
+ const identityMapCache = () => {
123
+ const store = /* @__PURE__ */ new Map();
124
+ return {
125
+ cacheType: "pongo:cache:identity-map",
126
+ get: (key) => Promise.resolve(store.has(key) ? store.get(key) : void 0),
127
+ getMany: (keys) => keys.map((k) => store.has(k) ? store.get(k) : void 0),
128
+ set: (key, value) => {
129
+ store.set(key, value);
130
+ },
131
+ setMany: (entries) => {
132
+ for (const { key, value } of entries) store.set(key, value);
133
+ },
134
+ update: (key, _updater) => {
135
+ store.delete(key);
136
+ },
137
+ updateMany: (keys, _updater) => {
138
+ for (const key of keys) store.delete(key);
139
+ },
140
+ delete: (key) => {
141
+ store.delete(key);
142
+ },
143
+ deleteMany: (keys) => {
144
+ for (const key of keys) store.delete(key);
145
+ },
146
+ clear: () => {
147
+ store.clear();
148
+ },
149
+ close: () => {
150
+ store.clear();
151
+ }
152
+ };
153
+ };
154
+
155
+ //#endregion
156
+ //#region src/core/cache/providers/lruCache.ts
157
+ const defaultLRUCacheOptions = { max: 1e3 };
158
+ const lruCache = (options) => {
159
+ const cache = new LRUCache({
160
+ ...defaultLRUCacheOptions,
161
+ ...options
162
+ });
163
+ return {
164
+ cacheType: "pongo:cache:lru",
165
+ get: (key) => {
166
+ const entry = cache.get(key);
167
+ if (entry === void 0) return void 0;
168
+ return entry.doc;
169
+ },
170
+ getMany: (keys) => keys.map((k) => {
171
+ const entry = cache.get(k);
172
+ if (entry === void 0) return void 0;
173
+ return entry.doc;
174
+ }),
175
+ set: (key, value) => {
176
+ cache.set(key, { doc: value });
177
+ },
178
+ setMany: (entries) => {
179
+ for (const { key, value } of entries) cache.set(key, { doc: value });
180
+ },
181
+ update: (key, _updater) => {
182
+ cache.delete(key);
183
+ },
184
+ updateMany(keys, _updater) {
185
+ for (const key of keys) cache.delete(key);
186
+ },
187
+ delete: (key) => {
188
+ cache.delete(key);
189
+ },
190
+ deleteMany: (keys) => {
191
+ for (const key of keys) cache.delete(key);
192
+ },
193
+ clear: () => {
194
+ cache.clear();
195
+ },
196
+ close: () => {
197
+ cache.clear();
198
+ }
199
+ };
200
+ };
201
+
202
+ //#endregion
203
+ //#region src/core/cache/providers/noopCache.ts
204
+ const noopCacheProvider = {
205
+ cacheType: "pongo:cache:no-op",
206
+ get: () => void 0,
207
+ set: () => {},
208
+ update: () => {},
209
+ delete: () => {},
210
+ getMany: (keys) => keys.map(() => void 0),
211
+ setMany: () => {},
212
+ updateMany: () => {},
213
+ deleteMany: () => {},
214
+ clear: () => {},
215
+ close: () => {}
216
+ };
217
+
218
+ //#endregion
219
+ //#region src/core/cache/pongoCache.ts
220
+ const DEFAULT_CONFIG = { type: "in-memory" };
221
+ const pongoCache = (options) => {
222
+ if (options === void 0 || options === "disabled") return noopCacheProvider;
223
+ if ("cacheType" in options) return options;
224
+ const config = options ?? DEFAULT_CONFIG;
225
+ if (config.type === "identity-map") return identityMapCache();
226
+ return pongoCacheWrapper({ provider: lruCache(config) });
227
+ };
228
+
229
+ //#endregion
230
+ //#region src/core/cache/transactions/pongoTransactionCache.ts
231
+ const pongoTransactionCache = (options) => {
232
+ const innerCache = options?.cache ?? identityMapCache();
233
+ const operations = [];
234
+ return {
235
+ type: "pongo:cache:transaction-buffer",
236
+ get(key) {
237
+ return innerCache.get(key);
238
+ },
239
+ set(key, value, options) {
240
+ const { mainCache } = options;
241
+ innerCache.set(key, value);
242
+ operations.push({
243
+ type: "set",
244
+ key,
245
+ value,
246
+ mainCache
247
+ });
248
+ },
249
+ update(key, updater, options) {
250
+ const { mainCache } = options;
251
+ innerCache.update(key, updater);
252
+ operations.push({
253
+ type: "update",
254
+ key,
255
+ updater,
256
+ mainCache
257
+ });
258
+ },
259
+ delete(key, options) {
260
+ innerCache.delete(key);
261
+ operations.push({
262
+ type: "delete",
263
+ key,
264
+ mainCache: options.mainCache
265
+ });
266
+ },
267
+ getMany(keys) {
268
+ return innerCache.getMany(keys);
269
+ },
270
+ setMany(entries, options) {
271
+ innerCache.setMany(entries);
272
+ operations.push({
273
+ type: "setMany",
274
+ entries,
275
+ mainCache: options.mainCache
276
+ });
277
+ },
278
+ updateMany(keys, updater, options) {
279
+ const { mainCache } = options;
280
+ innerCache.updateMany(keys, updater);
281
+ operations.push({
282
+ type: "updateMany",
283
+ keys,
284
+ updater,
285
+ mainCache
286
+ });
287
+ },
288
+ deleteMany(keys, options) {
289
+ innerCache.deleteMany(keys);
290
+ operations.push({
291
+ type: "deleteMany",
292
+ keys,
293
+ mainCache: options.mainCache
294
+ });
295
+ },
296
+ clear() {
297
+ innerCache.clear();
298
+ operations.length = 0;
299
+ },
300
+ async commit() {
301
+ for (const op of operations) switch (op.type) {
302
+ case "set":
303
+ await op.mainCache.set(op.key, op.value);
304
+ break;
305
+ case "setMany":
306
+ await op.mainCache.setMany(op.entries);
307
+ break;
308
+ case "update":
309
+ await op.mainCache.update(op.key, op.updater);
310
+ break;
311
+ case "updateMany":
312
+ await op.mainCache.updateMany(op.keys, op.updater);
313
+ break;
314
+ case "delete":
315
+ await op.mainCache.delete(op.key);
316
+ break;
317
+ case "deleteMany":
318
+ await op.mainCache.deleteMany(op.keys);
319
+ break;
320
+ }
321
+ innerCache.clear();
322
+ operations.length = 0;
323
+ }
324
+ };
325
+ };
326
+
327
+ //#endregion
328
+ //#region src/core/database/pongoDatabaseCache.ts
329
+ const PongoDatabaseCache = ({ driver, typedSchema }) => {
330
+ const dbClients = /* @__PURE__ */ new Map();
331
+ const getDatabaseDefinition = (dbName) => Object.values(typedSchema?.dbs ?? {}).find((d) => d.name === dbName);
332
+ return {
333
+ getOrCreate: (createOptions) => {
334
+ const metadata = getDatabaseMetadata(driver.driverType);
335
+ const dbName = createOptions.databaseName ?? metadata?.parseDatabaseName?.("connectionString" in createOptions ? createOptions.connectionString : void 0) ?? "db:default";
336
+ const existing = dbClients.get(dbName);
337
+ if (existing) return existing;
338
+ const definition = getDatabaseDefinition(createOptions.databaseName);
339
+ const newDb = driver.databaseFactory({
340
+ ...createOptions,
341
+ databaseName: dbName,
342
+ schema: {
343
+ ...createOptions.schema,
344
+ ...definition ? { definition } : {}
345
+ }
346
+ });
347
+ dbClients.set(dbName, newDb);
348
+ return newDb;
349
+ },
350
+ all: () => Array.from(dbClients.values()),
351
+ forAll: (func) => {
352
+ return Promise.all(Array.from(dbClients.values()).map((v) => v).map(func));
353
+ }
354
+ };
355
+ };
356
+
357
+ //#endregion
358
+ //#region src/core/schema/index.ts
359
+ const pongoCollectionSchema = (name) => ({ name });
360
+ pongoCollectionSchema.from = (collectionNames) => collectionNames.reduce((acc, collectionName) => (acc[collectionName] = pongoSchema.collection(collectionName), acc), {});
361
+ function pongoDbSchema(nameOrCollections, collections) {
362
+ if (collections === void 0) {
363
+ if (typeof nameOrCollections === "string") throw new Error("You need to provide colleciton definition");
364
+ return { collections: nameOrCollections };
365
+ }
366
+ return nameOrCollections && typeof nameOrCollections === "string" ? {
367
+ name: nameOrCollections,
368
+ collections
369
+ } : { collections };
370
+ }
371
+ pongoDbSchema.from = (databaseName, collectionNames) => databaseName ? pongoDbSchema(databaseName, pongoCollectionSchema.from(collectionNames)) : pongoDbSchema(pongoCollectionSchema.from(collectionNames));
372
+ const pongoClientSchema = (dbs) => ({ dbs });
373
+ const pongoSchema = {
374
+ client: pongoClientSchema,
375
+ db: pongoDbSchema,
376
+ collection: pongoCollectionSchema
377
+ };
378
+ const proxyClientWithSchema = (client, schema) => {
379
+ if (!schema) return client;
380
+ const dbNames = Object.keys(schema.dbs);
381
+ return new Proxy(client, { get(target, prop) {
382
+ if (dbNames.includes(prop)) return client.db(schema.dbs[prop]?.name);
383
+ return target[prop];
384
+ } });
385
+ };
386
+
387
+ //#endregion
388
+ //#region src/core/drivers/databaseDriver.ts
389
+ const PongoDriverRegistry = () => {
390
+ const drivers = /* @__PURE__ */ new Map();
391
+ const register = (driverType, driver) => {
392
+ const entry = drivers.get(driverType);
393
+ if (entry && (typeof entry !== "function" || typeof driver === "function")) return;
394
+ drivers.set(driverType, driver);
395
+ };
396
+ const tryResolve = async (driverType) => {
397
+ const entry = drivers.get(driverType);
398
+ if (!entry) return null;
399
+ if (typeof entry !== "function") return entry;
400
+ const driver = await entry();
401
+ register(driverType, driver);
402
+ return driver;
403
+ };
404
+ const tryGet = (driverType) => {
405
+ const entry = drivers.get(driverType);
406
+ return entry && typeof entry !== "function" ? entry : null;
407
+ };
408
+ const has = (driverType) => drivers.has(driverType);
409
+ return {
410
+ register,
411
+ tryResolve,
412
+ tryGet,
413
+ has,
414
+ get databaseDriverTypes() {
415
+ return Array.from(drivers.keys());
416
+ }
417
+ };
418
+ };
419
+ const pongoDriverRegistry$1 = globalThis.pongoDriverRegistry = globalThis.pongoDriverRegistry ?? PongoDriverRegistry();
420
+
421
+ //#endregion
422
+ //#region src/core/pongoTransaction.ts
423
+ const pongoTransaction = (options) => {
424
+ let isCommitted = false;
425
+ let isRolledBack = false;
426
+ let databaseName = null;
427
+ let transaction = null;
428
+ const cache = pongoTransactionCache();
429
+ return {
430
+ cache,
431
+ enlistDatabase: async (db) => {
432
+ if (transaction && databaseName !== db.databaseName) throw new Error("There's already other database assigned to transaction");
433
+ if (transaction && databaseName === db.databaseName) return transaction;
434
+ databaseName = db.databaseName;
435
+ transaction = db.transaction();
436
+ await transaction.begin();
437
+ return transaction;
438
+ },
439
+ commit: async () => {
440
+ if (isCommitted) return;
441
+ if (isRolledBack) throw new Error("Transaction is not active!");
442
+ isCommitted = true;
443
+ if (transaction) {
444
+ await transaction.commit();
445
+ transaction = null;
446
+ }
447
+ await cache.commit();
448
+ },
449
+ rollback: async (error) => {
450
+ if (isCommitted) throw new Error("Cannot rollback commited transaction!");
451
+ if (isRolledBack) return;
452
+ isRolledBack = true;
453
+ if (transaction) {
454
+ await transaction.rollback(error);
455
+ transaction = null;
456
+ }
457
+ cache.clear();
458
+ },
459
+ databaseName,
460
+ isStarting: false,
461
+ isCommitted,
462
+ get isActive() {
463
+ return !isCommitted && !isRolledBack;
464
+ },
465
+ get sqlExecutor() {
466
+ if (transaction === null) throw new Error("No database transaction was started");
467
+ return transaction.execute;
468
+ },
469
+ options
470
+ };
471
+ };
472
+
473
+ //#endregion
474
+ //#region src/core/pongoSession.ts
475
+ const isActive = (transaction) => transaction?.isActive === true;
476
+ function assertInActiveTransaction(transaction) {
477
+ if (!isActive(transaction)) throw new Error("No active transaction exists!");
478
+ }
479
+ function assertNotInActiveTransaction(transaction) {
480
+ if (isActive(transaction)) throw new Error("Active transaction already exists!");
481
+ }
482
+ const pongoSession = (options) => {
483
+ const explicit = options?.explicit === true;
484
+ const defaultTransactionOptions = options?.defaultTransactionOptions ?? { get snapshotEnabled() {
485
+ return false;
486
+ } };
487
+ let transaction = null;
488
+ let hasEnded = false;
489
+ const startTransaction = (options) => {
490
+ assertNotInActiveTransaction(transaction);
491
+ transaction = pongoTransaction(options ?? defaultTransactionOptions);
492
+ };
493
+ const commitTransaction = async () => {
494
+ assertInActiveTransaction(transaction);
495
+ await transaction.commit();
496
+ };
497
+ const abortTransaction = async () => {
498
+ assertInActiveTransaction(transaction);
499
+ await transaction.rollback();
500
+ };
501
+ const endSession = async () => {
502
+ if (hasEnded) return;
503
+ hasEnded = true;
504
+ if (isActive(transaction)) await transaction.rollback();
505
+ };
506
+ const session = {
507
+ get hasEnded() {
508
+ return hasEnded;
509
+ },
510
+ explicit,
511
+ defaultTransactionOptions: defaultTransactionOptions ?? { get snapshotEnabled() {
512
+ return false;
513
+ } },
514
+ get transaction() {
515
+ return transaction;
516
+ },
517
+ get snapshotEnabled() {
518
+ return defaultTransactionOptions.snapshotEnabled;
519
+ },
520
+ endSession,
521
+ incrementTransactionNumber: () => {},
522
+ inTransaction: () => isActive(transaction),
523
+ startTransaction,
524
+ commitTransaction,
525
+ abortTransaction,
526
+ withTransaction: async (fn, options) => {
527
+ startTransaction(options);
528
+ try {
529
+ const result = await fn(session);
530
+ await commitTransaction();
531
+ return result;
532
+ } catch (error) {
533
+ await abortTransaction();
534
+ throw error;
535
+ }
536
+ }
537
+ };
538
+ return session;
539
+ };
540
+
541
+ //#endregion
542
+ //#region src/core/pongoClient.ts
543
+ const pongoClient = (options) => {
544
+ const { driver, schema, errors, cache: cacheOptions, serialization, ...connectionOptions } = options;
545
+ const dbClients = PongoDatabaseCache({
546
+ driver,
547
+ typedSchema: schema?.definition
548
+ });
549
+ const serializer = JSONSerializer.from(options);
550
+ const cache = cacheOptions === "disabled" || cacheOptions === void 0 ? "disabled" : pongoCache(cacheOptions);
551
+ const pongoClient = {
552
+ driverType: driver.driverType,
553
+ connect: async () => {
554
+ await dbClients.forAll((db) => db.connect());
555
+ return pongoClient;
556
+ },
557
+ close: async () => {
558
+ await dbClients.forAll((db) => db.close());
559
+ },
560
+ db: (dbName, options) => {
561
+ return dbClients.getOrCreate({
562
+ ...connectionOptions,
563
+ databaseName: dbName,
564
+ serializer,
565
+ errors,
566
+ cache: options?.cache ?? cache,
567
+ serialization
568
+ });
569
+ },
570
+ startSession: pongoSession,
571
+ withSession: async (callback) => {
572
+ const session = pongoSession();
573
+ try {
574
+ return await callback(session);
575
+ } finally {
576
+ await session.endSession();
577
+ }
578
+ }
579
+ };
580
+ return proxyClientWithSchema(pongoClient, schema?.definition);
581
+ };
582
+
34
583
  //#endregion
35
584
  //#region src/mongo/mongoCollection.ts
36
585
  const toCollectionOperationOptions = (options) => options?.session ? { session: options.session } : void 0;