All files / src/model cte-builder.ts

100% Statements 33/33
100% Branches 45/45
100% Functions 14/14
100% Lines 32/32

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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247                                                                                                                                                                                                                    131x 539x                         131x                   7848x 7848x 7572x   276x 276x 263x                               7848x             1031x         1031x                             7848x 7848x 7848x                           7138x 263x   7138x       276x 276x                 276x 276x 229x 1x   228x   47x                       275x 1x   274x 275x 11x   263x                
import { Subquery, WriteStatement } from '../expressions/methods.ts';
import type { CteNode, FromItem, QueryNode, SelectNode, WithClause } from '../sql/ast.ts';
import type { JoinSourceBuilder, JoinSources } from './join-source-builder.ts';
import { ModelDefinition } from './model-definition.ts';
 
/**
 * Common table expression of the `with` option. PostgreSQL extension.
 *
 * @example
 * ```ts
 * const cte: CteDefinition = {
 *   query: Task.subquery({ attributes: ['id'], where: { parentId: null } }),
 *   unionAll: true,
 *   materialized: false,
 * };
 * ```
 */
export type CteDefinition = Readonly<{
  /** Non-recursive part, or a data-modifying statement with `returning` (never with `recursive`). */
  query: Subquery | WriteStatement;
  /** Recursive part (makes the whole clause `WITH RECURSIVE`); reference the CTE through `from`. */
  recursive?: Subquery;
  /** `UNION ALL` (default) or `UNION` between `query` and `recursive`. */
  unionAll?: boolean;
  /** `MATERIALIZED` / `NOT MATERIALIZED` hint. */
  materialized?: boolean;
  /** `SEARCH … FIRST BY … SET …`: adds a column that orders the rows breadth or depth first. Needs `recursive`. */
  search?: CteSearchOptions;
  /** `CYCLE … SET … [USING …]`: marks rows that revisit a key, and stops the recursion there. Needs `recursive`. */
  cycle?: CteCycleOptions;
}>;
 
/**
 * `search` of a recursive CTE definition. PostgreSQL extension.
 *
 * @postgrelize
 * @example
 * ```ts
 * const search: CteSearchOptions = { by: ['id'], mode: 'breadth', set: 'ordercol' };
 * ```
 */
export type CteSearchOptions = Readonly<{
  /** Columns of the CTE that define the order. */
  by: readonly string[];
  /** Traversal order.
   * @defaultValue 'depth' */
  mode?: 'breadth' | 'depth';
  /** Name of the added ordering column. */
  set: string;
}>;
 
/**
 * `cycle` of a recursive CTE definition. PostgreSQL extension.
 *
 * @postgrelize
 * @example
 * ```ts
 * const cycle: CteCycleOptions = { columns: ['id'], set: 'is_cycle', using: 'path' };
 * ```
 */
export type CteCycleOptions = Readonly<{
  /** Columns that identify a visited row. */
  columns: readonly string[];
  /** Name of the added boolean column that is true on the row that closes a cycle. */
  set: string;
  /** Name of the added array column with the path of visited keys (required: PostgreSQL 16 and 17 need it). */
  using: string;
}>;
 
/** `with` option: CTEs by name. */
export type WithOption = Readonly<Record<string, Subquery | WriteStatement | CteDefinition>>;
 
/**
 * CTE-related find options. PostgreSQL extension.
 *
 * Combined into {@link FindOptions} and {@link CountOptions}.
 *
 * @postgrelize
 * @example
 * ```ts
 * const opts: CteFindOptions = {
 *   with: { recent: { query: Post.subquery({ where: { published: true } }) } },
 *   source: 'recent',
 * };
 * ```
 */
export type CteFindOptions = Readonly<{
  /**
   * Common table expressions (`WITH [RECURSIVE] ...`). PostgreSQL extension.
   * @postgrelize
   */
  with?: WithOption;
  /**
   * Read model rows from this relation instead of the model table: a CTE or table name, or a
   * subquery such as a set operation (`a.union(b)`), aliased with the model name. Columns are
   * named like the attributes. PostgreSQL extension.
   * @postgrelize
   */
  source?: string | Subquery;
  /**
   * Extra relations (`FROM "Model", "alias"`), keyed by alias; correlate them in `where`. PostgreSQL extension.
   * @postgrelize
   */
  from?: JoinSources;
}>;
 
const isDefinition = (value: Subquery | WriteStatement | CteDefinition): value is CteDefinition =>
  'query' in value;
 
