Skip to main content
Version: 4.4

Class: QueryBuilder<T>#

knex.QueryBuilder

SQL query builder

Type parameters#

NameTypeDefault
TAnyEntity<T>AnyEntity

Hierarchy#

  • QueryBuilder

Constructors#

constructor#

+ new QueryBuilder<T>(entityName: string, metadata: MetadataStorage, driver: AbstractSqlDriver<AbstractSqlConnection>, context?: Transaction<any, any>, alias?: string, connectionType?: read | write, em?: EntityManager<AbstractSqlDriver<AbstractSqlConnection>>): QueryBuilder<T>

Type parameters:#

NameTypeDefault
TAnyEntity<T>AnyEntity<any\>

Parameters:#

NameTypeDefault value
entityNamestring-
metadataMetadataStorage-
driverAbstractSqlDriver<AbstractSqlConnection>-
context?Transaction<any, any>-
aliasstring...
connectionType?read | write-
em?EntityManager<AbstractSqlDriver<AbstractSqlConnection>>-

Returns: QueryBuilder<T>

Defined in: packages/knex/src/query/QueryBuilder.ts:59

Properties#

_aliasMap#

Private _aliasMap: Dictionary<string>

Defined in: packages/knex/src/query/QueryBuilder.ts:43


_cache#

Private Optional _cache: undefined | number | boolean | [string, number]

Defined in: packages/knex/src/query/QueryBuilder.ts:54


_cond#

Private _cond: Dictionary<any>

Defined in: packages/knex/src/query/QueryBuilder.ts:45


_data#

Private _data: Dictionary<any>

Defined in: packages/knex/src/query/QueryBuilder.ts:46


_fields#

Optional _fields: undefined | Field<T>[]

Defined in: packages/knex/src/query/QueryBuilder.ts:35


_groupBy#

Private _groupBy: Field<T>[]

Defined in: packages/knex/src/query/QueryBuilder.ts:48


_having#

Private _having: Dictionary<any>

Defined in: packages/knex/src/query/QueryBuilder.ts:49


_joinedProps#

Private _joinedProps: Map<string, PopulateOptions<any>>

Defined in: packages/knex/src/query/QueryBuilder.ts:53


_joins#

Private _joins: Dictionary<JoinOptions>

Defined in: packages/knex/src/query/QueryBuilder.ts:42


_limit#

Private Optional _limit: undefined | number

Defined in: packages/knex/src/query/QueryBuilder.ts:51


_offset#

Private Optional _offset: undefined | number

Defined in: packages/knex/src/query/QueryBuilder.ts:52


_onConflict#

Private Optional _onConflict: undefined | { fields: string[] ; ignore?: undefined | boolean ; merge?: undefined | EntityData<T> ; where?: undefined | { __@PrimaryKeyType@41543?: any } | { __@PrimaryKeyType@41543?: any } & Dictionary<any> | NonNullable<Query<T>> | NonNullable<Query<T>> & Dictionary<any> }[]

Defined in: packages/knex/src/query/QueryBuilder.ts:50


_orderBy#

Private _orderBy: QueryOrderMap

Defined in: packages/knex/src/query/QueryBuilder.ts:47


_populate#

_populate: PopulateOptions<T>[]

Defined in: packages/knex/src/query/QueryBuilder.ts:36


_populateMap#

_populateMap: Dictionary<string>

Defined in: packages/knex/src/query/QueryBuilder.ts:37


_schema#

Private Optional _schema: undefined | string

Defined in: packages/knex/src/query/QueryBuilder.ts:44


alias#

Readonly alias: string


aliasCounter#

Private aliasCounter: number= 1

Defined in: packages/knex/src/query/QueryBuilder.ts:39


finalized#

Private finalized: boolean= false

Defined in: packages/knex/src/query/QueryBuilder.ts:41


flags#

Private flags: Set<QueryFlag>

Defined in: packages/knex/src/query/QueryBuilder.ts:40


