@event-driven-io/dumbo 0.13.0-beta.46 → 0.13.0-beta.47

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 (43) hide show
  1. package/dist/cloudflare.cjs +432 -69
  2. package/dist/cloudflare.cjs.map +1 -1
  3. package/dist/cloudflare.d.cts +2 -2
  4. package/dist/cloudflare.d.ts +2 -2
  5. package/dist/cloudflare.js +432 -69
  6. package/dist/cloudflare.js.map +1 -1
  7. package/dist/{index-B5krjjrh.d.ts → index-4hcCG4SA.d.ts} +66 -30
  8. package/dist/{index-BaLXbc2l.d.ts → index-B7366zWR.d.ts} +1 -1
  9. package/dist/{index-_dj3upBo.d.cts → index-BJ-dm7dG.d.cts} +66 -30
  10. package/dist/{index-kYttgYkC.d.cts → index-BkJ1pipM.d.cts} +1 -1
  11. package/dist/{index-C9B46a1u.d.ts → index-CQokD3za.d.cts} +2 -2
  12. package/dist/{index-BoLWBnxd.d.cts → index-DIJv-L6M.d.ts} +2 -2
  13. package/dist/index.cjs +523 -269
  14. package/dist/index.cjs.map +1 -1
  15. package/dist/index.d.cts +2 -2
  16. package/dist/index.d.ts +2 -2
  17. package/dist/index.js +522 -270
  18. package/dist/index.js.map +1 -1
  19. package/dist/pg.cjs +168 -28
  20. package/dist/pg.cjs.map +1 -1
  21. package/dist/pg.d.cts +2 -2
  22. package/dist/pg.d.ts +2 -2
  23. package/dist/pg.js +168 -28
  24. package/dist/pg.js.map +1 -1
  25. package/dist/postgresql.cjs +4 -0
  26. package/dist/postgresql.cjs.map +1 -1
  27. package/dist/postgresql.d.cts +2 -2
  28. package/dist/postgresql.d.ts +2 -2
  29. package/dist/postgresql.js +4 -0
  30. package/dist/postgresql.js.map +1 -1
  31. package/dist/sqlite.cjs +429 -68
  32. package/dist/sqlite.cjs.map +1 -1
  33. package/dist/sqlite.d.cts +2 -2
  34. package/dist/sqlite.d.ts +2 -2
  35. package/dist/sqlite.js +429 -68
  36. package/dist/sqlite.js.map +1 -1
  37. package/dist/sqlite3.cjs +639 -343
  38. package/dist/sqlite3.cjs.map +1 -1
  39. package/dist/sqlite3.d.cts +10 -10
  40. package/dist/sqlite3.d.ts +10 -10
  41. package/dist/sqlite3.js +639 -343
  42. package/dist/sqlite3.js.map +1 -1
  43. package/package.json +1 -1
package/dist/pg.cjs CHANGED
@@ -320,6 +320,91 @@ const mapPostgresError = (error) => {
320
320
  });
321
321
  };
322
322
 
