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 |
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 a model by its primary key. |
|
public |
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 |
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 |
Set the "limit" and "offset" for a given page. |
|
public |
Execute the query and return a promise that resolves with an array of models. |
|
public |
Add a "group by" clause to the query. |
|
public |
Add a "having" clause to the query. |
|
public |
Insert a new record into the database. |
|
public |
Add a "order by" latest date clause to the query. |
|
public |
Set the "limit" value of the query. |
|
public |
Get an array with the values of a given column. |
|
public |
Set the "offset" value of the query. |
|
public |
Add an "order by" oldest date clause to the query. |
|
public |
Add a "or having" clause to the query. |
|
public |
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 |
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 |
Add a "order by" clause to the query. |
|
public |
Add a scope call to the query. |
|
public |
Set the columns to be selected. |
|
public |
Set the "offset" value of the query. |
|
public |
Set the "limit" value of the query. |
|
public |
Execute the query as an "update" statement. |
|
public |
Get a single column's value from the first result of a query. |
|
public |
Add a "where" clause to the query. |
|
public |
whereBetween(args: ...*): Builder Add a "where between" clause to the query. |
|
public |
Add a date "where" clause to the query. |
|
public |
Add a day "where" clause to the query. |
|
public |
whereExists(args: ...*): Builder Add a "where exists" clause to the query. |
|
public |
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 |
Add a "where _ is null" clause to the query. |
|
public |
Add a year "where" clause to the query. |
|
public |
Set the relationships that should be eager loaded. |
Protected Methods | ||
protected |
Add a method call to the stack. |
|
protected |
The Model instance being queried |
|
protected |
The Model instance being queried |
Public Constructors
public constructor(connection: Connection, model: Model) source
Create a new Builder instance.
Params:
Name | Type | Attribute | Description |
connection | Connection | ||
model | Model |
|
Protected Members
Public Methods
public addSelect(columns: ...string): Builder source
Add a new select column to the query.
Params:
Name | Type | Attribute | Description |
columns | ...string |
public findMany(ids: number[], columns: string[]): Promise source
Find many models by their primary key.
public findOrFail(id: number, columns: string[]): Promise source
Find a model by its primary key or throw an exception.
public first(columns: string[]): Promise source
Execute the query and get the first result.
Params:
Name | Type | Attribute | Description |
columns | string[] |
|
public firstOrFail(columns: string[]): Promise source
Execute the query and get the first result or throw an exception.
Params:
Name | Type | Attribute | Description |
columns | string[] |
|
public forPage(forPage: ...number): Builder source
Set the "limit" and "offset" for a given page.
Params:
Name | Type | Attribute | Description |
forPage | ...number |
public get(columns: string | string[]): Promise source
Execute the query and return a promise that resolves with an array of models.
public groupBy(columns: ...string): Builder source
Add a "group by" clause to the query.
Params:
Name | Type | Attribute | Description |
columns | ...string |
public having(args: ...*): Builder source
Add a "having" clause to the query.
Params:
Name | Type | Attribute | Description |
args | ...* |
public insert(values: *): Promise source
Insert a new record into the database.
Params:
Name | Type | Attribute | Description |
values | * |
public latest(order: string): Builder source
Add a "order by" latest date clause to the query.
Params:
Name | Type | Attribute | Description |
order | string |
|
public limit(limit: number): Builder source
Set the "limit" value of the query.
Params:
Name | Type | Attribute | Description |
limit | number |
public lists(column: string): Promise source
Get an array with the values of a given column.
Params:
Name | Type | Attribute | Description |
column | string |
public offset(offset: number): Builder source
Set the "offset" value of the query.
Params:
Name | Type | Attribute | Description |
offset | number |
public oldest(order: string): Builder source
Add an "order by" oldest date clause to the query.
Params:
Name | Type | Attribute | Description |
order | string |
|
public orHaving(args: ...*): Builder source
Add a "or having" clause to the query.
Params:
Name | Type | Attribute | Description |
args | ...* |
public orWhere(args: ...*): Builder source
Add a "or where" clause to the query.
Params:
Name | Type | Attribute | Description |
args | ...* |
public orWhereBetween(args: ...*): Builder source
Add a "or where between" clause to the query.
Params:
Name | Type | Attribute | Description |
args | ...* |
public orWhereExists(args: ...*): Builder source
Add a "or where exists" clause to the query.
Params:
Name | Type | Attribute | Description |
args | ...* |
public orWhereIn(args: ...*): Builder source
Add a "or where in" clause to the query.
Params:
Name | Type | Attribute | Description |
args | ...* |
public orWhereNotBetween(args: ...*): Builder source
Add a "or where not between" clause to the query.
Params:
Name | Type | Attribute | Description |
args | ...* |
public orWhereNotExists(args: ...*): Builder source
Add a "or where not exists" clause to the query.
Params:
Name | Type | Attribute | Description |
args | ...* |
public orWhereNotIn(args: ...*): Builder source
Add a "or where not in" clause to the query.
Params:
Name | Type | Attribute | Description |
args | ...* |
public orWhereNotNull(args: ...*): Builder source
Add a "or where _ is not null" clause to the query.
Params:
Name | Type | Attribute | Description |
args | ...* |
public orWhereNull(args: ...*): Builder source
Add a "or where _ is null" clause to the query.
Params:
Name | Type | Attribute | Description |
args | ...* |
public orderBy(order: ...string): Builder source
Add a "order by" clause to the query.
Params:
Name | Type | Attribute | Description |
order | ...string |
public scope(scopeName: string, scopeArgs: *[]): Builder source
Add a scope call to the query.
Params:
Name | Type | Attribute | Description |
scopeName | string | ||
scopeArgs | *[] |
public select(columns: ...string): Builder source
Set the columns to be selected.
Params:
Name | Type | Attribute | Description |
columns | ...string |
public skip(skip: number): Builder source
Set the "offset" value of the query.
Params:
Name | Type | Attribute | Description |
skip | number |
public take(take: number): Builder source
Set the "limit" value of the query.
Params:
Name | Type | Attribute | Description |
take | number |
public update(values: object): Promise source
Execute the query as an "update" statement.
Params:
Name | Type | Attribute | Description |
values | object |
public value(column: string): Promise source
Get a single column's value from the first result of a query.
Params:
Name | Type | Attribute | Description |
column | string |
public where(args: ...*): Builder source
Add a "where" clause to the query.
Params:
Name | Type | Attribute | Description |
args | ...* |
public whereBetween(args: ...*): Builder source
Add a "where between" clause to the query.
Params:
Name | Type | Attribute | Description |
args | ...* |
public whereDate(args: ...*): Builder source
Add a date "where" clause to the query.
Params:
Name | Type | Attribute | Description |
args | ...* |
public whereDay(args: ...*): Builder source
Add a day "where" clause to the query.
Params:
Name | Type | Attribute | Description |
args | ...* |
public whereExists(args: ...*): Builder source
Add a "where exists" clause to the query.
Params:
Name | Type | Attribute | Description |
args | ...* |
public whereIn(args: ...*): Builder source
Add a "where in" clause to the query.
Params:
Name | Type | Attribute | Description |
args | ...* |
public whereMonth(args: ...*): Builder source
Add a month "where" clause to the query.
Params:
Name | Type | Attribute | Description |
args | ...* |
public whereNested(args: ...*): Builder source
Add a nested "where" clause to the query.
Params:
Name | Type | Attribute | Description |
args | ...* |
public whereNotBetween(args: ...*): Builder source
Add a "where not between" clause to the query.
Params:
Name | Type | Attribute | Description |
args | ...* |
public whereNotExists(args: ...*): Builder source
Add a "where not exists" clause to the query.
Params:
Name | Type | Attribute | Description |
args | ...* |
public whereNotIn(args: ...*): Builder source
Add a "where not in" clause to the query.
Params:
Name | Type | Attribute | Description |
args | ...* |
public whereNotNull(args: ...*): Builder source
Add a "where _ is not null" clause to the query.
Params:
Name | Type | Attribute | Description |
args | ...* |
public whereNull(args: ...*): Builder source
Add a "where _ is null" clause to the query.
Params:
Name | Type | Attribute | Description |
args | ...* |
public whereYear(args: ...*): Builder source
Add a year "where" clause to the query.
Params:
Name | Type | Attribute | Description |
args | ...* |
public with(relations: string[]): Builder source
Set the relationships that should be eager loaded.
Params:
Name | Type | Attribute | Description |
relations | string[] |