pqb 0.58.1 → 0.58.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -4996,6 +4996,54 @@ declare class Transaction {
4996
4996
  * this will cause `uncaughtException` if the callback is sync and `unhandledRejection` if it is async.
4997
4997
  */
4998
4998
  afterCommit(this: Query, hook: AfterCommitStandaloneHook): void;
4999
+ /**
5000
+ * Whenever a query in a transaction fails, the transaction is transitioned to a failed state, no further queries are possible.
5001
+ *
5002
+ * Use [catchUniqueError](/guide/error-handling.html#catchuniqueerror) to handle uniqueness errors,
5003
+ * the following examples do not use it to illustrate error handling.
5004
+ *
5005
+ * ```ts
5006
+ * // This transaction is going to fail
5007
+ * db.$transaction(async () => {
5008
+ * try {
5009
+ * // imagine it fails because the username is already taken
5010
+ * await db.user.insert({ username: 'taken' });
5011
+ * } catch (err) {
5012
+ * // even though the query is wrapped in a try-catch, the transaction fails anyway
5013
+ * }
5014
+ * });
5015
+ * ```
5016
+ *
5017
+ * You can use `catch` method on a query instead of `try-catch` to surpass the problem.
5018
+ * The following transaction won't fail:
5019
+ *
5020
+ * ```ts
5021
+ * // Transaction succeeds
5022
+ * db.$transaction(async () => {
5023
+ * const result = await db.user.insert({ username: 'taken' }).catch(() => 'failed');
5024
+ *
5025
+ * if (result === 'failed') {
5026
+ * await db.user.insert({ username: 'another' });
5027
+ * }
5028
+ * });
5029
+ * ```
5030
+ *
5031
+ * This is because when using `catch` method, `OrchidORM` will wrap the query with a savepoint.
5032
+ *
5033
+ * Alternatively, you can use the `recoverable()` method:
5034
+ *
5035
+ * ```ts
5036
+ * // Transaction succeeds
5037
+ * db.$transaction(async () => {
5038
+ * try {
5039
+ * await db.user.insert({ username: 'taken' }).recoverable();
5040
+ * } catch (err) {
5041
+ * await db.user.insert({ username: 'another' });
5042
+ * }
5043
+ * });
5044
+ * ```
5045
+ */
5046
+ recoverable<T>(this: T): T;
4999
5047
  }
5000
5048
 
5001
5049
  type AfterHook<Select extends PropertyKey[], Shape extends Column.QueryColumns> = QueryAfterHook<{
package/dist/index.js CHANGED
@@ -4578,7 +4578,7 @@ class Transaction {
4578
4578
  return result;
4579
4579
  } else {
4580
4580
  try {
4581
- sql.text = `SAVEPOINT "${transactionId}"`;
4581
+ sql.text = `SAVEPOINT "t${transactionId}"`;
4582
4582
  if (log) logData = log.beforeQuery(sql);
4583
4583
  const { adapter } = trx;
4584
4584
  await adapter.arrays(sql.text, sql.values);
@@ -4586,13 +4586,13 @@ class Transaction {
4586
4586
  try {
4587
4587
  result = await callback(adapter);
4588
4588
  } catch (err) {
4589
- sql.text = `ROLLBACK TO SAVEPOINT "${transactionId}"`;
4589
+ sql.text = `ROLLBACK TO SAVEPOINT "t${transactionId}"`;
4590
4590
  if (log) logData = log.beforeQuery(sql);
4591
4591
  await adapter.arrays(sql.text, sql.values);
4592
4592
  if (log) log.afterQuery(sql, logData);
4593
4593
  throw err;
4594
4594
  }
4595
- sql.text = `RELEASE SAVEPOINT "${transactionId}"`;
4595
+ sql.text = `RELEASE SAVEPOINT "t${transactionId}"`;
4596
4596
  if (log) logData = log.beforeQuery(sql);
4597
4597
  await adapter.arrays(sql.text, sql.values);
4598
4598
  if (log) log.afterQuery(sql, logData);
@@ -4669,6 +4669,58 @@ class Transaction {
4669
4669
  queueMicrotask(hook);
4670
4670
  }
4671
4671
  }
4672
+ /**
4673
+ * Whenever a query in a transaction fails, the transaction is transitioned to a failed state, no further queries are possible.
4674
+ *
4675
+ * Use [catchUniqueError](/guide/error-handling.html#catchuniqueerror) to handle uniqueness errors,
4676
+ * the following examples do not use it to illustrate error handling.
4677
+ *
4678
+ * ```ts
4679
+ * // This transaction is going to fail
4680
+ * db.$transaction(async () => {
4681
+ * try {
4682
+ * // imagine it fails because the username is already taken
4683
+ * await db.user.insert({ username: 'taken' });
4684
+ * } catch (err) {
4685
+ * // even though the query is wrapped in a try-catch, the transaction fails anyway
4686
+ * }
4687
+ * });
4688
+ * ```
4689
+ *
4690
+ * You can use `catch` method on a query instead of `try-catch` to surpass the problem.
4691
+ * The following transaction won't fail:
4692
+ *
4693
+ * ```ts
4694
+ * // Transaction succeeds
4695
+ * db.$transaction(async () => {
4696
+ * const result = await db.user.insert({ username: 'taken' }).catch(() => 'failed');
4697
+ *
4698
+ * if (result === 'failed') {
4699
+ * await db.user.insert({ username: 'another' });
4700
+ * }
4701
+ * });
4702
+ * ```
4703
+ *
4704
+ * This is because when using `catch` method, `OrchidORM` will wrap the query with a savepoint.
4705
+ *
4706
+ * Alternatively, you can use the `recoverable()` method:
4707
+ *
4708
+ * ```ts
4709
+ * // Transaction succeeds
4710
+ * db.$transaction(async () => {
4711
+ * try {
4712
+ * await db.user.insert({ username: 'taken' }).recoverable();
4713
+ * } catch (err) {
4714
+ * await db.user.insert({ username: 'another' });
4715
+ * }
4716
+ * });
4717
+ * ```
4718
+ */
4719
+ recoverable() {
4720
+ const q = _clone(this);
4721
+ q.q.catch = true;
4722
+ return q;
4723
+ }
4672
4724
  }
4673
4725
  const runAfterCommit = (afterCommit, result) => {
4674
4726
  queueMicrotask(async () => {
@@ -4797,7 +4849,7 @@ Object.defineProperty(Then.prototype, "then", {
4797
4849
  });
4798
4850
  function maybeWrappedThen(resolve, reject) {
4799
4851
  const { q } = this;
4800
- const shouldCatch = q.catch || !!reject;
4852
+ const shouldCatch = q.catch;
4801
4853
  let beforeActionHooks;
4802
4854
  let afterHooks;
4803
4855
  let afterSaveHooks;
@@ -5267,7 +5319,7 @@ const then = async (q, adapter, trx, beforeHooks, afterHooks, afterSaveHooks, af
5267
5319
  }
5268
5320
  };
5269
5321
  const execQuery = (adapter, method, sql, catchTrx) => {
5270
- const catchingSavepoint = catchTrx ? String(catchTrx.catchI = (catchTrx.catchI || 0) + 1) : void 0;
5322
+ const catchingSavepoint = catchTrx ? `s${catchTrx.catchI = (catchTrx.catchI || 0) + 1}` : void 0;
5271
5323
  return adapter[method](
5272
5324
  sql.text,
5273
5325
  sql.values,