323
+ //#endregion
324
+ //#region src/core/taskProcessing/abort.ts
325
+ const never = { signal: new AbortController().signal };
326
+ const from = (options) => options?.abort ?? never;
327
+ const getSignal = (abort) => "signal" in abort ? abort.signal : abort;
328
+ const reason = (abort) => {
329
+ const signal = getSignal(abort);
330
+ return signal.reason instanceof Error ? signal.reason : new DumboError(typeof signal.reason === "string" ? signal.reason : "Operation aborted");
331
+ };
332
+ const scope = (parent, onAbort) => {
333
+ const controller = new AbortController();
334
+ return {
335
+ abort: (reason) => controller.abort(reason),
336
+ dispose: onAbortSignal(parent, (reason) => {
337
+ controller.abort(reason);
338
+ onAbort?.(reason);
339
+ }),
340
+ signal: controller.signal
341
+ };
342
+ };
343
+ const execute = (operation, options) => {
344
+ const abort = options?.abort;
345
+ if (!abort) return operation();
346
+ const signal = abort.signal;
347
+ if (signal.aborted) return Promise.reject(reason(abort));
348
+ return new Promise((resolve, reject) => {
349
+ let finished = false;
350
+ const rejectOnAbort = () => {
351
+ if (finished) return;
352
+ finished = true;
353
+ reject(reason(abort));
354
+ };
355
+ signal.addEventListener("abort", rejectOnAbort, { once: true });
356
+ let operationPromise;
357
+ try {
358
+ operationPromise = operation();
359
+ } catch (error) {
360
+ finished = true;
361
+ signal.removeEventListener("abort", rejectOnAbort);
362
+ reject(error);
363
+ return;
364
+ }
365
+ operationPromise.then((result) => {
366
+ if (finished) return;
367
+ finished = true;
368
+ signal.removeEventListener("abort", rejectOnAbort);
369
+ resolve(result);
370
+ }).catch((error) => {
371
+ if (finished) return;
372
+ finished = true;
373
+ signal.removeEventListener("abort", rejectOnAbort);
374
+ reject(error);
375
+ });
376
+ });
377
+ };
378
+ const throwIfAborted = (options) => {
379
+ const abort = options?.abort;
380
+ if (abort?.signal.aborted) throw reason(abort);
381
+ };
382
+ const rejectIfAborted = (options) => {
383
+ const abort = options?.abort;
384
+ return abort?.signal.aborted ? Promise.reject(reason(abort)) : void 0;
385
+ };
386
+ const onAbortSignal = (abort, handle) => {
387
+ if (!abort) return () => {};
388
+ const signal = abort.signal;
389
+ if (signal.aborted) {
390
+ handle(reason(abort));
391
+ return () => {};
392
+ }
393
+ const abortListener = () => handle(reason(abort));
394
+ signal.addEventListener("abort", abortListener, { once: true });
395
+ return () => signal.removeEventListener("abort", abortListener);
396
+ };
397
+ const Abort = {
398
+ execute,
399
+ from,
400
+ never,
401
+ onAbort: onAbortSignal,
402
+ reason,
403
+ rejectIfAborted,
404
+ scope,
405
+ throwIfAborted
406
+ };
407
+
323
408
  //#endregion
324
409
  //#region src/core/execute/execute.ts
325
410
  const mapSQLQueryResult = (result, mapping) => {
@@ -344,20 +429,45 @@ var BatchCommandNoChangesError = class BatchCommandNoChangesError extends DumboE
344
429
  }
345
430
  };
346
431
  const sqlExecutor = (sqlExecutor, options) => ({
347
- query: (sql, queryOptions) => executeInNewDbClient((client) => sqlExecutor.query(client, sql, queryOptions), options),
348
- batchQuery: (sqls, queryOptions) => executeInNewDbClient((client) => sqlExecutor.batchQuery(client, sqls, queryOptions), options),
349
- command: (sql, commandOptions) => executeInNewDbClient((client) => sqlExecutor.command(client, sql, commandOptions), options),
350
- batchCommand: (sqls, commandOptions) => executeInNewDbClient((client) => sqlExecutor.batchCommand(client, sqls, commandOptions), options)
432
+ query: (sql, queryOptions) => executeInNewDbClient((client) => sqlExecutor.query(client, sql, queryOptions), {
433
+ ...options,
434
+ ...queryOptions
435
+ }),
436
+ batchQuery: (sqls, queryOptions) => executeInNewDbClient((client) => sqlExecutor.batchQuery(client, sqls, queryOptions), {
437
+ ...options,
438
+ ...queryOptions
439
+ }),
440
+ command: (sql, commandOptions) => executeInNewDbClient((client) => sqlExecutor.command(client, sql, commandOptions), {
441
+ ...options,
442
+ ...commandOptions
443
+ }),
444
+ batchCommand: (sqls, commandOptions) => executeInNewDbClient((client) => sqlExecutor.batchCommand(client, sqls, commandOptions), {
445
+ ...options,
446
+ ...commandOptions
447
+ })
351
448
  });
