Home Reference Source Test Repository
public class | source

Builder

Builder provides a fluent API for building a query.

Since we'll be running the query on the server, we don't really care about grammars or processors, or even the breakdown of clauses and bindings. Instead, we'll just record which methods are called and with what arguments. This does mean the parameters in our function signatures and docs are not as helpful as they might be - check the Laravel documentation if any are unclear.

Test:

Constructor Summary

Public Constructor
public

constructor(connection: Connection, model: Model)

Create a new Builder instance.

Member Summary

Protected Members
protected

The Model instance being queried

protected

The connection class to send/receive the query/results.

protected

The methods called for this query and their arguments.

Method Summary

Public Methods
public

addSelect(columns: ...string): Builder

Add a new select column to the query.

public

Execute the query as a "delete" statement.

public

Force the query to only return distinct results.

public

find(id: number, columns: string[]): Promise

Find a model by its primary key.

public

findMany(ids: number[], columns: string[]): Promise

Find many models by their primary key.

public

findOrFail(id: number, columns: string[]): Promise

Find a model by its primary key or throw an exception.

public

first(columns: string[]): Promise

Execute the query and get the first result.

public

firstOrFail(columns: string[]): Promise

Execute the query and get the first result or throw an exception.

public

forPage(forPage: ...number): Builder

Set the "limit" and "offset" for a given page.

public

get(columns: string | string[]): Promise

Execute the query and return a promise that resolves with an array of models.

public

groupBy(columns: ...string): Builder

Add a "group by" clause to the query.

public

having(args: ...*): Builder

Add a "having" clause to the query.

public

insert(values: *): Promise

Insert a new record into the database.

public

latest(order: string): Builder

Add a "order by" latest date clause to the query.

public

limit(limit: number): Builder

Set the "limit" value of the query.

public

lists(column: string): Promise

Get an array with the values of a given column.

public

offset(offset: number): Builder

Set the "offset" value of the query.

public

oldest(order: string): Builder

Add an "order by" oldest date clause to the query.

public

orHaving(args: ...*): Builder

Add a "or having" clause to the query.

public

orWhere(args: ...*): Builder

Add a "or where" clause to the query.

public

orWhereBetween(args: ...*): Builder

Add a "or where between" clause to the query.

public

orWhereExists(args: ...*): Builder

Add a "or where exists" clause to the query.

public

orWhereIn(args: ...*): Builder

Add a "or where in" clause to the query.

public

orWhereNotBetween(args: ...*): Builder

Add a "or where not between" clause to the query.

public

orWhereNotExists(args: ...*): Builder

Add a "or where not exists" clause to the query.

public

orWhereNotIn(args: ...*): Builder

Add a "or where not in" clause to the query.

public

orWhereNotNull(args: ...*): Builder

Add a "or where _ is not null" clause to the query.

public

orWhereNull(args: ...*): Builder

Add a "or where _ is null" clause to the query.

public

orderBy(order: ...string): Builder

Add a "order by" clause to the query.

public

scope(scopeName: string, scopeArgs: *[]): Builder

Add a scope call to the query.

public

select(columns: ...string): Builder

Set the columns to be selected.

public

skip(skip: number): Builder

Set the "offset" value of the query.

public

take(take: number): Builder

Set the "limit" value of the query.

public

update(values: object): Promise

Execute the query as an "update" statement.

public

value(column: string): Promise

Get a single column's value from the first result of a query.

public

where(args: ...*): Builder

Add a "where" clause to the query.

public

whereBetween(args: ...*): Builder

Add a "where between" clause to the query.

public

whereDate(args: ...*): Builder

Add a date "where" clause to the query.

public

whereDay(args: ...*): Builder

Add a day "where" clause to the query.

public

whereExists(args: ...*): Builder

Add a "where exists" clause to the query.

public

whereIn(args: ...*): Builder

Add a "where in" clause to the query.

public

whereMonth(args: ...*): Builder

Add a month "where" clause to the query.

public

whereNested(args: ...*): Builder

Add a nested "where" clause to the query.

public

whereNotBetween(args: ...*): Builder

Add a "where not between" clause to the query.

public

whereNotExists(args: ...*): Builder

Add a "where not exists" clause to the query.

public

whereNotIn(args: ...*): Builder

Add a "where not in" clause to the query.

public

whereNotNull(args: ...*): Builder

Add a "where _ is not null" clause to the query.

public

whereNull(args: ...*): Builder

Add a "where _ is null" clause to the query.

public

