Skip to main content

ReturningQuery

ReturningQuery is an abstract subclass of Query that adds .returning() support. Insert and Update extend it; Select, Delete, and Union do not.

Like Query, it cannot be instantiated directly — calling ReturningQuery() or new ReturningQuery() throws a TypeError ("ReturningQuery is abstract and cannot be instantiated").

Properties

KeyTypeReadonlyDescription
_returningColumnsReturningColumn[] | undefinedNoColumns registered via .returning().

Also inherits _comment, _params, and the EventEmitter properties from Query.

Methods

returning()

returning(...columns: string[]): this

Adds a RETURNING clause listing the given columns. Each column string is parsed as field [as alias] and throws a TypeError ("does not match") if it doesn't match that format. Calling returning() with no arguments clears any previously set returning columns.

import { Insert } from '@sqb/builder';

const result = Insert('customers', { id: 1, name: 'aaa' })
.returning('id', 'update as u1')
.generate();

result.sql;
// insert into customers (id, name) values (1, 'aaa') returning id, "update" as u1
result.returningFields;
// [{ field: 'id', alias: undefined }, { field: 'update', alias: 'u1' }]

Requested columns are also reported back on GenerateResult.returningFields — see GenerateResult.

See also