Options
All
  • Public
  • Public/Protected
  • All
Menu

Class JoinClause

Represents a QueryBuilder for a nested join clause. We essentially abuse QueryBuilder's WHERE building since it works exactly the same as the ON clause of a join.

Hierarchy

Index

Constructors

constructor

Properties

aggregateFunction

aggregateFunction: string | null = null

The aggregate function for this query. If this is set, it is assumed that this query selects of format SELECT fun(columns) AS aggregate.

columns

columns: string[] = ["*"]

The columns we are currently retrieving.

groups

groups: string[] = []

All the GROUP BY columns for the query.

isDistinct

isDistinct: boolean = false

If we are only looking for distinct values.

joiningOn

joiningOn: string

joins

joins: JoinClause[] = []

All joins for the current query.

limitCount

limitCount: number = -1

The LIMIT clause for the query, or -1 if not applicable.

modelConstructor

modelConstructor: Function | null

The constructor of the model we are currently querying. If this is non-null, we are still operating on the full model (no narrowed down views) which means that once we have fetched the objects we can assign this prototype to ensure that the returned instance supports any user-defined methods (along with defaults such as save).

orders

orders: object[] = []

All the ORDER BY clauses for the query.

table

table: string

Which table we are currently querying.

type

type: JoinType

wheres

wheres: WhereQueryClause[] = []

All where clauses for the current query.

Methods

aggregate

  • aggregate(fn: string, column?: undefined | string): Promise<number>
  • Runs the specified aggregate function and returns the result.

    Parameters

    • fn: string
    • Optional column: undefined | string

    Returns Promise<number>

all

  • all(): Promise<any[]>

avg

  • avg(column?: undefined | string): Promise<number>
  • Returns the average of the specified column, or the current column by default.

    Parameters

    • Optional column: undefined | string

    Returns Promise<number>

count

  • count(): Promise<number>

Protected createNew

  • createNew(): this

delete

  • delete(): Promise<void>

distinct

  • distinct(value?: boolean): this
  • Marks this query as looking for distinct values. Use distinct(false) to undo.

    Parameters

    • Default value value: boolean = true

    Returns this

exists

  • exists(): Promise<boolean>

find

  • find(id: number): Promise<any | undefined>
  • Finds the first row with the specified ID. Alias for where("id", id).first()

    Parameters

    • id: number

    Returns Promise<any | undefined>

first

  • first(): Promise<any | undefined>

from

  • Changes the table this query is currently acting on to the table associated with the specified model.

    Type parameters

    Parameters

    • model: M

    Returns QueryBuilder<A>

  • Changes the table this query is currently acting on to the specified table. An optional type parameter can be used to set the scheme of the table so that further operations are as type-safe as possible.

    Type parameters

    • M

    Parameters

    • table: string

    Returns QueryBuilder<M>

get

  • get(): Promise<any[]>

groupBy

  • groupBy<K>(...groups: K[]): this
  • Marks the query to group the results by the provided column names.

    Type parameters

    • K: keyof T

    Parameters

    • Rest ...groups: K[]

    Returns this

insert

  • insert(...entries: Partial<any>[]): Promise<void>

insertAndGetId

  • insertAndGetId(entry: Partial<any>): Promise<number>
  • Essentially the same as insert(), but returns the ID of the inserted row and only supports inserting a single row at a time.

    Parameters

    • entry: Partial<any>

    Returns Promise<number>

join

  • Adds a new INNER JOIN with the specified table on the specified columns and operators. This operation loses type-safety since it is impossible to determine the return type of the join statically.

    Parameters

    • table: string
    • first: string
    • operator: Operator
    • second: string
    • Optional type: JoinType

    Returns QueryBuilder<KeyedDatabaseResult>

  • Adds a new nested INNER JOIN with the specified table. The handler receives a JoinClause which it can use to build the new join.

    Parameters

    • table: string
    • handler: function

    Returns QueryBuilder<KeyedDatabaseResult>

  • Adds a new INNER JOIN with the specified model and the specified columns and operator. This operation is type safe and returns a union of the current fields and the fields of the specified model.

    Type parameters

    Parameters

    • model: M
    • first: string
    • operator: Operator
    • second: string
    • Optional type: JoinType

    Returns QueryBuilder<any & A>

  • Adds a new nested INNER JOIN with the specified model. The handler receives a JoinClause which it can use to build the new join. This operation is type safe and returns a union of the current fields and the fields of the specified model.

    Type parameters

    Parameters

    Returns QueryBuilder<any & A>

