All files / src/transaction cls-namespace.ts

91.66% Statements 11/12
91.66% Branches 11/12
100% Functions 5/5
91.66% Lines 11/12

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                          132x                             10x 10x           1x   9x                 77x                       13x 13x 13x   13x                 14x          
import type { Transaction } from './transaction.ts';
 
/**
 * Continuation-local namespace in the shape of `cls-hooked` / `continuation-local-storage`
 * (the namespace passed to Sequelize v6 `useCLS`). Only these members are used.
 */
export type ClsNamespace = Readonly<{
  bind: (...args: never[]) => unknown;
  run: (callback: (context?: unknown) => unknown) => unknown;
  get: (key: string) => unknown;
  set: (key: string, value: unknown) => unknown;
}>;
 
const KEY = 'transaction';
 
/**
 * Keeps the current transaction under the `transaction` key of a CLS namespace, as Sequelize v6 does:
 * managed transactions run in `namespace.run` and set the key, queries without a `transaction` option
 * read it, and finishing a transaction clears it. Project code can also place a transaction there.
 */
export class NamespaceContext {
  readonly #namespace: ClsNamespace;
 
  /**
   * @param namespace - Namespace given to `Sequelize.useCLS`.
   * @throws Error when it is not a CLS namespace (has no `bind` and `run`), like v6.
   */
  constructor(namespace: unknown) {
    const candidate = namespace as Partial<ClsNamespace> | null | undefined;
    if (
      typeof candidate !== 'object' ||
      candidate === null ||
      typeof candidate.bind !== 'function' ||
      typeof candidate.run !== 'function'
    ) {
      throw new Error('Must provide CLS namespace');
    }
    this.#namespace = candidate as ClsNamespace;
  }
 
  /**
   * Transaction stored in the namespace of the current context.
   *
   * @returns The transaction, or `undefined` when none is stored (or it was cleared).
   */
  current(): Transaction | undefined {
    return (this.#namespace.get(KEY) as Transaction | null | undefined) ?? undefined;
  }
 
  /**
   * Runs `run` in a new namespace context with `transaction` stored in it.
   *
   * @param transaction - Transaction to store.
   * @param run - Callback.
   * @returns Result of `run`.
   */
  run<T>(transaction: Transaction, run: () => Promise<T>): Promise<T> {
    let result: Promise<T> | undefined;
    this.#namespace.run(() => {
      this.#namespace.set(KEY, transaction);
      result = run();
    });
    return result as Promise<T>;
  }
 
  /**
   * Clears the stored transaction when it is the one that finished (not for savepoints, as in v6).
   *
   * @param transaction - Finished transaction.
   */
  release(transaction: Transaction): void {
    Iif (transaction.parent === undefined && this.#namespace.get(KEY) === transaction) {
      this.#namespace.set(KEY, null);
    }
  }
}