whereYear(args: ...*): Builder

Add a year "where" clause to the query.

public

with(relations: string[]): Builder

Set the relationships that should be eager loaded.

Protected Methods
protected

_call(name: string, args: *[]): Builder

Add a method call to the stack.

protected

The Model instance being queried

protected

_setModel(model: Model): void

The Model instance being queried

Public Constructors

public constructor(connection: Connection, model: Model) source

Create a new Builder instance.

Params:

NameTypeAttributeDescription
connection Connection
model Model
  • optional

Protected Members

protected _model: Model | null source

The Model instance being queried

protected connection: Connection source

The connection class to send/receive the query/results.

protected stack: Array[] source

The methods called for this query and their arguments.

Public Methods

public addSelect(columns: ...string): Builder source

Add a new select column to the query.

Params:

NameTypeAttributeDescription
columns ...string

Return:

Builder

public delete(): Promise source

Execute the query as a "delete" statement.

Return:

Promise

public distinct(): Builder source

Force the query to only return distinct results.

Return:

Builder

public find(id: number, columns: string[]): Promise source

Find a model by its primary key.

Params:

NameTypeAttributeDescription
id number
columns string[]
  • optional

the columns to fetch

Return:

Promise

public findMany(ids: number[], columns: string[]): Promise source

Find many models by their primary key.

Params:

NameTypeAttributeDescription
ids number[]
columns string[]
  • optional

Return:

Promise

public findOrFail(id: number, columns: string[]): Promise source

Find a model by its primary key or throw an exception.

Params:

NameTypeAttributeDescription
id number
columns string[]
  • optional

Return:

Promise

public first(columns: string[]): Promise source

Execute the query and get the first result.

Params:

NameTypeAttributeDescription
columns string[]
  • optional

Return:

Promise

public firstOrFail(columns: string[]): Promise source

Execute the query and get the first result or throw an exception.

Params:

NameTypeAttributeDescription
columns string[]
  • optional

Return:

Promise

public forPage(forPage: ...number): Builder source

Set the "limit" and "offset" for a given page.

Params:

NameTypeAttributeDescription
forPage ...number

Return:

Builder

public get(columns: string | string[]): Promise source

Execute the query and return a promise that resolves with an array of models.

Params:

NameTypeAttributeDescription
columns string | string[]
  • optional

Return:

Promise

public groupBy(columns: ...string): Builder source

Add a "group by" clause to the query.

Params:

NameTypeAttributeDescription
columns ...string

Return:

Builder

public having(args: ...*): Builder source

Add a "having" clause to the query.

Params:

NameTypeAttributeDescription
args ...*

Return:

Builder

public insert(values: *): Promise source

Insert a new record into the database.

Params:

NameTypeAttributeDescription
values *

Return:

Promise

public latest(order: string): Builder source

Add a "order by" latest date clause to the query.

Params:

NameTypeAttributeDescription
order string
  • optional
  • default: 'created_at'

Return:

Builder

public limit(limit: number): Builder source

Set the "limit" value of the query.

Params:

NameTypeAttributeDescription
limit number

Return:

Builder

public lists(column: string): Promise source

Get an array with the values of a given column.

Params:

NameTypeAttributeDescription
column string

Return:

Promise

public offset(offset: number): Builder source

Set the "offset" value of the query.

Params:

NameTypeAttributeDescription
offset number

Return:

Builder

public oldest(order: string): Builder source

Add an "order by" oldest date clause to the query.

Params:

NameTypeAttributeDescription
order string
  • optional
  • default: 'created_at'

Return:

Builder

public orHaving(args: ...*): Builder source

Add a "or having" clause to the query.

Params:

NameTypeAttributeDescription
args ...*

Return:

Builder

public orWhere(args: ...*): Builder source

Add a "or where" clause to the query.

Params:

NameTypeAttributeDescription
args ...*

Return:

Builder

public orWhereBetween(args: ...*): Builder source

Add a "or where between" clause to the query.

Params:

NameTypeAttributeDescription
args ...*

Return:

Builder

public orWhereExists(args: ...*): Builder source

Add a "or where exists" clause to the query.

Params:

NameTypeAttributeDescription
args ...*

Return:

Builder

public orWhereIn(args: ...*): Builder source

Add a "or where in" clause to the query.

Params:

NameTypeAttributeDescription
args ...*

Return:

Builder

public orWhereNotBetween(args: ...*): Builder source

Add a "or where not between" clause to the query.

Params:

NameTypeAttributeDescription
args ...*

