All files / src/model row-plan.ts

100% Statements 10/10
100% Branches 6/6
100% Functions 2/2
100% Lines 10/10

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                            262x                 107950x 107950x 942x 942x   107950x                         4631x     4631x 4631x 4631x      
import type { ModelDefinition } from './model-definition.ts';
 
/** How one driver column is stored: the attribute it maps to and its data type parser. */
export type ColumnPlan = Readonly<{
  name: string;
  parse: ((value: unknown) => unknown) | undefined;
}>;
 
/**
 * Per-definition cache of column plans. A driver row is converted once per row and column, so the
 * attribute lookup (`attributes`, `fieldToAttribute`, type parser) is resolved once per column name
 * instead. Definitions are replaced, never mutated, so the cache is keyed by definition identity.
 */
export class RowPlans {
  readonly #plans = new WeakMap<ModelDefinition, Map<string, ColumnPlan>>();
 
  /**
   * Plans of a definition, filled lazily by {@link RowPlans.plan}.
   *
   * @param definition - Model definition.
   * @returns Column plans by driver column name.
   */
  columns(definition: ModelDefinition): Map<string, ColumnPlan> {
    let columns = this.#plans.get(definition);
    if (columns === undefined) {
      columns = new Map();
      this.#plans.set(definition, columns);
    }
    return columns;
  }
 
  /**
   * Resolves and remembers the plan of a column. Columns that match no attribute are kept as they
   * are, without a parser.
   *
   * @param definition - Model definition.
   * @param columns - The definition's plans.
   * @param key - Driver column name.
   * @returns The plan.
   */
  plan(definition: ModelDefinition, columns: Map<string, ColumnPlan>, key: string): ColumnPlan {
    const name = definition.attributes.has(key)
      ? key
      : (definition.fieldToAttribute.get(key) ?? key);
    const plan = { name, parse: definition.attributes.get(name)?.type.parse };
    columns.set(key, plan);
    return plan;
  }
}