leftJoin

  • Adds a new LEFT JOIN with the specified table on the specified columns and operators. This operation loses type-safety since it is impossible to determine the return type of the join statically.

    Parameters

    • table: string
    • first: string
    • operator: Operator
    • second: string

    Returns QueryBuilder<KeyedDatabaseResult>

  • Adds a new nested LEFT JOIN with the specified table. The handler receives a JoinClause which it can use to build the new join.

    Parameters

    • table: string
    • handler: function

    Returns QueryBuilder<KeyedDatabaseResult>

  • Adds a new LEFT JOIN with the specified model and the specified columns and operator. This operation is type safe and returns a union of the current fields and the fields of the specified model.

    Type parameters

    Parameters

    • model: M
    • first: string
    • operator: Operator
    • second: string

    Returns QueryBuilder<any & A>

  • Adds a new nested LEFT JOIN with the specified model. The handler receives a JoinClause which it can use to build the new join. This operation is type safe and returns a union of the current fields and the fields of the specified model.

    Type parameters

    Parameters

    Returns QueryBuilder<any & A>

limit

  • limit(count: number): this
  • Limits the amount of rows this query targets to the specified amount. Must be positive non-null.

    Parameters

    • count: number

    Returns this

max

  • max(column?: undefined | string): Promise<number>
  • Returns the maximum value of the specified column, or the current column by default.

    Parameters

    • Optional column: undefined | string

    Returns Promise<number>

min

  • min(column?: undefined | string): Promise<number>
  • Returns the minimum value of the specified column, or the current column by default.

    Parameters

    • Optional column: undefined | string

    Returns Promise<number>

on

  • on(method: function): this
  • on(first: string, operator: Operator, second: string, boolean?: QueryBoolean): this
  • Creates a new nested ON clause. The specified callback receives a nested query builder that can be used to build the nested queries.

    Parameters

    • method: function
        • (builder: this): any
        • Parameters

          • builder: this

          Returns any

    Returns this

  • Adds a new ON clause for the specified column, operator and value. Optionally you can provide a QueryBoolean of "OR" instead of "AND", but it is recommended that you use orOn instead.

    Parameters

    Returns this

orOn

  • orOn(method: function): this
  • orOn(first: string, operator: Operator, second: string): this
  • Creates a new nested OR ON clause. The specified callback receives a nested query builder that can be used to build the nested queries.

    Parameters

    • method: function
        • (): any
        • Returns any

    Returns this

  • Adds a new OR ON clause for the specified column, operator and value.

    Parameters

    • first: string
    • operator: Operator
    • second: string

    Returns this

orWhere

  • orWhere(handler: function): this
  • orWhere<K>(column: K, value: T[K]): this
  • orWhere<K>(column: K, operator: Operator, value: T[K]): this
  • Creates a new nested WHERE OR clause. The specified callback receives a nested query builder that can be used to build the nested queries.

    Parameters

    • handler: function
        • (builder: this): any
        • Parameters

          • builder: this

          Returns any

    Returns this

  • Adds a new WHERE OR clause for the specified column, requiring that it matches the specified variable. This is an alias for where(column, "=", value).

    Type parameters

    • K: keyof T

    Parameters

    • column: K
    • value: T[K]

    Returns this

  • Adds a new WHERE OR clause for the specified column, operator and value. Use where if you want to add a WHERE AND clause instead.

    Type parameters

    • K: keyof T

    Parameters

    • column: K
    • operator: Operator
    • value: T[K]

    Returns this

orWhereColumn

  • orWhereColumn<K1, K2>(first: K1, operator: Operator, second: K2): this
  • Adds a new WHERE OR clause comparing two columns of the current query. This uses OR, use whereColumn if you want AND instead.

    Type parameters

    • K1: keyof T

    • K2: keyof T

    Parameters

    • first: K1
    • operator: Operator
    • second: K2

    Returns this

orWhereNested

  • orWhereNested(handler: function): this

orWhereNotNull

  • orWhereNotNull<K>(column: K): this