352
449
  const sqlExecutorInNewConnection = (options) => ({
353
- query: (sql, queryOptions) => executeInNewConnection((connection) => connection.execute.query(sql, queryOptions), options),
354
- batchQuery: (sqls, queryOptions) => executeInNewConnection((connection) => connection.execute.batchQuery(sqls, queryOptions), options),
355
- command: (sql, commandOptions) => executeInNewConnection((connection) => connection.execute.command(sql, commandOptions), options),
356
- batchCommand: (sqls, commandOptions) => executeInNewConnection((connection) => connection.execute.batchCommand(sqls, commandOptions), options)
450
+ query: (sql, queryOptions) => executeInNewConnection((connection) => connection.execute.query(sql, queryOptions), {
451
+ ...options,
452
+ ...queryOptions
453
+ }),
454
+ batchQuery: (sqls, queryOptions) => executeInNewConnection((connection) => connection.execute.batchQuery(sqls, queryOptions), {
455
+ ...options,
456
+ ...queryOptions
457
+ }),
458
+ command: (sql, commandOptions) => executeInNewConnection((connection) => connection.execute.command(sql, commandOptions), {
459
+ ...options,
460
+ ...commandOptions
461
+ }),
462
+ batchCommand: (sqls, commandOptions) => executeInNewConnection((connection) => connection.execute.batchCommand(sqls, commandOptions), {
463
+ ...options,
464
+ ...commandOptions
465
+ })
357
466
  });
