Skip to main content

Update

Builds an UPDATE ... SET ... query. Extends ReturningQuery (which extends Query). See the Update Statement guide for a full walkthrough.

Constructor

Update(tableName: string | TableName | Raw, input: Record<string, any> | Select | Raw): Update
new Update(tableName: string | TableName | Raw, input: Record<string, any> | Select | Raw): Update

Dual-callable. Validates both arguments and throws a TypeError if they don't match:

  • tableName must be a string, a TableName, or a Raw instance, otherwise: "String or Raw instance required as first argument (tableName) for Update".
  • input must be a plain object, a Select, or a Raw instance (not an array), otherwise: "Object or Raw instance required as second argument (input) for Update".
import { Update, Eq } from '@sqb/builder';

Update('customers', { name: 'Jane' }).where(Eq('id', 1));

Properties

KeyTypeReadonlyDescription
_typeSerializationType.UPDATE_QUERYYesDiscriminates this node during serialization.
_tableTableName | RawNoThe target table, set from the constructor's tableName argument.
_inputRecord<string, any> | Select | RawNoThe column values / sub-select set from the constructor's input argument.
_whereLogicalOperator | undefinedNoThe implicit top-level And built by .where().

Plus everything inherited from ReturningQuery (_returningColumns, _comment, _params, EventEmitter).

Methods

where()

where(...condition: any[]): this

Accumulates conditions into an implicit top-level And, exactly like Select#where(). See Operators and Conditions. Omitting .where() updates every row in the table.

Update('customers', { id: 2, name: 'aaa' }).where(Eq('id', 1)).generate().sql;
// update customers set id = 2, name = 'aaa' where id = 1

returning(), generate(), values(), comment()

Inherited from ReturningQuery and Query.

See also