orWhereNull

  • orWhereNull<K>(column: K): this

orWhereRaw

orderBy

  • Marks the query to order the results by the specified column in the specified direction, or ASC (ascending) by default.

    Type parameters

    • K: keyof T

    Parameters

    Returns this

orderByAsc

  • orderByAsc<K>(column: K): this
  • Marks the query to order the results by the specified column in ascending direction.

    Type parameters

    • K: keyof T

    Parameters

    • column: K

    Returns this

orderByDesc

  • orderByDesc<K>(column: K): this
  • Marks the query to order the results by the specified column in descending direction.

    Type parameters

    • K: keyof T

    Parameters

    • column: K

    Returns this

pluck

  • pluck<K>(column: K): Promise<T[K][]>
  • Returns only the specified column of all rows this query currently targets.

    Type parameters

    • K: keyof T

    Parameters

    • column: K

    Returns Promise<T[K][]>

rightJoin

  • Adds a new RIGHT JOIN with the specified table on the specified columns and operators. This operation loses type-safety since it is impossible to determine the return type of the join statically.

    Parameters

    • table: string
    • first: string
    • operator: Operator
    • second: string

    Returns QueryBuilder<KeyedDatabaseResult>

  • Adds a new nested RIGHT JOIN with the specified table. The handler receives a JoinClause which it can use to build the new join.

    Parameters

    • table: string
    • handler: function

    Returns QueryBuilder<KeyedDatabaseResult>

  • Adds a new RIGHT JOIN with the specified model and the specified columns and operator. This operation is type safe and returns a union of the current fields and the fields of the specified model.

    Type parameters

    Parameters

    • model: M
    • first: string
    • operator: Operator
    • second: string

    Returns QueryBuilder<any & A>

  • Adds a new nested RIGHT JOIN with the specified model. The handler receives a JoinClause which it can use to build the new join. This operation is type safe and returns a union of the current fields and the fields of the specified model.

    Type parameters

    Parameters

    Returns QueryBuilder<any & A>

select

sum

  • sum(column?: undefined | string): Promise<number>
  • Returns the sum of the specified column, or the current column by default.

    Parameters

    • Optional column: undefined | string

    Returns Promise<number>

update

  • update(values: Partial<any>): Promise<void>
  • Updates the specified values for all rows matching the current query.

    Parameters

    • values: Partial<any>

    Returns Promise<void>

value

  • value<K>(column: K): Promise<T[K]>
  • Returns the specified column of the first result returned by this query.

    Type parameters

    • K: keyof T

    Parameters

    • column: K

    Returns Promise<T[K]>

where

  • where(handler: function): this
  • where<K>(column: K, value: T[K]): this
  • where<K>(column: K, operator: Operator, value: T[K], boolean?: QueryBoolean): this
  • Creates a new nested where clause. The specified callback receives a nested query builder that can be used to build the nested queries.

    Parameters

    • handler: function
        • (builder: this): any
        • Parameters

          • builder: this

          Returns any

    Returns this

  • Adds a new where clause for the specified column, requiring that it matches the specified variable. This is an alias for where(column, "=", value).

    Type parameters

    • K: keyof T

    Parameters

    • column: K
    • value: T[K]

    Returns this

  • Adds a new where clause for the specified column, operator and value. Optionally you can provide a QueryBoolean of "OR" instead of "AND", but it is recommended that you use orWhere instead.

    Type parameters

    • K: keyof T

    Parameters

    Returns this

whereColumn

  • Adds a new where clause comparing two columns of the current query. This uses AND as boolean by default, use orWhereColumn if you want OR instead.

    Type parameters

    • K1: keyof T

    • K2: keyof T

    Parameters

    Returns this

whereNested

  • whereNested(handler: function, boolean?: QueryBoolean): this

whereNotNull

  • whereNotNull<K>(column: K): this

whereNull

  • whereNull<K>(column: K, boolean?: QueryBoolean, negate?: boolean): this
  • Adds a new where clause asserting that the specified column is NULL.

    Type parameters

    • K: keyof T

    Parameters

    • column: K
    • Default value boolean: QueryBoolean = "AND"
    • Default value negate: boolean = false

    Returns this

whereRaw

Static execute

  • execute(sql: string, args?: any[]): Promise<void>

Static model

Static table

Generated using TypeDoc