All files / src/model write-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 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                                                                                                                                                                                                                                                                                                                                                                                                                       
import type { IncludeOption } from '../associations/include.ts';
import type { OnConflictOptions } from './on-conflict-builder.ts';
import type { Values } from './options.ts';
import type { QueryOptionsBase } from './runtime.ts';
import type { WhereOptions } from './types.ts';
 
/**
 * Options of `create` and instance `save`.
 *
 * Runs `beforeSave` / `beforeCreate` / `beforeUpdate` hooks (and their `after` counterparts) unless `hooks` is `false`.
 *
 * @see {@link Model.create}
 *
 * @example
 * ```ts
 * const opts: SaveOptions<User> = { fields: ['name', 'email'], validate: true, transaction };
 * await user.save(opts);
 * ```
 */
// oxlint-disable-next-line typescript/no-explicit-any -- v6 declares `SaveOptions<TModelAttributes = any>`.
export type SaveOptions<T = any> = QueryOptionsBase & {
  /** Attributes to save; others are ignored. Default: all changed attributes. Mutable so hooks can adjust it, as in v6. */
  // oxlint-disable-next-line typescript/no-explicit-any -- an `any` model accepts any field name, like v6
  fields?: readonly (0 extends 1 & T ? any : keyof T & string)[];
} & {
  /** Do not update `updatedAt`.
   * @defaultValue false */
  silent?: boolean;
  /** Return all columns after insert/update; PostgreSQL always uses `RETURNING`, so this is accepted for v6 compatibility.
   * @defaultValue true */
  returning?: boolean;
  /** Run lifecycle hooks.
   * @defaultValue true */
  hooks?: boolean;
  /** Run validations.
   * @defaultValue true (`false` for `bulkCreate`) */
  validate?: boolean;
  /**
   * `OVERRIDING SYSTEM VALUE` / `OVERRIDING USER VALUE` for identity columns. PostgreSQL extension.
   * @postgrelize
   */
  overriding?: 'system' | 'user';
};
 
/**
 * Options of `create`.
 *
 * Extends {@link SaveOptions}.
 *
 * @see {@link Model.create}
 *
 * @example
 * ```ts
 * const post = await Post.create(
 *   { title: 'Hello', author: { name: 'Ann' } },
 *   { include: [{ association: 'author' }], transaction },
 * );
 * ```
 */
export type CreateOptions<T = Values> = SaveOptions<T> & {
  /**
   * Advanced `ON CONFLICT` handling. PostgreSQL extension.
   * @postgrelize
   */
  onConflict?: OnConflictOptions<T>;
  /** Create associated rows from nested values (Sequelize v6). */
  include?: IncludeOption;
};
 
/**
 * Options of `upsert`.
 *
 * Extends {@link SaveOptions}. With `onConflict` the result of `upsert` becomes `[instance, created]`.
 *
 * @see {@link Model.upsert}
 *
 * @example
 * ```ts
 * const [user] = await User.upsert(
 *   { email: 'a@example.com', name: 'Ann' },
 *   { conflictFields: ['email'] },
 * );
 * ```
 */
export type UpsertOptions<T = Values> = SaveOptions<T> & {
  /** Conflict target columns. Default: unique key containing a given attribute, else the primary key. */
  conflictFields?: readonly string[];
  /**
   * Advanced `ON CONFLICT` handling; the result becomes `[instance, created]`. PostgreSQL extension.
   * @postgrelize
   */
  onConflict?: OnConflictOptions<T>;
  /**
   * `OVERRIDING SYSTEM VALUE` / `OVERRIDING USER VALUE` for identity columns.
   * @postgrelize
   */
  overriding?: 'system' | 'user';
};
 
/**
 * Options of `Model.insert`. PostgreSQL extension.
 *
 * Rows are inserted with a single `INSERT` and no per-row save hooks; only the bulk-create hooks run.
 *
 * @postgrelize
 * @see {@link Model.insert}
 *
 * @example
 * ```ts
 * const opts: InsertOptions<User> = {
 *   onConflict: { target: ['email'], action: 'nothing' },
 *   hooks: false,
 * };
 * const rows = await User.insert([{ email: 'a@example.com' }], opts);
 * ```
 */
export type InsertOptions<T = Values> = QueryOptionsBase & {
  /** Attributes to insert; others are ignored. Default: all given attributes. */
  fields?: readonly (keyof T & string)[];
  /** Advanced `ON CONFLICT` handling. */
  onConflict?: OnConflictOptions<T>;
  /** Run `beforeBulkCreate` / `afterBulkCreate` hooks.
   * @defaultValue true */
  hooks?: boolean;
  /** Validate rows before inserting.
   * @defaultValue false */
  validate?: boolean;
  /** `OVERRIDING SYSTEM VALUE` / `OVERRIDING USER VALUE` for identity columns. */
  overriding?: 'system' | 'user';
  /**
   * Return the pre-statement row values as `instance.oldValues`. PostgreSQL extension, needs PostgreSQL 18+.
   * @postgrelize
   */
  returningOld?: boolean;
};
 
/**
 * Options of `Model.insertFrom`, which inserts the result of a subquery (`INSERT ... SELECT`). PostgreSQL extension.
 *
 * @postgrelize
 * @see {@link Model.insertFrom}
 *
 * @example
 * ```ts
 * const opts: InsertFromOptions<Task> = {
 *   columns: ['title', 'projectId'],
 *   onConflict: { action: 'nothing' },
 * };
 * ```
 */
export type InsertFromOptions<T = Values> = QueryOptionsBase &
  Readonly<{
    /** Target attributes, in the order of the subquery columns. */
    columns: readonly (keyof T & string)[];
    /** Advanced `ON CONFLICT` handling. */
    onConflict?: OnConflictOptions<T>;
    /** Return the pre-statement row values as `instance.oldValues`. PostgreSQL 18+. */
    returningOld?: boolean;
  }>;
 
/**
 * Options of `increment` and `decrement`.
 *
 * Runs one `UPDATE ... SET col = col + n` and updates `updatedAt` unless `silent`.
 *
 * @see {@link Model.increment}
 *
 * @example
 * ```ts
 * await Product.increment('stock', { by: 5, where: { id: 1 } });
 * const opts: IncrementOptions<Product> = { by: 2, silent: true, transaction };
 * const product = await Product.findByPk(1);
 * await product!.decrement('stock', opts);
 * ```
 */
export type IncrementOptions<T = Values> = QueryOptionsBase &
  Readonly<{
    /** Rows to change; required for the static form, ignored on instances. */
    where?: WhereOptions<T>;
    /** Amount to add (`increment`) or subtract (`decrement`).
     * @defaultValue 1 */
    by?: number;
    /** Do not update `updatedAt`.
     * @defaultValue false */
    silent?: boolean;
  }>;
 
/** Attribute name(s) or a map of attribute name to amount for `increment` / `decrement`. */
export type AttributeAmounts<T> =
  | (keyof T & string)
  | readonly (keyof T & string)[]
  | Partial<Record<keyof T & string, number>>;
 
/**
 * Options of the instance updates that association accessors run (Sequelize v6 `InstanceUpdateOptions`): the {@link SaveOptions}.
 *
 * @example
 * ```ts
 * const options: InstanceUpdateOptions<User> = { fields: ['name'], transaction };
 * await user.update({ name: 'Ann' }, options);
 * ```
 */
export type InstanceUpdateOptions<T = Values> = SaveOptions<T>;