Write options
Options of create, bulkCreate, update, destroy, restore, upsert, increment, instance save, and the PostgreSQL write statements: insert, insertFrom, ON CONFLICT, MERGE, UPDATE ... FROM, COPY.
Options marked postgrelize extension exist only in postgrelize; Sequelize v6 has no such option. This page is generated from the TypeScript declarations and their TSDoc, so it always matches the code.
BuildOptionsCreateOptionsBulkCreateOptionsUpdateOptionsDestroyOptionsRestoreOptionsUpsertOptionsSaveOptionsInstanceUpdateOptionsInstanceDestroyOptionsIncrementOptionsInsertOptionsInsertFromOptionsOnConflictOptionsMergeOptionsUpdateStatementOptionsWriteStatementOptionsCopyFromOptionsCopyToOptionsCopyToFormatOptionsBulkHookOptions
BuildOptions
Options of Model.build (and the model constructor).
See also: Model.build.
const opts: BuildOptions = { isNewRecord: false, raw: true };
const user = User.build({ id: 1, name: 'Ann' }, opts);| Option | Type | Description |
|---|---|---|
isNewRecord | boolean | Whether the instance represents a row that does not exist yet; save() inserts when true, updates when false. Default: true. |
raw | boolean | Set values without setters and without change tracking. Default: false. |
include | IncludeOption | Build associated instances from nested values (Sequelize v6 nested creation). |
CreateOptions
Options of create. Extends SaveOptions.
See also: Model.create.
const post = await Post.create(
{ title: 'Hello', author: { name: 'Ann' } },
{ include: [{ association: 'author' }], transaction },
);| Option | Type | Description |
|---|---|---|
transaction | Transaction | Transaction to run in; null forces running outside the CLS transaction. |
logging | false | LoggingFunction | SQL logger for this query, or false. |
benchmark | boolean | Pass query duration to the logger. |
statementTimeout postgrelize extension | number | statement_timeout for this query only, in milliseconds (non-negative integer; 0 disables it). PostgreSQL cancels the statement with a DatabaseError (code 57014) when it runs longer. Outside a transaction the query runs in its own short transaction; inside one the previous timeout is restored afterwards. PostgreSQL extension. |
retry | Readonly<{ max?: number | undefined; match?: readonly RetryMatch[] | undefined; backoff… | Retries this query when it fails, merged over the instance retry option (Sequelize v6). The connection acquisition is part of the retried work; a query that runs in a transaction is never retried. |
useMaster postgrelize extension | boolean | Force the writer even for SELECTs when read replicas are configured. postgrelize extension. |
replacements | Replacements | Values of ? (array) or :name (object) placeholders written in raw SQL inside the options (where: literal('"a" = :a'), attributes: [[literal(...), 'x']]), as in Sequelize v6. Every value is sent as a bound parameter, never concatenated into the SQL text; arrays expand to a comma separated list of parameters. |
fields | readonly (0 extends 1 & T ? any : keyof T & string)[] | Attributes to save; others are ignored. Default: all changed attributes. Mutable so hooks can adjust it, as in v6. |
silent | boolean | Do not update updatedAt. Default: false. |
returning | boolean | Return all columns after insert/update; PostgreSQL always uses RETURNING, so this is accepted for v6 compatibility. Default: true. |
hooks | boolean | Run lifecycle hooks. Default: true. |
validate | boolean | Run validations. Default: true (false for bulkCreate). |
overriding postgrelize extension | "system" | "user" | OVERRIDING SYSTEM VALUE / OVERRIDING USER VALUE for identity columns. PostgreSQL extension. |
onConflict postgrelize extension | Readonly<{ target?: OnConflictTarget<T> | undefined; action: "nothing" | Readonly<{ upd… | Advanced ON CONFLICT handling. PostgreSQL extension. |
include | IncludeOption | Create associated rows from nested values (Sequelize v6). |
BulkCreateOptions
Options of bulkCreate. Extends SaveOptions. Rows are inserted with one INSERT unless individualHooks or include is set. Validation is off by default (unlike create).
See also: Model.bulkCreate, OnConflictOptions.
const rows = [{ email: 'a@example.com', name: 'Ann' }];
await User.bulkCreate(rows, {
updateOnDuplicate: ['name'],
conflictAttributes: ['email'],
transaction,
});
const opts: BulkCreateOptions<User> = { ignoreDuplicates: true };| Option | Type | Description |
|---|---|---|
transaction | Transaction | Transaction to run in; null forces running outside the CLS transaction. |
logging | false | LoggingFunction | SQL logger for this query, or false. |
benchmark | boolean | Pass query duration to the logger. |
statementTimeout postgrelize extension | number | statement_timeout for this query only, in milliseconds (non-negative integer; 0 disables it). PostgreSQL cancels the statement with a DatabaseError (code 57014) when it runs longer. Outside a transaction the query runs in its own short transaction; inside one the previous timeout is restored afterwards. PostgreSQL extension. |
retry | Readonly<{ max?: number | undefined; match?: readonly RetryMatch[] | undefined; backoff… | Retries this query when it fails, merged over the instance retry option (Sequelize v6). The connection acquisition is part of the retried work; a query that runs in a transaction is never retried. |
useMaster postgrelize extension | boolean | Force the writer even for SELECTs when read replicas are configured. postgrelize extension. |
replacements | Replacements | Values of ? (array) or :name (object) placeholders written in raw SQL inside the options (where: literal('"a" = :a'), attributes: [[literal(...), 'x']]), as in Sequelize v6. Every value is sent as a bound parameter, never concatenated into the SQL text; arrays expand to a comma separated list of parameters. |
fields | readonly (0 extends 1 & T ? any : keyof T & string)[] | Attributes to save; others are ignored. Default: all changed attributes. Mutable so hooks can adjust it, as in v6. |
silent | boolean | Do not update updatedAt. Default: false. |
returning | boolean | Return all columns after insert/update; PostgreSQL always uses RETURNING, so this is accepted for v6 compatibility. Default: true. |
hooks | boolean | Run lifecycle hooks. Default: true. |
validate | boolean | Run validations. Default: true (false for bulkCreate). |
overriding postgrelize extension | "system" | "user" | OVERRIDING SYSTEM VALUE / OVERRIDING USER VALUE for identity columns. PostgreSQL extension. |
unnest postgrelize extension | boolean | Insert with one array parameter per column (INSERT … SELECT * FROM unnest($1::type[], …)) instead of one parameter per cell, so batches are not capped by PostgreSQL's 65535 parameters. Every row must provide every column; ARRAY columns and conflict options are not supported. PostgreSQL extension. Default: false. |
ignoreDuplicates | boolean | Add ON CONFLICT DO NOTHING, skipping rows that violate a unique constraint. Default: false. |
individualHooks | boolean | Run per-instance create hooks (rows are then inserted one by one, like v6). Default: false. |
include | IncludeOption | Create associated rows from nested values (rows are inserted one by one). |
updateOnDuplicate | readonly (keyof T & string)[] | Attributes updated from the proposed row on conflict (DO UPDATE SET a = EXCLUDED.a). |
conflictAttributes | readonly (keyof T & string)[] | Conflict target attributes for updateOnDuplicate. Default: first unique key, else primary key. |
conflictWhere | WhereOptions<T> | Predicate of a partial unique index used as conflict target. |
onConflict postgrelize extension | Readonly<{ target?: OnConflictTarget<T> | undefined; action: "nothing" | Readonly<{ upd… | Advanced ON CONFLICT handling; cannot be combined with the options above. PostgreSQL extension. |
UpdateOptions
Options of static update. Extends BulkHookOptions. Updates updatedAt unless silent.
See also: Model.update.
const [count] = await User.update(
{ active: false },
{ where: { lastLogin: { [Op.lt]: new Date('2020-01-01') } }, transaction },
);
const opts: UpdateOptions<Task> = { where: { done: false }, returning: true };| Option | Type | Description |
|---|---|---|
transaction | Transaction | Transaction to run in; null forces running outside the CLS transaction. |
logging | false | LoggingFunction | SQL logger for this query, or false. |
benchmark | boolean | Pass query duration to the logger. |
statementTimeout postgrelize extension | number | statement_timeout for this query only, in milliseconds (non-negative integer; 0 disables it). PostgreSQL cancels the statement with a DatabaseError (code 57014) when it runs longer. Outside a transaction the query runs in its own short transaction; inside one the previous timeout is restored afterwards. PostgreSQL extension. |
retry | Readonly<{ max?: number | undefined; match?: readonly RetryMatch[] | undefined; backoff… | Retries this query when it fails, merged over the instance retry option (Sequelize v6). The connection acquisition is part of the retried work; a query that runs in a transaction is never retried. |
useMaster postgrelize extension | boolean | Force the writer even for SELECTs when read replicas are configured. postgrelize extension. |
replacements | Replacements | Values of ? (array) or :name (object) placeholders written in raw SQL inside the options (where: literal('"a" = :a'), attributes: [[literal(...), 'x']]), as in Sequelize v6. Every value is sent as a bound parameter, never concatenated into the SQL text; arrays expand to a comma separated list of parameters. |
fields | readonly (keyof T & string)[] | Attributes to update; others are ignored. Mutable so hooks can adjust it, as in v6. |
where | WhereOptions<T> | Rows to update. |
silent | boolean | Do not update updatedAt. Default: false. |
returning | boolean | Return updated rows as the second element of the result. |
returningOld postgrelize extension | boolean | With returning: expose values before the update as instance.oldValues. PostgreSQL extension, needs PostgreSQL 18+. |
validate | boolean | Validate the new values. Default: true. |
paranoid | boolean | For paranoid models: false also updates soft-deleted rows. Default: true. |
from postgrelize extension | Readonly<Record<string, FromSourceOption>> | Extra relations (UPDATE ... FROM), keyed by alias; reference them with col('alias.attr'). PostgreSQL extension. |
hooks | boolean | Run the bulk hooks (beforeBulkUpdate, beforeBulkDestroy, ...). Default: true. |
individualHooks | boolean | Also run per-instance hooks; rows are loaded first, which costs an extra query. Default: false. |
DestroyOptions
Options of static destroy. Extends BulkHookOptions. Paranoid models are soft-deleted (deletedAt set) unless force.
See also: Model.destroy.
await Post.destroy({ where: { draft: true }, force: true });
const opts: DestroyOptions<Task> = { truncate: true, cascade: true, restartIdentity: true };| Option | Type | Description |
|---|---|---|
transaction | Transaction | Transaction to run in; null forces running outside the CLS transaction. |
logging | false | LoggingFunction | SQL logger for this query, or false. |
benchmark | boolean | Pass query duration to the logger. |
statementTimeout postgrelize extension | number | statement_timeout for this query only, in milliseconds (non-negative integer; 0 disables it). PostgreSQL cancels the statement with a DatabaseError (code 57014) when it runs longer. Outside a transaction the query runs in its own short transaction; inside one the previous timeout is restored afterwards. PostgreSQL extension. |
retry | Readonly<{ max?: number | undefined; match?: readonly RetryMatch[] | undefined; backoff… | Retries this query when it fails, merged over the instance retry option (Sequelize v6). The connection acquisition is part of the retried work; a query that runs in a transaction is never retried. |
useMaster postgrelize extension | boolean | Force the writer even for SELECTs when read replicas are configured. postgrelize extension. |
replacements | Replacements | Values of ? (array) or :name (object) placeholders written in raw SQL inside the options (where: literal('"a" = :a'), attributes: [[literal(...), 'x']]), as in Sequelize v6. Every value is sent as a bound parameter, never concatenated into the SQL text; arrays expand to a comma separated list of parameters. |
where | WhereOptions<T> | Rows to delete. |
limit | number | Delete at most this many rows. |
truncate | boolean | Use TRUNCATE instead of DELETE. Default: false. |
cascade | boolean | With truncate: add CASCADE. Default: false. |
restartIdentity | boolean | With truncate: add RESTART IDENTITY. Default: false. |
returning postgrelize extension | boolean | Return deleted rows instead of their count (DELETE ... RETURNING). PostgreSQL extension. |
force | boolean | For paranoid models: delete rows instead of setting deletedAt. Default: false. |
using postgrelize extension | Readonly<Record<string, FromSourceOption>> | Extra relations (DELETE ... USING), keyed by alias. PostgreSQL extension. |
hooks | boolean | Run the bulk hooks (beforeBulkUpdate, beforeBulkDestroy, ...). Default: true. |
individualHooks | boolean | Also run per-instance hooks; rows are loaded first, which costs an extra query. Default: false. |
RestoreOptions
Options of static restore, which clears deletedAt of soft-deleted rows of paranoid models.
See also: Model.restore.
await Post.restore({ where: { authorId: 1 }, transaction });
const opts: RestoreOptions<Post> = { hooks: false };| Option | Type | Description |
|---|---|---|
transaction | Transaction | Transaction to run in; null forces running outside the CLS transaction. |
logging | false | LoggingFunction | SQL logger for this query, or false. |
benchmark | boolean | Pass query duration to the logger. |
statementTimeout postgrelize extension | number | statement_timeout for this query only, in milliseconds (non-negative integer; 0 disables it). PostgreSQL cancels the statement with a DatabaseError (code 57014) when it runs longer. Outside a transaction the query runs in its own short transaction; inside one the previous timeout is restored afterwards. PostgreSQL extension. |
retry | Readonly<{ max?: number | undefined; match?: readonly RetryMatch[] | undefined; backoff… | Retries this query when it fails, merged over the instance retry option (Sequelize v6). The connection acquisition is part of the retried work; a query that runs in a transaction is never retried. |
useMaster postgrelize extension | boolean | Force the writer even for SELECTs when read replicas are configured. postgrelize extension. |
replacements | Replacements | Values of ? (array) or :name (object) placeholders written in raw SQL inside the options (where: literal('"a" = :a'), attributes: [[literal(...), 'x']]), as in Sequelize v6. Every value is sent as a bound parameter, never concatenated into the SQL text; arrays expand to a comma separated list of parameters. |
where | WhereOptions<T> | Rows to restore; all soft-deleted rows when omitted. |
hooks | boolean | Run the bulk hooks (beforeBulkUpdate, beforeBulkDestroy, ...). Default: true. |
individualHooks | boolean | Also run per-instance hooks; rows are loaded first, which costs an extra query. Default: false. |
UpsertOptions
Options of upsert. Extends SaveOptions. With onConflict the result of upsert becomes [instance, created].
See also: Model.upsert.
const [user] = await User.upsert(
{ email: 'a@example.com', name: 'Ann' },
{ conflictFields: ['email'] },
);| Option | Type | Description |
|---|---|---|
transaction | Transaction | Transaction to run in; null forces running outside the CLS transaction. |
logging | false | LoggingFunction | SQL logger for this query, or false. |
benchmark | boolean | Pass query duration to the logger. |
statementTimeout postgrelize extension | number | statement_timeout for this query only, in milliseconds (non-negative integer; 0 disables it). PostgreSQL cancels the statement with a DatabaseError (code 57014) when it runs longer. Outside a transaction the query runs in its own short transaction; inside one the previous timeout is restored afterwards. PostgreSQL extension. |
retry | Readonly<{ max?: number | undefined; match?: readonly RetryMatch[] | undefined; backoff… | Retries this query when it fails, merged over the instance retry option (Sequelize v6). The connection acquisition is part of the retried work; a query that runs in a transaction is never retried. |
useMaster postgrelize extension | boolean | Force the writer even for SELECTs when read replicas are configured. postgrelize extension. |
replacements | Replacements | Values of ? (array) or :name (object) placeholders written in raw SQL inside the options (where: literal('"a" = :a'), attributes: [[literal(...), 'x']]), as in Sequelize v6. Every value is sent as a bound parameter, never concatenated into the SQL text; arrays expand to a comma separated list of parameters. |
fields | readonly (0 extends 1 & T ? any : keyof T & string)[] | Attributes to save; others are ignored. Default: all changed attributes. Mutable so hooks can adjust it, as in v6. |
silent | boolean | Do not update updatedAt. Default: false. |
returning | boolean | Return all columns after insert/update; PostgreSQL always uses RETURNING, so this is accepted for v6 compatibility. Default: true. |
hooks | boolean | Run lifecycle hooks. Default: true. |
validate | boolean | Run validations. Default: true (false for bulkCreate). |
overriding postgrelize extension | "system" | "user" | OVERRIDING SYSTEM VALUE / OVERRIDING USER VALUE for identity columns. PostgreSQL extension. OVERRIDING SYSTEM VALUE / OVERRIDING USER VALUE for identity columns. |
conflictFields | readonly string[] | Conflict target columns. Default: unique key containing a given attribute, else the primary key. |
onConflict postgrelize extension | Readonly<{ target?: OnConflictTarget<T> | undefined; action: "nothing" | Readonly<{ upd… | Advanced ON CONFLICT handling; the result becomes [instance, created]. PostgreSQL extension. |
SaveOptions
Options of create and instance save. Runs beforeSave / beforeCreate / beforeUpdate hooks (and their after counterparts) unless hooks is false.
See also: Model.create.
const opts: SaveOptions<User> = { fields: ['name', 'email'], validate: true, transaction };
await user.save(opts);| Option | Type | Description |
|---|---|---|
transaction | Transaction | Transaction to run in; null forces running outside the CLS transaction. |
logging | false | LoggingFunction | SQL logger for this query, or false. |
benchmark | boolean | Pass query duration to the logger. |
statementTimeout postgrelize extension | number | statement_timeout for this query only, in milliseconds (non-negative integer; 0 disables it). PostgreSQL cancels the statement with a DatabaseError (code 57014) when it runs longer. Outside a transaction the query runs in its own short transaction; inside one the previous timeout is restored afterwards. PostgreSQL extension. |
retry | Readonly<{ max?: number | undefined; match?: readonly RetryMatch[] | undefined; backoff… | Retries this query when it fails, merged over the instance retry option (Sequelize v6). The connection acquisition is part of the retried work; a query that runs in a transaction is never retried. |
useMaster postgrelize extension | boolean | Force the writer even for SELECTs when read replicas are configured. postgrelize extension. |
replacements | Replacements | Values of ? (array) or :name (object) placeholders written in raw SQL inside the options (where: literal('"a" = :a'), attributes: [[literal(...), 'x']]), as in Sequelize v6. Every value is sent as a bound parameter, never concatenated into the SQL text; arrays expand to a comma separated list of parameters. |
fields | readonly (0 extends 1 & T ? any : keyof T & string)[] | Attributes to save; others are ignored. Default: all changed attributes. Mutable so hooks can adjust it, as in v6. |
silent | boolean | Do not update updatedAt. Default: false. |
returning | boolean | Return all columns after insert/update; PostgreSQL always uses RETURNING, so this is accepted for v6 compatibility. Default: true. |
hooks | boolean | Run lifecycle hooks. Default: true. |
validate | boolean | Run validations. Default: true (false for bulkCreate). |
overriding postgrelize extension | "system" | "user" | OVERRIDING SYSTEM VALUE / OVERRIDING USER VALUE for identity columns. PostgreSQL extension. |
InstanceUpdateOptions
Options of the instance updates that association accessors run (Sequelize v6 InstanceUpdateOptions): the SaveOptions.
const options: InstanceUpdateOptions<User> = { fields: ['name'], transaction };
await user.update({ name: 'Ann' }, options);| Option | Type | Description |
|---|---|---|
transaction | Transaction | Transaction to run in; null forces running outside the CLS transaction. |
logging | false | LoggingFunction | SQL logger for this query, or false. |
benchmark | boolean | Pass query duration to the logger. |
statementTimeout postgrelize extension | number | statement_timeout for this query only, in milliseconds (non-negative integer; 0 disables it). PostgreSQL cancels the statement with a DatabaseError (code 57014) when it runs longer. Outside a transaction the query runs in its own short transaction; inside one the previous timeout is restored afterwards. PostgreSQL extension. |
retry | Readonly<{ max?: number | undefined; match?: readonly RetryMatch[] | undefined; backoff… | Retries this query when it fails, merged over the instance retry option (Sequelize v6). The connection acquisition is part of the retried work; a query that runs in a transaction is never retried. |
useMaster postgrelize extension | boolean | Force the writer even for SELECTs when read replicas are configured. postgrelize extension. |
replacements | Replacements | Values of ? (array) or :name (object) placeholders written in raw SQL inside the options (where: literal('"a" = :a'), attributes: [[literal(...), 'x']]), as in Sequelize v6. Every value is sent as a bound parameter, never concatenated into the SQL text; arrays expand to a comma separated list of parameters. |
fields | readonly (0 extends 1 & T ? any : keyof T & string)[] | Attributes to save; others are ignored. Default: all changed attributes. Mutable so hooks can adjust it, as in v6. |
silent | boolean | Do not update updatedAt. Default: false. |
returning | boolean | Return all columns after insert/update; PostgreSQL always uses RETURNING, so this is accepted for v6 compatibility. Default: true. |
hooks | boolean | Run lifecycle hooks. Default: true. |
validate | boolean | Run validations. Default: true (false for bulkCreate). |
overriding postgrelize extension | "system" | "user" | OVERRIDING SYSTEM VALUE / OVERRIDING USER VALUE for identity columns. PostgreSQL extension. |
InstanceDestroyOptions
Options of instance destroy.
See also: Model.destroy.
await post.destroy({ force: true, transaction });
const opts: InstanceDestroyOptions = { hooks: false };| Option | Type | Description |
|---|---|---|
transaction | Transaction | Transaction to run in; null forces running outside the CLS transaction. |
logging | false | LoggingFunction | SQL logger for this query, or false. |
benchmark | boolean | Pass query duration to the logger. |
statementTimeout postgrelize extension | number | statement_timeout for this query only, in milliseconds (non-negative integer; 0 disables it). PostgreSQL cancels the statement with a DatabaseError (code 57014) when it runs longer. Outside a transaction the query runs in its own short transaction; inside one the previous timeout is restored afterwards. PostgreSQL extension. |
retry | Readonly<{ max?: number | undefined; match?: readonly RetryMatch[] | undefined; backoff… | Retries this query when it fails, merged over the instance retry option (Sequelize v6). The connection acquisition is part of the retried work; a query that runs in a transaction is never retried. |
useMaster postgrelize extension | boolean | Force the writer even for SELECTs when read replicas are configured. postgrelize extension. |
replacements | Replacements | Values of ? (array) or :name (object) placeholders written in raw SQL inside the options (where: literal('"a" = :a'), attributes: [[literal(...), 'x']]), as in Sequelize v6. Every value is sent as a bound parameter, never concatenated into the SQL text; arrays expand to a comma separated list of parameters. |
force | boolean | For paranoid models: delete the row instead of setting deletedAt. Default: false. |
hooks | boolean | Run beforeDestroy / afterDestroy hooks. Default: true. |
IncrementOptions
Options of increment and decrement. Runs one UPDATE ... SET col = col + n and updates updatedAt unless silent.
See also: Model.increment.
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);| Option | Type | Description |
|---|---|---|
transaction | Transaction | Transaction to run in; null forces running outside the CLS transaction. |
logging | false | LoggingFunction | SQL logger for this query, or false. |
benchmark | boolean | Pass query duration to the logger. |
statementTimeout postgrelize extension | number | statement_timeout for this query only, in milliseconds (non-negative integer; 0 disables it). PostgreSQL cancels the statement with a DatabaseError (code 57014) when it runs longer. Outside a transaction the query runs in its own short transaction; inside one the previous timeout is restored afterwards. PostgreSQL extension. |
retry | Readonly<{ max?: number | undefined; match?: readonly RetryMatch[] | undefined; backoff… | Retries this query when it fails, merged over the instance retry option (Sequelize v6). The connection acquisition is part of the retried work; a query that runs in a transaction is never retried. |
useMaster postgrelize extension | boolean | Force the writer even for SELECTs when read replicas are configured. postgrelize extension. |
replacements | Replacements | Values of ? (array) or :name (object) placeholders written in raw SQL inside the options (where: literal('"a" = :a'), attributes: [[literal(...), 'x']]), as in Sequelize v6. Every value is sent as a bound parameter, never concatenated into the SQL text; arrays expand to a comma separated list of parameters. |
where | WhereOptions<T> | Rows to change; required for the static form, ignored on instances. |
by | number | Amount to add (increment) or subtract (decrement). Default: 1. |
silent | boolean | Do not update updatedAt. Default: false. |
InsertOptions postgrelize extension
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.
See also: Model.insert.
const opts: InsertOptions<User> = {
onConflict: { target: ['email'], action: 'nothing' },
hooks: false,
};
const rows = await User.insert([{ email: 'a@example.com' }], opts);| Option | Type | Description |
|---|---|---|
transaction | Transaction | Transaction to run in; null forces running outside the CLS transaction. |
logging | false | LoggingFunction | SQL logger for this query, or false. |
benchmark | boolean | Pass query duration to the logger. |
statementTimeout postgrelize extension | number | statement_timeout for this query only, in milliseconds (non-negative integer; 0 disables it). PostgreSQL cancels the statement with a DatabaseError (code 57014) when it runs longer. Outside a transaction the query runs in its own short transaction; inside one the previous timeout is restored afterwards. PostgreSQL extension. |
retry | Readonly<{ max?: number | undefined; match?: readonly RetryMatch[] | undefined; backoff… | Retries this query when it fails, merged over the instance retry option (Sequelize v6). The connection acquisition is part of the retried work; a query that runs in a transaction is never retried. |
useMaster postgrelize extension | boolean | Force the writer even for SELECTs when read replicas are configured. postgrelize extension. |
replacements | Replacements | Values of ? (array) or :name (object) placeholders written in raw SQL inside the options (where: literal('"a" = :a'), attributes: [[literal(...), 'x']]), as in Sequelize v6. Every value is sent as a bound parameter, never concatenated into the SQL text; arrays expand to a comma separated list of parameters. |
fields | readonly (keyof T & string)[] | Attributes to insert; others are ignored. Default: all given attributes. |
onConflict | Readonly<{ target?: OnConflictTarget<T> | undefined; action: "nothing" | Readonly<{ upd… | Advanced ON CONFLICT handling. |
hooks | boolean | Run beforeBulkCreate / afterBulkCreate hooks. Default: true. |
validate | boolean | Validate rows before inserting. Default: false. |
overriding | "system" | "user" | OVERRIDING SYSTEM VALUE / OVERRIDING USER VALUE for identity columns. |
returningOld postgrelize extension | boolean | Return the pre-statement row values as instance.oldValues. PostgreSQL extension, needs PostgreSQL 18+. |
InsertFromOptions postgrelize extension
Options of Model.insertFrom, which inserts the result of a subquery (INSERT ... SELECT). PostgreSQL extension.
See also: Model.insertFrom.
const opts: InsertFromOptions<Task> = {
columns: ['title', 'projectId'],
onConflict: { action: 'nothing' },
};| Option | Type | Description |
|---|---|---|
transaction | Transaction | Transaction to run in; null forces running outside the CLS transaction. |
logging | false | LoggingFunction | SQL logger for this query, or false. |
benchmark | boolean | Pass query duration to the logger. |
statementTimeout postgrelize extension | number | statement_timeout for this query only, in milliseconds (non-negative integer; 0 disables it). PostgreSQL cancels the statement with a DatabaseError (code 57014) when it runs longer. Outside a transaction the query runs in its own short transaction; inside one the previous timeout is restored afterwards. PostgreSQL extension. |
retry | Readonly<{ max?: number | undefined; match?: readonly RetryMatch[] | undefined; backoff… | Retries this query when it fails, merged over the instance retry option (Sequelize v6). The connection acquisition is part of the retried work; a query that runs in a transaction is never retried. |
useMaster postgrelize extension | boolean | Force the writer even for SELECTs when read replicas are configured. postgrelize extension. |
replacements | Replacements | Values of ? (array) or :name (object) placeholders written in raw SQL inside the options (where: literal('"a" = :a'), attributes: [[literal(...), 'x']]), as in Sequelize v6. Every value is sent as a bound parameter, never concatenated into the SQL text; arrays expand to a comma separated list of parameters. |
columns | readonly (keyof T & string)[] | Target attributes, in the order of the subquery columns. |
onConflict | Readonly<{ target?: OnConflictTarget<T> | undefined; action: "nothing" | Readonly<{ upd… | Advanced ON CONFLICT handling. |
returningOld | boolean | Return the pre-statement row values as instance.oldValues. PostgreSQL 18+. |
OnConflictOptions postgrelize extension
Advanced INSERT ... ON CONFLICT specification. PostgreSQL extension.
const rows = [{ tenantId: 1, key: 'views', hits: 1, version: 2 }];
await Product.bulkCreate(rows, {
onConflict: {
target: ['tenantId', 'key'],
action: {
update: { hits: literal('"Product"."hits" + EXCLUDED."hits"'), updatedAt: fn('now') },
where: { version: { [Op.lt]: excluded('version') } },
},
},
});| Option | Type | Description |
|---|---|---|
target | OnConflictTarget<T> | Unique index or constraint to arbitrate on. Required for update. |
action | "nothing" | Readonly<{ update: OnConflictUpdate<T>; where?: WhereOptions<T> | undefined… | 'nothing' to skip conflicting rows, or an update. |
MergeOptions postgrelize extension
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.
See also: Model.merge.
const opts: MergeOptions<Product> = {
source: [{ sku: 'a1', price: 10 }],
on: { sku: col('source.sku') },
whenMatched: { update: { price: col('source.price') } },
whenNotMatched: { insert: true },
};| Option | Type | Description |
|---|---|---|
source | Subquery | readonly Readonly<Record<string, unknown>>[] | Source rows (plain objects) or a subquery. |
as | string | Alias of the source relation. Default: 'source'. |
on | {} | Join condition between target (model alias) and source (col('source.attr')). |
whenMatched | MergeMatchedAction<T> | readonly MergeMatchedAction<T>[] | Actions for matched rows, applied in order. |
whenNotMatched | MergeInsertAction<T> | readonly MergeInsertAction<T>[] | Actions for source rows without a target row. |
whenNotMatchedBySource | MergeMatchedAction<T> | readonly MergeMatchedAction<T>[] | Actions for target rows without a source row (PostgreSQL 17+). |
UpdateStatementOptions postgrelize extension
Options of Model.updateStatement(). PostgreSQL extension.
const options: UpdateStatementOptions<Task> = { where: { id: 1 }, returning: true, silent: true };| Option | Type | Description |
|---|---|---|
where | WhereOptions<T> | Rows to change; all rows when omitted. The default scope applies. |
returning | boolean | Add RETURNING with all columns, so that the CTE can be read. |
silent | boolean | Leave updatedAt unchanged. |
WriteStatementOptions
Options of Model.deleteStatement() and Model.updateStatement().
const options: WriteStatementOptions<Task> = { where: { done: true }, returning: true };| Option | Type | Description |
|---|---|---|
where | WhereOptions<T> | Rows to change; all rows when omitted. The default scope applies. |
returning | boolean | Add RETURNING with all columns, so that the CTE can be read. |
CopyFromOptions postgrelize extension
Options of Model.copyFrom, which loads rows with COPY ... FROM STDIN. PostgreSQL extension.
See also: Model.copyFrom.
const opts: CopyFromOptions<User> = { columns: ['name', 'email'], transaction };
await User.copyFrom([{ name: 'Ann', email: 'a@example.com' }], opts);| Option | Type | Description |
|---|---|---|
transaction | Transaction | Transaction to run in; null forces running outside the CLS transaction. |
logging | false | LoggingFunction | SQL logger for this query, or false. |
benchmark | boolean | Pass query duration to the logger. |
statementTimeout postgrelize extension | number | statement_timeout for this query only, in milliseconds (non-negative integer; 0 disables it). PostgreSQL cancels the statement with a DatabaseError (code 57014) when it runs longer. Outside a transaction the query runs in its own short transaction; inside one the previous timeout is restored afterwards. PostgreSQL extension. |
retry | Readonly<{ max?: number | undefined; match?: readonly RetryMatch[] | undefined; backoff… | Retries this query when it fails, merged over the instance retry option (Sequelize v6). The connection acquisition is part of the retried work; a query that runs in a transaction is never retried. |
useMaster postgrelize extension | boolean | Force the writer even for SELECTs when read replicas are configured. postgrelize extension. |
replacements | Replacements | Values of ? (array) or :name (object) placeholders written in raw SQL inside the options (where: literal('"a" = :a'), attributes: [[literal(...), 'x']]), as in Sequelize v6. Every value is sent as a bound parameter, never concatenated into the SQL text; arrays expand to a comma separated list of parameters. |
columns | readonly (keyof T & string)[] | Attributes to copy, in row order; default: keys of the first row of an array source. |
CopyToOptions postgrelize extension
Options of Model.copyTo: a finder query that selects the exported rows plus the output format. PostgreSQL extension.
See also: Model.copyTo.
const opts: CopyToOptions<User> = { where: { active: true }, order: [['id', 'ASC']], format: 'csv', header: true };| Option | Type | Description |
|---|---|---|
transaction | Transaction | Transaction to run in; null forces running outside the CLS transaction. |
logging | false | LoggingFunction | SQL logger for this query, or false. |
benchmark | boolean | Pass query duration to the logger. |
statementTimeout postgrelize extension | number | statement_timeout for this query only, in milliseconds (non-negative integer; 0 disables it). PostgreSQL cancels the statement with a DatabaseError (code 57014) when it runs longer. Outside a transaction the query runs in its own short transaction; inside one the previous timeout is restored afterwards. PostgreSQL extension. |
retry | Readonly<{ max?: number | undefined; match?: readonly RetryMatch[] | undefined; backoff… | Retries this query when it fails, merged over the instance retry option (Sequelize v6). The connection acquisition is part of the retried work; a query that runs in a transaction is never retried. |
useMaster postgrelize extension | boolean | Force the writer even for SELECTs when read replicas are configured. postgrelize extension. |
replacements | Replacements | Values of ? (array) or :name (object) placeholders written in raw SQL inside the options (where: literal('"a" = :a'), attributes: [[literal(...), 'x']]), as in Sequelize v6. Every value is sent as a bound parameter, never concatenated into the SQL text; arrays expand to a comma separated list of parameters. |
attributes | FindAttributeOptions | Selected attributes. All attributes when omitted. |
order | Order | readonly unknown[] | Sort order: attribute names, [attribute, 'ASC' | 'DESC'] pairs or expressions. |
group | {} | GROUP BY attributes or expressions. |
having | {} | HAVING condition. |
limit | number | Maximum number of rows; null or undefined for no limit. |
offset | number | Number of rows to skip; null or undefined to skip none. |
lock | LockOption | Row lock (FOR UPDATE etc.); requires a transaction. |
skipLocked | boolean | Add SKIP LOCKED to the row lock; only with lock. |
noWait postgrelize extension | boolean | Add NOWAIT to the row lock so the query fails instead of waiting; only with lock. PostgreSQL extension. |
distinctOn postgrelize extension | readonly unknown[] | SELECT DISTINCT ON (...): attributes or expressions. PostgreSQL extension. |
withTies postgrelize extension | boolean | FETCH FIRST limit ROWS WITH TIES instead of LIMIT: also returns the rows that tie with the last one on the order columns. Needs limit and order. PostgreSQL extension. |
sample postgrelize extension | Readonly<{ method: "BERNOULLI" | "SYSTEM"; percent: number; seed?: number | undefined; }> | Read only a random sample of the model table (TABLESAMPLE). PostgreSQL extension. |
where | WhereOptions<T> | Filter rows; see WhereOptions. All rows when omitted. |
raw | boolean | Return plain row objects instead of model instances. Default: false. |
nest | boolean | With raw, build nested objects from dotted keys such as 'posts.title'. Default: false. |
plain | boolean | Return a single row instead of an array (set internally by findOne). Default: false. |
rejectOnEmpty | boolean | Error | Throw EmptyResultError (or the given Error) when no row is found. Default: false. |
paranoid | boolean | For paranoid models: false includes soft-deleted rows. Default: true. |
hooks | boolean | Run beforeFind / afterFind hooks. Default: true. |
include | IncludeOption | Eager-load associations through JOINs (or separate queries for separate includes). |
subQuery | boolean | false disables the sub-query used for limit combined with multi-row includes. |
tableAs | string | Alias of the main table in FROM instead of the model name, so literals and col('alias.x') can refer to it (a v6 internal option that v6's typings omit). Ignored when include is set. |
with postgrelize extension | Readonly<Record<string, Subquery | Readonly<{ query: Subquery | WriteStatement; recursi… | Common table expressions (WITH [RECURSIVE] ...). PostgreSQL extension. |
source postgrelize extension | string | Subquery | 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. |
from postgrelize extension | Readonly<Record<string, FromSourceOption>> | Extra relations (FROM "Model", "alias"), keyed by alias; correlate them in where. PostgreSQL extension. |
format | "text" | "csv" | "binary" | Output format. Default: 'text'. |
header | boolean | Emit a header line (csv). Default: false. |
delimiter | string | Single-character field delimiter. Default: tab for text, comma for csv. |
nullString | string | String written for NULL values. Default: \N for text, empty for csv. |
quote | string | Single-character quote (csv). Default: ". |
CopyToFormatOptions postgrelize extension
Output format options of Model.copyTo (COPY ... TO STDOUT). PostgreSQL extension.
const format: CopyToFormatOptions = { format: 'csv', header: true, delimiter: ';', nullString: '' };| Option | Type | Description |
|---|---|---|
format | "text" | "csv" | "binary" | Output format. Default: 'text'. |
header | boolean | Emit a header line (csv). Default: false. |
delimiter | string | Single-character field delimiter. Default: tab for text, comma for csv. |
nullString | string | String written for NULL values. Default: \N for text, empty for csv. |
quote | string | Single-character quote (csv). Default: ". |
BulkHookOptions
Hook switches shared by bulk update, destroy and restore.
const opts: BulkHookOptions = { hooks: true, individualHooks: true };
await User.destroy({ where: { active: false }, ...opts });| Option | Type | Description |
|---|---|---|
hooks | boolean | Run the bulk hooks (beforeBulkUpdate, beforeBulkDestroy, ...). Default: true. |
individualHooks | boolean | Also run per-instance hooks; rows are loaded first, which costs an extra query. Default: false. |