All files / src/model returning-old.ts

100% Statements 17/17
100% Branches 10/10
100% Functions 8/8
100% Lines 16/16

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            131x               261x   261x                   131x 131x                             397x 389x   8x 1x   7x                           8x         28x                                       14x   90x 41x   14x          
import { SqlAst, type ExprNode } from '../sql/ast.ts';
import type { AttributeColumns } from './attribute-columns.ts';
import type { ModelDefinition } from './model-definition.ts';
import type { RowMapper } from './row-mapper.ts';
import type { ModelRuntime } from './runtime.ts';
 
const ast = new SqlAst();
 
/**
 * `returningOld` option (PostgreSQL 18+ `RETURNING WITH (OLD AS ...)`): checks the server
 * version, adds the pre-statement row to a write's RETURNING list and reads it back.
 */
export class ReturningOld {
  /** Alias of the pre-statement row in `RETURNING WITH (OLD AS ...)`. */
  static readonly ALIAS = '__pgz_old';
  /** Prefix of returned columns carrying pre-statement values. */
  static readonly PREFIX = `${ReturningOld.ALIAS}.`;
 
  readonly #columns: AttributeColumns;
  readonly #rows: RowMapper;
 
  /**
   * @param columns - Stored columns of a model.
   * @param rows - Parses returned values.
   */
  constructor(columns: AttributeColumns, rows: RowMapper) {
    this.#columns = columns;
    this.#rows = rows;
  }
 
  /**
   * Whether the write should return pre-statement values.
   *
   * @param runtime - Runtime of the model (server version).
   * @param options - Write options.
   * @returns True with `returningOld: true`.
   * @throws Error on PostgreSQL older than 18.
   */
  async enabled(
    runtime: Pick<ModelRuntime, 'serverVersion'>,
    options: Readonly<{ returningOld?: boolean }>,
  ): Promise<boolean> {
    if (options.returningOld !== true) {
      return false;
    }
    if ((await runtime.serverVersion()) < 180000) {
      throw new Error('returningOld requires PostgreSQL 18 or newer');
    }
    return true;
  }
 
  /**
   * Adds the pre-statement row (all stored columns) to the RETURNING list of a write.
   *
   * @param definition - Model definition.
   * @param node - INSERT / UPDATE / MERGE node.
   * @returns Node with `returningOld`.
   */
  node<N extends Readonly<{ returning?: readonly ExprNode[] }>>(
    definition: ModelDefinition,
    node: N,
  ): N & Readonly<{ returningOld: string }> {
    return {
      ...node,
      returningOld: ReturningOld.ALIAS,
      returning: [
        ...(node.returning ?? []),
        ...this.#columns.stored(definition).map((attribute): ExprNode => ({
          kind: 'alias',
          expr: ast.ident(ReturningOld.ALIAS, attribute.field),
          alias: `${ReturningOld.PREFIX}${attribute.fieldName}`,
        })),
      ],
    };
  }
 
  /**
   * Pre-statement values of a returned row.
   *
   * @param definition - Model definition.
   * @param row - Returned row.
   * @returns Attribute values, or `null` when the row did not exist before (inserted).
   */
  previous(
    definition: ModelDefinition,
    row: Readonly<Record<string, unknown>>,
  ): Readonly<Record<string, unknown>> | null {
    const old = Object.fromEntries(
      Object.entries(row)
        .filter(([key]) => key.startsWith(ReturningOld.PREFIX))
        .map(([key, value]) => [key.slice(ReturningOld.PREFIX.length), value]),
    );
    return definition.primaryKeys.every((name) => old[name] === null || old[name] === undefined)
      ? null
      : this.#rows.parse(definition, old);
  }
}