Sequence
Builds a sequence-getter expression — nextval('name') or currval('name') — usable anywhere an
SQL element is accepted (typically as a Select column or an Insert value).
Constructor
Sequence(expression: string, next?: boolean): Sequence
new Sequence(expression: string, next?: boolean): Sequence
Dual-callable. expression is the sequence name; next (default false) selects nextval when
truthy or currval otherwise. Serializes to an empty string if expression is falsy.
import { Sequence, Insert } from '@sqb/builder';
Insert('customers', { id: Sequence('customers_id_seq', true), name: 'Jane' });
// insert into customers (id, name) values (nextval('customers_id_seq'), 'Jane')
The rendered nextval(...)/currval(...) syntax is PostgreSQL-flavored; a dialect extension can
override it for databases with different sequence syntax (e.g. Oracle's seq.NEXTVAL).
Properties
| Key | Type | Readonly | Description |
|---|---|---|---|
_type | SerializationType.SEQUENCE_GETTER_STATEMENT | Yes | Discriminates this node during serialization. |
_expression | string | No | The sequence name, set from the constructor. |
_next | boolean | No | Whether to render nextval (true) or currval (false). |
_alias | string | undefined | No | Set by .as(). |
Methods
next()
next(value: boolean): this
Sets _next, switching between nextval (true) and currval (false).
as()
as(alias: string): this
Sets an alias, rendered as a trailing alias after the closing paren (no AS keyword).
Sequence('customers_id_seq').next(true).as('id');
// nextval('customers_id_seq') id