helper#

Private Readonly helper: QueryBuilderHelper

Defined in: packages/knex/src/query/QueryBuilder.ts:59


knex#

Private Readonly knex: Knex<any, unknown[]>

Defined in: packages/knex/src/query/QueryBuilder.ts:58


lockMode#

Private Optional lockMode: undefined | NONE | OPTIMISTIC | PESSIMISTIC_READ | PESSIMISTIC_WRITE

Defined in: packages/knex/src/query/QueryBuilder.ts:55


platform#

Private Readonly platform: AbstractSqlPlatform

Defined in: packages/knex/src/query/QueryBuilder.ts:57


subQueries#

Private subQueries: Dictionary<string>

Defined in: packages/knex/src/query/QueryBuilder.ts:56


type#

type: QueryType

Defined in: packages/knex/src/query/QueryBuilder.ts:34

Methods#

addSelect#

addSelect(fields: string | KnexStringRef | QueryBuilder<any, any> | keyof T | Field<T>[]): QueryBuilder<T>

Parameters:#

NameType
fieldsstring | KnexStringRef | QueryBuilder<any, any> | keyof T | Field<T>[]

Returns: QueryBuilder<T>

Defined in: packages/knex/src/query/QueryBuilder.ts:81


andWhere#

andWhere(cond: QBFilterQuery<T>): QueryBuilder<T>

Parameters:#

NameType
condQBFilterQuery<T>

Returns: QueryBuilder<T>

Defined in: packages/knex/src/query/QueryBuilder.ts:197

andWhere(cond: string, params?: any[]): QueryBuilder<T>

Parameters:#

NameType
condstring
params?any[]

Returns: QueryBuilder<T>

Defined in: packages/knex/src/query/QueryBuilder.ts:198


as#

as(alias: string): QueryBuilder<any, any>

Returns knex instance with sub-query aliased with given alias. You can provide EntityName.propName as alias, then the field name will be used based on the metadata

Parameters:#

NameType
aliasstring

Returns: QueryBuilder<any, any>

Defined in: packages/knex/src/query/QueryBuilder.ts:459


autoJoinPivotTable#

PrivateautoJoinPivotTable(field: string): void

Parameters:#

NameType
fieldstring

Returns: void

Defined in: packages/knex/src/query/QueryBuilder.ts:713


cache#

cache(config?: number | boolean | [string, number]): QueryBuilder<T>

Parameters:#

NameTypeDefault value
confignumber | boolean | [string, number]true

Returns: QueryBuilder<T>

Defined in: packages/knex/src/query/QueryBuilder.ts:309


clone#

clone(): QueryBuilder<T>

Returns: QueryBuilder<T>

Defined in: packages/knex/src/query/QueryBuilder.ts:472


count#

count(field?: string | string[], distinct?: boolean): QueryBuilder<T>

Parameters:#

NameTypeDefault value
field?string | string[]-
distinctbooleanfalse

Returns: QueryBuilder<T>

Defined in: packages/knex/src/query/QueryBuilder.ts:105


delete#

delete(cond?: any): QueryBuilder<T>

Parameters:#

NameTypeDefault value
condany...

Returns: QueryBuilder<T>

Defined in: packages/knex/src/query/QueryBuilder.ts:97


execute#

execute<U>(method?: all | get | run, mapResults?: boolean): Promise<U>

Executes this QB and returns the raw results, mapped to the property names (unless disabled via last parameter). Use method to specify what kind of result you want to get (array/single/meta).

Type parameters:#

NameDefault
Uany

Parameters:#

NameTypeDefault value
methodall | get | run'all'
mapResultsbooleantrue

Returns: Promise<U>

Defined in: packages/knex/src/query/QueryBuilder.ts:391


finalize#

Privatefinalize(): void

Returns: void

Defined in: packages/knex/src/query/QueryBuilder.ts:626


getAliasForJoinPath#

getAliasForJoinPath(path?: string): undefined | string

