All files / src/model merge-options.ts

0% Statements 0/0
0% Branches 0/0
0% Functions 0/0
0% Lines 0/0

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                                                                                                                                                         
import type { Subquery } from '../expressions/methods.ts';
import type { WhereOptions } from './types.ts';
 
/**
 * One `WHEN MATCHED` / `WHEN NOT MATCHED BY SOURCE` action of `merge`.
 *
 * Set exactly one of `update`, `delete` or `nothing`.
 *
 * @example
 * ```ts
 * const action: MergeMatchedAction<Product> = { where: { active: true }, update: { price: col('source.price') } };
 * ```
 */
export interface MergeMatchedAction<T> {
  /** Extra condition (`WHEN MATCHED AND ...`). */
  readonly where?: WhereOptions<T> | unknown;
  /** Update the target row with these values / expressions (`col('source.attr')`). */
  readonly update?: Partial<Record<keyof T & string, unknown>>;
  /** Delete the target row. */
  readonly delete?: true;
  /** Do nothing. */
  readonly nothing?: true;
}
 
/**
 * `WHEN NOT MATCHED` action of `merge`.
 *
 * Set `insert` or `nothing`.
 *
 * @example
 * ```ts
 * const action: MergeInsertAction<Product> = { insert: { sku: col('source.sku'), price: 0 } };
 * ```
 */
export interface MergeInsertAction<T> {
  /** Extra condition (`WHEN NOT MATCHED AND ...`); may reference only source columns. */
  readonly where?: unknown;
  /** Values to insert: `true` copies same-named source columns, or a map of values / expressions. */
  readonly insert?: true | Partial<Record<keyof T & string, unknown>>;
  /** Do nothing. */
  readonly nothing?: true;
}
 
/**
 * Options of `Model.merge`, which runs a `MERGE` statement. PostgreSQL extension. PostgreSQL 15+ (RETURNING: 17+).
 *
 * Rows of the model table are the target; `source` rows or a subquery are the source.
 *
 * @postgrelize
 * @see {@link Model.merge}
 *
 * @example
 * ```ts
 * const opts: MergeOptions<Product> = {
 *   source: [{ sku: 'a1', price: 10 }],
 *   on: { sku: col('source.sku') },
 *   whenMatched: { update: { price: col('source.price') } },
 *   whenNotMatched: { insert: true },
 * };
 * ```
 */
export interface MergeOptions<T> {
  /** Source rows (plain objects) or a subquery. */
  readonly source: readonly Readonly<Record<string, unknown>>[] | Subquery;
  /** Alias of the source relation.
   * @defaultValue 'source' */
  readonly as?: string;
  /** Join condition between target (model alias) and source (`col('source.attr')`). */
  readonly on: WhereOptions<T> | unknown;
  /** Actions for matched rows, applied in order. */
  readonly whenMatched?: MergeMatchedAction<T> | readonly MergeMatchedAction<T>[];
  /** Actions for source rows without a target row. */
  readonly whenNotMatched?: MergeInsertAction<T> | readonly MergeInsertAction<T>[];
  /** Actions for target rows without a source row (PostgreSQL 17+). */
  readonly whenNotMatchedBySource?: MergeMatchedAction<T> | readonly MergeMatchedAction<T>[];
}