358
467
  const executeInNewDbClient = async (handle, options) => {
468
+ Abort.throwIfAborted(options);
359
469
  const { connect, close } = options;
360
- const client = await connect();
470
+ const client = await connect({ abort: Abort.from(options) });
361
471
  try {
362
472
  return await handle(client);
363
473
  } catch (error) {
@@ -366,7 +476,8 @@ const executeInNewDbClient = async (handle, options) => {
366
476
  }
367
477
  };
368
478
  const executeInNewConnection = async (handle, options) => {
369
- const connection = await options.connection();
479
+ Abort.throwIfAborted(options);
480
+ const connection = await options.connection({ abort: Abort.from(options) });
370
481
  try {
371
482
  return await handle(connection);
372
483
  } finally {
@@ -439,10 +550,10 @@ const toTransactionResult = (transactionResult) => transactionResult !== void 0
439
550
  success: true,
440
551
  result: transactionResult
441
552
  };
442
- const executeInTransaction = async (transaction, handle) => {
553
+ const executeInTransaction = async (transaction, handle, context = { abort: Abort.never }) => {
443
554
  await transaction.begin();
444
555
  try {
445
- const { success, result } = toTransactionResult(await handle(transaction));
556
+ const { success, result } = toTransactionResult(await handle(transaction, context));
446
557
  if (success) await transaction.commit();
447
558
  else await transaction.rollback();
448
559
  return result;
@@ -451,18 +562,32 @@ const executeInTransaction = async (transaction, handle) => {
451
562
  throw e;
452
563
  }
453
564
  };
565
+ const executeInNestedTransaction = async (transaction, handle, options, context) => {
566
+ Abort.throwIfAborted(options);
567
+ if (!(options?.allowNestedTransactions ?? transaction._transactionOptions.allowNestedTransactions ?? false)) throw new InvalidOperationError("Cannot start a nested transaction: allowNestedTransactions is false. Set transactionOptions: { allowNestedTransactions: true } on your pool or connection.");
568
+ return executeInTransaction(transaction, handle, context);
569
+ };
454
570
  const transactionFactoryWithDbClient = (connect, initTransaction) => {
455
571
  let currentTransaction = void 0;
456
- const getOrInitCurrentTransaction = (options) => currentTransaction ?? (currentTransaction = initTransaction(connect(), {
457
- close: () => {
458
- currentTransaction = void 0;
459
- return Promise.resolve();
460
- },
461
- ...options ?? {}
462
- }));
572
+ const getOrInitCurrentTransaction = (options) => {
573
+ Abort.throwIfAborted(options);
574
+ if (currentTransaction) return currentTransaction;
575
+ currentTransaction = initTransaction(connect({ abort: Abort.from(options) }), {
576
+ close: () => {
577
+ currentTransaction = void 0;
578
+ return Promise.resolve();
579
+ },
580
+ ...options ?? {}
581
+ });
582
+ return currentTransaction;
583
+ };
463
584
  return {
464
585
  transaction: getOrInitCurrentTransaction,
465
- withTransaction: (handle, options) => executeInTransaction(getOrInitCurrentTransaction(options), handle)
586
+ withTransaction: (handle, options) => {
587
+ const abortRejection = Abort.rejectIfAborted(options);
588
+ if (abortRejection) return abortRejection;
589
+ return executeInTransaction(getOrInitCurrentTransaction(options), handle, { abort: Abort.from(options) });
590
+ }
466
591
  };
467
592
  };
468
593
  const wrapInConnectionClosure = async (connection, handle) => {
@@ -474,7 +599,8 @@ const wrapInConnectionClosure = async (connection, handle) => {
474
599
  };
475
600
  const transactionFactoryWithNewConnection = (connect) => ({
476
601
  transaction: (options) => {
477
- const connection = connect();
602
+ Abort.throwIfAborted(options);
603
+ const connection = connect({ abort: Abort.from(options) });
478
604
  const transaction = connection.transaction(options);
479
605
  return {
480
606
  ...transaction,
@@ -483,7 +609,9 @@ const transactionFactoryWithNewConnection = (connect) => ({
483
609
  };
484
610
  },
485
611
  withTransaction: (handle, options) => {
486
- const connection = connect();
612
+ const abortRejection = Abort.rejectIfAborted(options);
613
+ if (abortRejection) return abortRejection;
614
+ const connection = connect({ abort: Abort.from(options) });
487
615
  const withTx = connection.withTransaction;
488
616
  return wrapInConnectionClosure(connection, () => withTx(handle, options));
489
617
  }
@@ -495,9 +623,10 @@ const createConnection = (options) => {
495
623
  const { driverType, connect, close, initTransaction, executor, serializer } = options;
496
624
  let client = null;
497
625
  let connectPromise = null;
498
- const getClient = async () => {
626
+ const getClient = async (context) => {
627
+ Abort.throwIfAborted(context);
499
628
  if (client) return client;
500
- if (!connectPromise) connectPromise = connect().then((c) => {
629
+ if (!connectPromise) connectPromise = connect(context ?? { abort: Abort.never }).then((c) => {
501
630
  client = c;
502
631
  return c;
503
632
  });
@@ -523,7 +652,10 @@ const createAmbientConnectionPool = (options) => {
523
652
  getConnection: () => connection,
524
653
  execute: connection.execute,
525
654
  transaction: (options) => connection.transaction(options),
526
- withConnection: (handle, _options) => handle(connection),
655
+ withConnection: (handle, options) => {
656
+ Abort.throwIfAborted(options);
657
+ return handle(connection, { abort: Abort.from(options) });
658
+ },
527
659
  withTransaction: (handle, options) => {
528
660
  const withTx = connection.withTransaction;
529
661
  return withTx(handle, options);
@@ -532,11 +664,17 @@ const createAmbientConnectionPool = (options) => {
532
664
  };
533
665
  const createConnectionPool = (pool) => {
534
666
  const { driverType, getConnection } = pool;
535
- const connection = "connection" in pool ? pool.connection : () => Promise.resolve(getConnection());
667
+ const connection = "connection" in pool ? pool.connection : (options) => {
668
+ Abort.throwIfAborted(options);
669
+ return Promise.resolve(getConnection({ abort: Abort.from(options) }));
670
+ };
536
671
  return {
537
672
  driverType,
538
673
  connection,
539
- withConnection: "withConnection" in pool ? pool.withConnection : (handle, _options) => executeInNewConnection(handle, { connection }),
674
+ withConnection: "withConnection" in pool ? pool.withConnection : (handle, options) => executeInNewConnection((connection) => handle(connection, { abort: Abort.from(options) }), {
675
+ connection,
676
+ ...options
677
+ }),
540
678
  close: "close" in pool ? pool.close : () => Promise.resolve(),
541
679
  execute: "execute" in pool ? pool.execute : sqlExecutorInNewConnection({
542
680
  driverType,
@@ -2180,18 +2318,20 @@ const pgTransaction = (connection, serializer) => (getClient, options) => {
2180
2318
  allowNestedTransactions,
2181
2319
  useSavepoints: options?.useSavepoints ?? false
2182
2320
  });
2183
- return {
2321
+ const transaction = {
2184
2322
  connection: connection(),
2185
2323
  driverType: PgDriverType,
2186
2324
  begin: tx.begin,
2187
2325
  commit: tx.commit,
2188
2326
  rollback: tx.rollback,
2189
2327
  execute: sqlExecutor(pgSQLExecutor({ serializer }), { connect: () => getClient }),
2328
+ withTransaction: (handle, options) => executeInNestedTransaction(transaction, handle, options),
2190
2329
  _transactionOptions: {
2191
2330
  ...options ?? {},
2192
2331
  allowNestedTransactions
2193
2332
  }
2194
2333
  };
2334
+ return transaction;
2195
2335
  };
2196
2336
 
2197
2337
  //#endregion