Parameters:#

NameType
path?string

Returns: undefined | string

Defined in: packages/knex/src/query/QueryBuilder.ts:367


getFieldsForJoinedLoad#

ProtectedgetFieldsForJoinedLoad<U>(prop: EntityProperty<U>, alias: string): Field<U>[]

Type parameters:#

NameType
UAnyEntity<U>

Parameters:#

NameType
propEntityProperty<U>
aliasstring

Returns: Field<U>[]

Defined in: packages/knex/src/query/QueryBuilder.ts:146


getFormattedQuery#

getFormattedQuery(): string

Returns raw interpolated query string with all the parameters inlined.

Returns: string

Defined in: packages/knex/src/query/QueryBuilder.ts:362


getKnex#

getKnex(): QueryBuilder<any, any>

Returns: QueryBuilder<any, any>

Defined in: packages/knex/src/query/QueryBuilder.ts:484


getKnexQuery#

getKnexQuery(): QueryBuilder<any, any>

Returns: QueryBuilder<any, any>

Defined in: packages/knex/src/query/QueryBuilder.ts:314


getNextAlias#

getNextAlias(prefix?: string): string

Parameters:#

NameTypeDefault value
prefixstring'e'

Returns: string

Defined in: packages/knex/src/query/QueryBuilder.ts:382


getParams#

getParams(): readonly Value[]

Returns the list of all parameters for this query.

Returns: readonly Value[]

Defined in: packages/knex/src/query/QueryBuilder.ts:355


getQuery#

getQuery(): string

Returns the query with parameters as wildcards.

Returns: string

Defined in: packages/knex/src/query/QueryBuilder.ts:348


getQueryBase#

PrivategetQueryBase(): QueryBuilder<any, any>

Returns: QueryBuilder<any, any>

Defined in: packages/knex/src/query/QueryBuilder.ts:585


getResult#

getResult(): Promise<T[]>

Alias for qb.getResultList()

Returns: Promise<T[]>

Defined in: packages/knex/src/query/QueryBuilder.ts:430


getResultList#

getResultList(): Promise<T[]>

Executes the query, returning array of results

Returns: Promise<T[]>

Defined in: packages/knex/src/query/QueryBuilder.ts:437


getSingleResult#

getSingleResult(): Promise<null | T>

Executes the query, returning the first result or null

Returns: Promise<null | T>

Defined in: packages/knex/src/query/QueryBuilder.ts:450


groupBy#

groupBy(fields: string | keyof T | (string | keyof T)[]): QueryBuilder<T>

Parameters:#

NameType
fieldsstring | keyof T | (string | keyof T)[]

Returns: QueryBuilder<T>

Defined in: packages/knex/src/query/QueryBuilder.ts:215


having#

having(cond?: any, params?: any[]): QueryBuilder<T>

Parameters:#

NameTypeDefault value
condany...
params?any[]-

Returns: QueryBuilder<T>

Defined in: packages/knex/src/query/QueryBuilder.ts:220


ignore#

ignore(): QueryBuilder<T>

Returns: QueryBuilder<T>

Defined in: packages/knex/src/query/QueryBuilder.ts:235


init#

Privateinit(type: QueryType, data?: any, cond?: any): QueryBuilder<T>

Parameters:#

NameType
typeQueryType
data?any
cond?any

Returns: QueryBuilder<T>

Defined in: packages/knex/src/query/QueryBuilder.ts:566


insert#

insert(data: EntityData<T>): QueryBuilder<T>

Parameters:#

NameType
dataEntityData<T>

Returns: QueryBuilder<T>

Defined in: packages/knex/src/query/QueryBuilder.ts:89


join#

join(field: string, alias: string, cond?: any, type?: leftJoin | innerJoin | pivotJoin, path?: string): QueryBuilder<T>

Parameters:#

NameTypeDefault value
fieldstring-
aliasstring-
condany...
typeleftJoin | innerJoin | pivotJoin'innerJoin'
path?string-