/**
 * Resolves the CTE-related find options (PostgreSQL extension): the `WITH [RECURSIVE]` clause,
 * extra FROM relations, and the definition used when rows come from a `source` relation.
 */
export class CteBuilder {
  readonly #joins: JoinSourceBuilder;
 
  /**
   * @param joins - Converts `from` relations into FROM items.
   */
  constructor(joins: JoinSourceBuilder) {
    this.#joins = joins;
  }
 
  /**
   * Builds the `WITH` clause of the `with` option.
   *
   * @param option - CTEs by name.
   * @returns WITH clause, or `undefined` without CTEs.
   */
  withClause(option: WithOption | undefined): WithClause | undefined {
    const entries = Object.entries(option ?? {});
    if (entries.length === 0) {
      return undefined;
    }
    const ctes = entries.map(([name, value]) => CteBuilder.#cte(name, value));
    return {
      recursive: entries.some(([, value]) => isDefinition(value) && value.recursive !== undefined),
      ctes,
    };
  }
 
  /**
   * Definition used when rows come from `source`: same attributes, columns named like attributes.
   *
   * @param definition - Model definition.
   * @param source - `source` option.
   * @returns The definition itself without `source`.
   */
  sourceDefinition(
    definition: ModelDefinition,
    source: string | Subquery | undefined,
  ): ModelDefinition {
    return source === undefined
      ? definition
      : new ModelDefinition({
          ...definition,
          tableName: typeof source === 'string' ? source : definition.tableName,
          schema: undefined,
          attributes: new Map(
            [...definition.attributes].map(([name, attribute]) => [
              name,
              { ...attribute, field: name },
            ]),
          ),
          fieldToAttribute: new Map([...definition.attributes.keys()].map((name) => [name, name])),
        });
  }
 
  /**
   * Resolved CTE options for the select builder.
   *
   * @param options - Find options.
   * @returns `withClause` / `extraFrom` when present.
   */
  selectOptions(options: CteFindOptions): Readonly<{
    withClause?: WithClause;
    extraFrom?: readonly FromItem[];
    fromQuery?: QueryNode;
  }> {
    const withClause = this.withClause(options.with);
    const extraFrom = this.#joins.build(options.from);
    return {
      ...(withClause === undefined ? {} : { withClause }),
      ...(extraFrom.length === 0 ? {} : { extraFrom }),
      ...(options.source instanceof Subquery ? { fromQuery: options.source.build() } : {}),
    };
  }
 
  /**
   * Route of a statement: `'write'` when a CTE modifies data, so it never goes to a replica.
   *
   * @param select - Built SELECT.
   * @returns `'read'` or `'write'`.
   */
  route(select: SelectNode): 'read' | 'write' {
    const modifying = select.with?.ctes.some(
      (cte) => cte.query.kind !== 'select' && cte.query.kind !== 'setop',
    );
    return modifying === true ? 'write' : 'read';
  }
 
  static #cte(name: string, value: Subquery | WriteStatement | CteDefinition): CteNode {
    const cte: CteDefinition = isDefinition(value) ? value : { query: value };
    return {
      name,
      query: CteBuilder.#body(name, cte),
      ...(cte.materialized === undefined ? {} : { materialized: cte.materialized }),
      ...CteBuilder.#extras(name, cte),
    };
  }
 
  static #body(name: string, cte: CteDefinition): CteNode['query'] {
    const base = cte.query.build();
    if (cte.query instanceof WriteStatement) {
      if (cte.recursive !== undefined) {
        throw new Error(`CTE "${name}": a data-modifying statement cannot be recursive`);
      }
      return base;
    }
    return cte.recursive === undefined
      ? base
      : {
          kind: 'setop',
          op: 'UNION',
          left: base as QueryNode,
          right: cte.recursive.build(),
          all: cte.unionAll !== false,
        };
  }
 
  static #extras(name: string, cte: CteDefinition): Pick<CteNode, 'search' | 'cycle'> {
    if ((cte.search !== undefined || cte.cycle !== undefined) && cte.recursive === undefined) {
      throw new Error(`CTE "${name}": search and cycle need a recursive part`);
    }
    const mode = (cte.search?.mode ?? 'depth').toUpperCase();
    if (mode !== 'DEPTH' && mode !== 'BREADTH') {
      throw new Error(`Invalid CTE search mode: ${String(cte.search?.mode)}`);
    }
    return {
      ...(cte.search === undefined
        ? {}
        : { search: { mode, by: cte.search.by, set: cte.search.set } }),
      ...(cte.cycle === undefined ? {} : { cycle: cte.cycle }),
    };
  }
}