Skip to main content

Exists

Sub-query existence test (exists (query)). Extends CompOperator (single-operand — it has no right-hand side). Shorthand key: exists (special-cased in the object-literal shorthand, not a suffix).

Constructor

Exists(query: Select | Raw): Exists
new Exists(query: Select | Raw): Exists

Dual-callable. query must be a Select or Raw instance, otherwise throws a TypeError ("You must provide a Select or Raw in exists()").

import { Exists, Eq, Field, Select } from '@sqb/builder';

const hasOrders = Select().from('orders').where(Eq('customer_id', Field('customers.id')));

Select().from('customers').where(Exists(hasOrders));
// where exists (select * from orders where customer_id = customers.id)

Properties

Inherits every property from CompOperator (_type, _left, _symbol, _isArray); _left holds query and _right is unused. _symbol is always 'exists'.

Methods

Exists overrides _serialize() to render only the left operand (exists <query>), serializing to an empty string if the sub-query itself is empty; it adds no chainable methods of its own.

Object-literal shorthand

The exists/!exists keys are special-cased (not suffixes) in .where()'s object-literal form:

Select().from('customers').where({ exists: Select().from('orders') });
// where exists (select * from orders)

See also