Returns: QueryBuilder<T>

Defined in: packages/knex/src/query/QueryBuilder.ts:115


joinAndSelect#

joinAndSelect(field: string, alias: string, cond?: any, type?: leftJoin | innerJoin | pivotJoin, path?: string): QueryBuilder<T>

Parameters:#

NameTypeDefault value
fieldstring-
aliasstring-
condany...
typeleftJoin | innerJoin | pivotJoin'innerJoin'
path?string-

Returns: QueryBuilder<T>

Defined in: packages/knex/src/query/QueryBuilder.ts:124


joinReference#

PrivatejoinReference(field: string, alias: string, cond: Dictionary<any>, type: leftJoin | innerJoin | pivotJoin, path?: string): EntityProperty<any>

Parameters:#

NameType
fieldstring
aliasstring
condDictionary<any>
typeleftJoin | innerJoin | pivotJoin
path?string

Returns: EntityProperty<any>

Defined in: packages/knex/src/query/QueryBuilder.ts:495


leftJoin#

leftJoin(field: string, alias: string, cond?: any): QueryBuilder<T>

Parameters:#

NameTypeDefault value
fieldstring-
aliasstring-
condany...

Returns: QueryBuilder<T>

Defined in: packages/knex/src/query/QueryBuilder.ts:120


leftJoinAndSelect#

leftJoinAndSelect(field: string, alias: string, cond?: any): QueryBuilder<T>

Parameters:#

NameTypeDefault value
fieldstring-
aliasstring-
condany...

Returns: QueryBuilder<T>

Defined in: packages/knex/src/query/QueryBuilder.ts:142


limit#

limit(limit?: number, offset?: number): QueryBuilder<T>

Parameters:#

NameTypeDefault value
limit?number-
offsetnumber0

Returns: QueryBuilder<T>

Defined in: packages/knex/src/query/QueryBuilder.ts:268


merge#

merge(data?: EntityData<T>): QueryBuilder<T>

Parameters:#

NameType
data?EntityData<T>

Returns: QueryBuilder<T>

Defined in: packages/knex/src/query/QueryBuilder.ts:240


offset#

offset(offset?: number): QueryBuilder<T>

Parameters:#

NameType
offset?number

Returns: QueryBuilder<T>

Defined in: packages/knex/src/query/QueryBuilder.ts:278


onConflict#

onConflict(fields: string | string[]): QueryBuilder<T>

Parameters:#

NameType
fieldsstring | string[]

Returns: QueryBuilder<T>

Defined in: packages/knex/src/query/QueryBuilder.ts:229


orWhere#

orWhere(cond: QBFilterQuery<T>): QueryBuilder<T>

Parameters:#

NameType
condQBFilterQuery<T>

Returns: QueryBuilder<T>

Defined in: packages/knex/src/query/QueryBuilder.ts:203

orWhere(cond: string, params?: any[]): QueryBuilder<T>

Parameters:#

NameType
condstring
params?any[]

Returns: QueryBuilder<T>

Defined in: packages/knex/src/query/QueryBuilder.ts:204


orderBy#

orderBy(orderBy: QueryOrderMap): QueryBuilder<T>

Parameters:#

NameType
orderByQueryOrderMap

Returns: QueryBuilder<T>

Defined in: packages/knex/src/query/QueryBuilder.ts:209


populate#

populate(populate: PopulateOptions<T>[]): QueryBuilder<T>

internal

Parameters:#

NameType
populatePopulateOptions<T>[]

Returns: QueryBuilder<T>

Defined in: packages/knex/src/query/QueryBuilder.ts:248


prepareFields#

PrivateprepareFields<T, U>(fields: Field<T>[], type?: groupBy | where | sub-query): U[]

Type parameters:#

NameTypeDefault
TAnyEntity<T>-
Ustring | Raw<any>string \

Parameters:#

NameTypeDefault value
fieldsField<T>[]-
typegroupBy | where | sub-query'where'