Return:

Builder

public orWhereNotExists(args: ...*): Builder source

Add a "or where not exists" clause to the query.

Params:

NameTypeAttributeDescription
args ...*

Return:

Builder

public orWhereNotIn(args: ...*): Builder source

Add a "or where not in" clause to the query.

Params:

NameTypeAttributeDescription
args ...*

Return:

Builder

public orWhereNotNull(args: ...*): Builder source

Add a "or where _ is not null" clause to the query.

Params:

NameTypeAttributeDescription
args ...*

Return:

Builder

public orWhereNull(args: ...*): Builder source

Add a "or where _ is null" clause to the query.

Params:

NameTypeAttributeDescription
args ...*

Return:

Builder

public orderBy(order: ...string): Builder source

Add a "order by" clause to the query.

Params:

NameTypeAttributeDescription
order ...string

Return:

Builder

public scope(scopeName: string, scopeArgs: *[]): Builder source

Add a scope call to the query.

Params:

NameTypeAttributeDescription
scopeName string
scopeArgs *[]

Return:

Builder

public select(columns: ...string): Builder source

Set the columns to be selected.

Params:

NameTypeAttributeDescription
columns ...string

Return:

Builder

public skip(skip: number): Builder source

Set the "offset" value of the query.

Params:

NameTypeAttributeDescription
skip number

Return:

Builder

public take(take: number): Builder source

Set the "limit" value of the query.

Params:

NameTypeAttributeDescription
take number

Return:

Builder

public update(values: object): Promise source

Execute the query as an "update" statement.

Params:

NameTypeAttributeDescription
values object

Return:

Promise

public value(column: string): Promise source

Get a single column's value from the first result of a query.

Params:

NameTypeAttributeDescription
column string

Return:

Promise

public where(args: ...*): Builder source

Add a "where" clause to the query.

Params:

NameTypeAttributeDescription
args ...*

Return:

Builder

public whereBetween(args: ...*): Builder source

Add a "where between" clause to the query.

Params:

NameTypeAttributeDescription
args ...*

Return:

Builder

public whereDate(args: ...*): Builder source

Add a date "where" clause to the query.

Params:

NameTypeAttributeDescription
args ...*

Return:

Builder

public whereDay(args: ...*): Builder source

Add a day "where" clause to the query.

Params:

NameTypeAttributeDescription
args ...*

Return:

Builder

public whereExists(args: ...*): Builder source

Add a "where exists" clause to the query.

Params:

NameTypeAttributeDescription
args ...*

Return:

Builder

public whereIn(args: ...*): Builder source

Add a "where in" clause to the query.

Params:

NameTypeAttributeDescription
args ...*

Return:

Builder

public whereMonth(args: ...*): Builder source

Add a month "where" clause to the query.

Params:

NameTypeAttributeDescription
args ...*

Return:

Builder

public whereNested(args: ...*): Builder source

Add a nested "where" clause to the query.

Params:

NameTypeAttributeDescription
args ...*

Return:

Builder

public whereNotBetween(args: ...*): Builder source

Add a "where not between" clause to the query.

Params:

NameTypeAttributeDescription
args ...*

Return:

Builder

public whereNotExists(args: ...*): Builder source

Add a "where not exists" clause to the query.

Params:

NameTypeAttributeDescription
args ...*

Return:

Builder

public whereNotIn(args: ...*): Builder source

Add a "where not in" clause to the query.

Params:

NameTypeAttributeDescription
args ...*

Return:

Builder

public whereNotNull(args: ...*): Builder source

Add a "where _ is not null" clause to the query.

Params:

NameTypeAttributeDescription
args ...*

Return:

Builder

public whereNull(args: ...*): Builder source

Add a "where _ is null" clause to the query.

Params:

NameTypeAttributeDescription
args ...*

Return:

Builder

public whereYear(args: ...*): Builder source

Add a year "where" clause to the query.

Params:

NameTypeAttributeDescription
args ...*

Return:

Builder

public with(relations: string[]): Builder source

Set the relationships that should be eager loaded.

Params:

NameTypeAttributeDescription
relations string[]

Return:

Builder

Protected Methods

protected _call(name: string, args: *[]): Builder source

Add a method call to the stack.

Params:

NameTypeAttributeDescription
name string
args *[]

Return:

Builder

protected _getModel(): Model source

The Model instance being queried

Return:

Model

model

protected _setModel(model: Model): void source

The Model instance being queried

Params:

NameTypeAttributeDescription
model Model

Return:

void