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.mjs CHANGED
@@ -4576,7 +4576,7 @@ class Transaction {
4576
4576
  return result;
4577
4577
  } else {
4578
4578
  try {
4579
- sql.text = `SAVEPOINT "${transactionId}"`;
4579
+ sql.text = `SAVEPOINT "t${transactionId}"`;
4580
4580
  if (log) logData = log.beforeQuery(sql);
4581
4581
  const { adapter } = trx;
4582
4582
  await adapter.arrays(sql.text, sql.values);
@@ -4584,13 +4584,13 @@ class Transaction {
4584
4584
  try {
4585
4585
  result = await callback(adapter);
4586
4586
  } catch (err) {
4587
- sql.text = `ROLLBACK TO SAVEPOINT "${transactionId}"`;
4587
+ sql.text = `ROLLBACK TO SAVEPOINT "t${transactionId}"`;
4588
4588
  if (log) logData = log.beforeQuery(sql);
4589
4589
  await adapter.arrays(sql.text, sql.values);
4590
4590
  if (log) log.afterQuery(sql, logData);
4591
4591
  throw err;
4592
4592
  }
4593
- sql.text = `RELEASE SAVEPOINT "${transactionId}"`;
4593
+ sql.text = `RELEASE SAVEPOINT "t${transactionId}"`;
4594
4594
  if (log) logData = log.beforeQuery(sql);
4595
4595
  await adapter.arrays(sql.text, sql.values);
4596
4596
  if (log) log.afterQuery(sql, logData);
@@ -4667,6 +4667,58 @@ class Transaction {
4667
4667
  queueMicrotask(hook);
4668
4668
  }
4669
4669
  }
4670
+ /**
4671
+ * Whenever a query in a transaction fails, the transaction is transitioned to a failed state, no further queries are possible.
4672
+ *
4673
+ * Use [catchUniqueError](/guide/error-handling.html#catchuniqueerror) to handle uniqueness errors,
4674
+ * the following examples do not use it to illustrate error handling.
4675
+ *
4676
+ * ```ts
4677
+ * // This transaction is going to fail
4678
+ * db.$transaction(async () => {
4679
+ * try {
4680
+ * // imagine it fails because the username is already taken
4681
+ * await db.user.insert({ username: 'taken' });
4682
+ * } catch (err) {
4683
+ * // even though the query is wrapped in a try-catch, the transaction fails anyway
4684
+ * }
4685
+ * });
4686
+ * ```
4687
+ *
4688
+ * You can use `catch` method on a query instead of `try-catch` to surpass the problem.
4689
+ * The following transaction won't fail:
4690
+ *
4691
+ * ```ts
4692
+ * // Transaction succeeds
4693
+ * db.$transaction(async () => {
4694
+ * const result = await db.user.insert({ username: 'taken' }).catch(() => 'failed');
4695
+ *
4696
+ * if (result === 'failed') {
4697
+ * await db.user.insert({ username: 'another' });
4698
+ * }
4699
+ * });
4700
+ * ```
4701
+ *
4702
+ * This is because when using `catch` method, `OrchidORM` will wrap the query with a savepoint.
4703
+ *
4704
+ * Alternatively, you can use the `recoverable()` method:
4705
+ *
4706
+ * ```ts
4707
+ * // Transaction succeeds
4708
+ * db.$transaction(async () => {
4709
+ * try {
4710
+ * await db.user.insert({ username: 'taken' }).recoverable();
4711
+ * } catch (err) {
4712
+ * await db.user.insert({ username: 'another' });
4713
+ * }
4714
+ * });
4715
+ * ```
4716
+ */
4717
+ recoverable() {
4718
+ const q = _clone(this);
4719
+ q.q.catch = true;
4720
+ return q;
4721
+ }
4670
4722
  }
4671
4723
  const runAfterCommit = (afterCommit, result) => {
4672
4724
  queueMicrotask(async () => {
@@ -4795,7 +4847,7 @@ Object.defineProperty(Then.prototype, "then", {
4795
4847
  });
4796
4848
  function maybeWrappedThen(resolve, reject) {
4797
4849
  const { q } = this;
4798
- const shouldCatch = q.catch || !!reject;
4850
+ const shouldCatch = q.catch;
4799
4851
  let beforeActionHooks;
4800
4852
  let afterHooks;
4801
4853
  let afterSaveHooks;
@@ -5265,7 +5317,7 @@ const then = async (q, adapter, trx, beforeHooks, afterHooks, afterSaveHooks, af
5265
5317
  }
5266
5318
  };
5267
5319
  const execQuery = (adapter, method, sql, catchTrx) => {
5268
- const catchingSavepoint = catchTrx ? String(catchTrx.catchI = (catchTrx.catchI || 0) + 1) : void 0;
5320
+ const catchingSavepoint = catchTrx ? `s${catchTrx.catchI = (catchTrx.catchI || 0) + 1}` : void 0;
5269
5321
  return adapter[method](
5270
5322
  sql.text,
5271
5323
  sql.values,