pqb 0.40.7 → 0.40.8

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
@@ -3395,6 +3395,32 @@ class Transaction {
3395
3395
  }
3396
3396
  }
3397
3397
  }
3398
+ /**
3399
+ * Use the `$ensureTransaction` when you want to ensure the sequence of queries is running in a transaction, but there is no need for Postgres [savepoints](https://www.postgresql.org/docs/current/sql-savepoint.html).
3400
+ *
3401
+ * ```ts
3402
+ * async function updateUserBalance(userId: string, amount: number) {
3403
+ * await db.$ensureTransaction(async () => {
3404
+ * await db.transfer.create({ userId, amount })
3405
+ * await db.user.find(userId).increment({ balance: amount })
3406
+ * })
3407
+ * }
3408
+ *
3409
+ * async function saveDeposit(userId: string, deposit: { ... }) {
3410
+ * await db.$ensureTransaction(async () => {
3411
+ * await db.deposit.create(deposit)
3412
+ * // transaction in updateUserBalance won't be started
3413
+ * await updateUserBalance(userId, deposit.amount)
3414
+ * })
3415
+ * }
3416
+ * ```
3417
+ */
3418
+ ensureTransaction(cb) {
3419
+ const trx = this.internal.transactionStorage.getStore();
3420
+ if (trx)
3421
+ return cb();
3422
+ return Transaction.prototype.transaction.call(this, cb);
3423
+ }
3398
3424
  }
3399
3425
  const runAfterCommit = async (afterCommit, result) => {
3400
3426
  if (afterCommit) {