Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | 131x 131x 435x 435x 435x 155x 4x 4x 4x | import type {
AdvisoryLockKey,
Transaction,
TransactionOptions,
} from '../transaction/transaction.ts';
import type { SequelizeConstructor } from './sequelize-constructor.ts';
/**
* Transaction methods of `Sequelize`: `transaction` and `withAdvisoryLock`.
*
* @internal
*/
// oxlint-disable-next-line max-lines-per-function, typescript/explicit-module-boundary-types -- a mixin factory's body is its class (each method is still size-checked); its type can only be inferred.
export const SequelizeTransactions = <TBase extends SequelizeConstructor>(Base: TBase) =>
class TransactionsCapability extends Base {
/**
* Runs a callback in a managed transaction: committed when it resolves, rolled back when it throws. Queries must pass `{ transaction }` unless CLS is enabled with {@link Sequelize.useCLS}.
*
* @param run - Callback receiving the transaction.
* @returns Result of `run`.
*
* @example
* ```ts
* const created = await sequelize.transaction(async (t) => {
* const owner = await User.create({ name: 'Ada' }, { transaction: t });
* return Post.create({ title: 'Hello', userId: owner.id }, { transaction: t });
* });
* ```
*/
transaction<T>(run: (transaction: Transaction) => PromiseLike<T>): Promise<T>;
/**
* Runs a callback in a managed transaction started with the given options (isolation level, read only, settings, savepoint parent).
*
* @param options - Transaction options.
* @param run - Callback receiving the transaction.
* @returns Result of `run`.
*
* @example
* ```ts
* const count = await sequelize.transaction({ readOnly: true }, async (t) => {
* return User.count({ transaction: t });
* });
* ```
*/
transaction<T>(
options: TransactionOptions,
run: (transaction: Transaction) => PromiseLike<T>,
): Promise<T>;
/**
* Begins an unmanaged transaction that you must commit or roll back yourself. With `options.transaction` it becomes a savepoint inside that transaction.
*
* @param options - Transaction options.
* @returns Started transaction.
*
* @example
* ```ts
* const t = await sequelize.transaction({ isolationLevel: Transaction.ISOLATION_LEVELS.SERIALIZABLE });
* try {
* await User.create({ name: 'Ada' }, { transaction: t });
* await t.commit();
* } catch (error) {
* await t.rollback();
* throw error;
* }
* ```
*/
transaction(options?: TransactionOptions): Promise<Transaction>;
async transaction<T>(
optionsOrRun?: TransactionOptions | ((transaction: Transaction) => PromiseLike<T>),
maybeRun?: (transaction: Transaction) => PromiseLike<T>,
): Promise<T | Transaction> {
const options = typeof optionsOrRun === 'function' ? {} : (optionsOrRun ?? {});
const run = typeof optionsOrRun === 'function' ? optionsOrRun : maybeRun;
return run === undefined
? this.internals.begin(options)
: this.internals.managed(options, async (transaction) => run(transaction));
}
/**
* Runs a callback in a managed transaction that first takes a transaction-level advisory lock, so concurrent callers with the same key run one at a time. The lock is released when the transaction ends. PostgreSQL extension.
*
* @postgrelize
* @param key - Lock key (integer or string).
* @param run - Callback receiving the transaction.
* @param options - Transaction options.
* @returns Result of `run`.
*
* @example
* ```ts
* await sequelize.withAdvisoryLock('nightly-report', async (t) => {
* await Invoice.update({ status: 'sent' }, { where: { status: 'ready' }, transaction: t });
* });
* ```
*/
withAdvisoryLock<T>(
key: AdvisoryLockKey,
run: (transaction: Transaction) => Promise<T>,
options: TransactionOptions = {},
): Promise<T> {
return this.transaction(options, async (transaction) => {
await transaction.advisoryLock(key);
return run(transaction);
});
}
};
|