All files / src/model/operations increment-operation.ts

100% Statements 13/13
75% Branches 3/4
100% Functions 4/4
100% Lines 12/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 78 79 80 81 82                                                                              131x                                   6x 6x 6x 6x 6x 6x                 6x 6x 6x 6x 6x            
import type { SqlCompiler } from '../../sql/compile.ts';
import type { AttributeColumns } from '../attribute-columns.ts';
import type { ModelDefinition } from '../model-definition.ts';
import type { ModelRegistry } from '../model-registry.ts';
import type { ModelValues } from '../model-values.ts';
import type {
  AnyModel,
  AttributeAmounts,
  IncrementOptions,
  ModelStatic,
  Values,
} from '../options.ts';
import type { RowMapper } from '../row-mapper.ts';
import type { UpdateQueryBuilder } from '../update-query-builder.ts';
 
/** Collaborators of {@link IncrementOperation}. */
export type IncrementOperationServices = Readonly<{
  /** Model state. */
  registry: ModelRegistry;
  /** Value rules. */
  values: ModelValues;
  /** SET items. */
  columns: AttributeColumns;
  /** UPDATE builder. */
  updates: UpdateQueryBuilder;
  /** Instances of returned rows. */
  rows: RowMapper;
  /** SQL compiler. */
  compiler: SqlCompiler;
}>;
 
/** `Model.increment` / `Model.decrement`: `SET x = x ± by` (and `updatedAt`) on matching rows. */
export class IncrementOperation {
  readonly #services: IncrementOperationServices;
 
  /**
   * @param services - Collaborators.
   */
  constructor(services: IncrementOperationServices) {
    this.#services = services;
  }
 
  /**
   * Increments (`sign` 1) or decrements (`sign` -1) attributes of matching rows.
   *
   * @param model - Model class.
   * @param fields - Attribute name(s), or a map of attribute to amount.
   * @param options - Increment options.
   * @param sign - `1` or `-1`.
   * @returns Updated instances and the affected row count.
   */
  async change<M extends AnyModel>(
    model: ModelStatic<M>,
    fields: AttributeAmounts<Values>,
    options: IncrementOptions,
    sign: 1 | -1,
  ): Promise<[M[], number]> {
    const { definition, runtime } = this.#services.registry.state(model);
    const { compiler, rows, updates } = this.#services;
    const set = this.#setItems(definition, fields, options, sign);
    const node = updates.build(definition, set, options.where ?? {}, { returning: true });
    const result = await runtime.execute(compiler.compile(node), options);
    return [result.rows.map((row) => rows.instance(model, row)), result.rowCount];
  }
 
  #setItems(
    definition: ModelDefinition,
    fields: AttributeAmounts<Values>,
    options: IncrementOptions,
    sign: 1 | -1,
  ) {
    const { columns, updates, values } = this.#services;
    const amounts = values.amounts(fields, options.by ?? 1);
    const silent = options.silent === true;
    const timestamp = values.withTimestamps(definition, {}, new Date(), false, silent);
    return [
      ...updates.increment(definition, amounts, sign),
      ...columns.setItems(definition, timestamp),
    ];
  }
}