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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | 132x 559x 149x 144x 344x 162x 162x 141x 139x 23x 20x 23x 132x 132x 132x 13x 13x 13x 55285x 55667x 447x 344x 132x | import { AsyncLocalStorage } from 'node:async_hooks';
import { NamespaceContext } from './cls-namespace.ts';
import type { Transaction } from './transaction.ts';
/** Continuation-local storage of the current transaction (`AsyncLocalStorage`, used by `Sequelize.useCLS`). */
export class TransactionContext {
readonly #storage = new AsyncLocalStorage<Transaction>();
/**
* Transaction of the current async context.
*
* @returns The transaction, or `undefined` outside `run`.
*/
current(): Transaction | undefined {
return this.#storage.getStore();
}
/**
* Runs `run` with `transaction` as the current transaction.
*
* @param transaction - Transaction to make current.
* @param run - Callback.
* @returns Result of `run`.
*/
run<T>(transaction: Transaction, run: () => Promise<T>): Promise<T> {
return this.#storage.run(transaction, run);
}
}
/** Runs managed transactions: begins, runs the callback, commits on success and rolls back on error. */
export class TransactionRunner {
readonly #context: Pick<TransactionContext, 'run'>;
/**
* @param context - Context the transaction is made current in while the callback runs.
*/
constructor(context: Pick<TransactionContext, 'run'>) {
this.#context = context;
}
/**
* Runs `run` inside `transaction`.
*
* @param transaction - Transaction, not started yet.
* @param run - Callback receiving the transaction.
* @returns Result of `run`.
*/
async run<T>(
transaction: Transaction,
run: (transaction: Transaction) => Promise<T>,
): Promise<T> {
await transaction.begin();
try {
const result = await this.#context.run(transaction, () => run(transaction));
await transaction.commit();
return result;
} catch (error) {
if (transaction.finished === undefined) {
await transaction.rollback().catch(() => undefined);
}
throw error;
}
}
}
/**
* Transaction scope shared by every `Sequelize` instance: the `useCLS` switch, the current
* transaction of the async context and the managed-transaction runner.
*/
export class TransactionScope {
readonly #context = new TransactionContext();
#namespace: NamespaceContext | undefined;
#runner = new TransactionRunner(this.#context);
#cls = false;
/**
* Enables continuation-local transactions.
*
* @param namespace - CLS namespace (v6 style); `AsyncLocalStorage` is used without one.
* @throws Error when `namespace` is given but is not a CLS namespace.
*/
enableCls(namespace?: unknown): void {
this.#namespace = namespace === undefined ? undefined : new NamespaceContext(namespace);
this.#runner = new TransactionRunner(this.#namespace ?? this.#context);
this.#cls = true;
}
/**
* Transaction that queries without a `transaction` option use.
*
* @returns The transaction, or `undefined` when CLS is off or outside a managed transaction.
*/
active(): Transaction | undefined {
return this.#namespace?.current() ?? this.implicitParent();
}
/**
* Parent of a new transaction started without `transaction`: with `AsyncLocalStorage` CLS the
* enclosing managed transaction (savepoint); with a v6 namespace none, as in v6.
*
* @returns The enclosing transaction, if any.
*/
implicitParent(): Transaction | undefined {
return this.#cls && this.#namespace === undefined ? this.#context.current() : undefined;
}
/**
* Called when a transaction releases its connection.
*
* @param transaction - Finished transaction.
*/
release(transaction: Transaction): void {
this.#namespace?.release(transaction);
}
/**
* Runs `run` inside `transaction`, committing or rolling back.
*
* @param transaction - Transaction, not started yet.
* @param run - Callback receiving the transaction.
* @returns Result of `run`.
*/
run<T>(transaction: Transaction, run: (transaction: Transaction) => Promise<T>): Promise<T> {
return this.#runner.run(transaction, run);
}
}
/** The scope used by all `Sequelize` instances. */
export const transactionScope = new TransactionScope();
|