Skip to main content

Join

Represents one join clause attached to a Select via .join(). Every join-type constructor — InnerJoin, LeftJoin, LeftOuterJoin, RightJoin, RightOuterJoin, OuterJoin, FullOuterJoin, CrossJoin — is a thin subclass that hard-codes the joinType argument shown below. See the Joins guide for a full walkthrough.

Constructor

Join(joinType: JoinType, table: string | TableName | Select | Raw): Join
new Join(joinType: JoinType, table: string | TableName | Select | Raw): Join

Dual-callable. table accepts a table-name string (converted to TableName), a TableName instance, a Raw fragment, or a sub-Select. Anything else throws a TypeError ("Table name, select query or raw object required for Join"). See JoinType for the enum passed as the first argument.

import { Join, JoinType, Select } from '@sqb/builder';

Select().from('t1').join(Join(JoinType.LEFT, 't2'));
// equivalent to LeftJoin('t2')

Properties

KeyTypeReadonlyDescription
_typeSerializationType.JOINYesDiscriminates this node during serialization.
_joinTypeJoinTypeNoSet from the constructor's joinType argument; decides the rendered keyword (inner join, left join, ...).
_tableTableName | Select | RawNoThe joined table, set from the constructor's table argument.
_conditionsLogicalOperatorNoThe implicit top-level And built by .on().

Methods

on()

on(...conditions: SqlElement[]): this

Accumulates join conditions into an implicit top-level And, exactly like Select#where(). Omitting .on() (or calling it with no arguments) produces a join with no ON clause. See Operators and Conditions.

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

Select().from('t1').join(InnerJoin('t2').on(Eq('t2.id', Field('t1.id'))));
// ... inner join t2 on t2.id = t1.id

A sub-Select used as the joined table must carry an alias (.as()), or .generate() throws "Alias required for sub-select in Join".

See also