Returns: U[]

Defined in: packages/knex/src/query/QueryBuilder.ts:536


raw#

raw(sql: string): Raw<any>

Parameters:#

NameType
sqlstring

Returns: Raw<any>

Defined in: packages/knex/src/query/QueryBuilder.ts:261


ref#

ref(field: string): Ref<string, {}>

internal

Parameters:#

NameType
fieldstring

Returns: Ref<string, {}>

Defined in: packages/knex/src/query/QueryBuilder.ts:257


select#

select(fields: string | KnexStringRef | QueryBuilder<any, any> | keyof T | Field<T>[], distinct?: boolean): QueryBuilder<T>

Parameters:#

NameTypeDefault value
fieldsstring | KnexStringRef | QueryBuilder<any, any> | keyof T | Field<T>[]-
distinctbooleanfalse

Returns: QueryBuilder<T>

Defined in: packages/knex/src/query/QueryBuilder.ts:71


setFlag#

setFlag(flag: QueryFlag): QueryBuilder<T>

Parameters:#

NameType
flagQueryFlag

Returns: QueryBuilder<T>

Defined in: packages/knex/src/query/QueryBuilder.ts:299


setLockMode#

setLockMode(mode?: NONE | OPTIMISTIC | PESSIMISTIC_READ | PESSIMISTIC_WRITE): QueryBuilder<T>

Parameters:#

NameType
mode?NONE | OPTIMISTIC | PESSIMISTIC_READ | PESSIMISTIC_WRITE

Returns: QueryBuilder<T>

Defined in: packages/knex/src/query/QueryBuilder.ts:289


truncate#

truncate(): QueryBuilder<T>

Returns: QueryBuilder<T>

Defined in: packages/knex/src/query/QueryBuilder.ts:101


unsetFlag#

unsetFlag(flag: QueryFlag): QueryBuilder<T>

Parameters:#

NameType
flagQueryFlag

Returns: QueryBuilder<T>

Defined in: packages/knex/src/query/QueryBuilder.ts:304


update#

update(data: EntityData<T>): QueryBuilder<T>

Parameters:#

NameType
dataEntityData<T>

Returns: QueryBuilder<T>

Defined in: packages/knex/src/query/QueryBuilder.ts:93


where#

where(cond: QBFilterQuery<T>, operator?: $and | $or): QueryBuilder<T>

Parameters:#

NameType
condQBFilterQuery<T>
operator?$and | $or

Returns: QueryBuilder<T>

Defined in: packages/knex/src/query/QueryBuilder.ts:160

where(cond: string, params?: any[], operator?: $and | $or): QueryBuilder<T>

Parameters:#

NameType
condstring
params?any[]
operator?$and | $or

Returns: QueryBuilder<T>

Defined in: packages/knex/src/query/QueryBuilder.ts:161


withSchema#

withSchema(schema?: string): QueryBuilder<T>

Parameters:#

NameType
schema?string

Returns: QueryBuilder<T>

Defined in: packages/knex/src/query/QueryBuilder.ts:283


withSubQuery#

withSubQuery(subQuery: QueryBuilder<any, any>, alias: string): QueryBuilder<T>

Parameters:#

NameType
subQueryQueryBuilder<any, any>
aliasstring

Returns: QueryBuilder<T>

Defined in: packages/knex/src/query/QueryBuilder.ts:155


wrapModifySubQuery#

PrivatewrapModifySubQuery(meta: EntityMetadata<any>): void

Parameters:#

NameType
metaEntityMetadata<any>

Returns: void

Defined in: packages/knex/src/query/QueryBuilder.ts:699


wrapPaginateSubQuery#

PrivatewrapPaginateSubQuery(meta: EntityMetadata<any>): void

Parameters:#

NameType
metaEntityMetadata<any>

Returns: void

Defined in: packages/knex/src/query/QueryBuilder.ts:679

Last updated on by renovate[bot]