Version: 4.3

Class: QueryBuilder<T>

SQL query builder

Type parameters

NameTypeDefault
TAnyEntity<T>AnyEntity

Hierarchy

  • QueryBuilder

Constructors

constructor

+ new QueryBuilder(entityName: string, metadata: MetadataStorage, driver: AbstractSqlDriver, context?: Transaction, alias?: string, connectionType?: "read" | "write", em?: SqlEntityManager): QueryBuilder

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

Parameters:

NameTypeDefault value
entityNamestring-
metadataMetadataStorage-
driverAbstractSqlDriver-
context?Transaction-
aliasstring`e0`
connectionType?"read" | "write"-
em?SqlEntityManager-

Returns: QueryBuilder

Properties

_aliasMap

Private _aliasMap: Dictionary<string>

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


_cache

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

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


_cond

Private _cond: Dictionary

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


_data

Private _data: Dictionary

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


_fields

Optional _fields: 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

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


_joinedProps

Private _joinedProps: Map<string, PopulateOptions<any>> = new Map<string, PopulateOptions<any>>()

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


_joins

Private _joins: Dictionary<JoinOptions>

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


_limit

Private Optional _limit: number

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


_offset

Private Optional _offset: number

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


_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: string

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


alias

Readonly alias: string

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


aliasCounter

Private aliasCounter: number = 1

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


connectionType

Private Optional connectionType: "read" | "write"

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


context

Private Optional Readonly context: Transaction

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


driver

Private Readonly driver: AbstractSqlDriver

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


em

Private Optional Readonly em: SqlEntityManager

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


entityName

Private Readonly entityName: string

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


finalized

Private finalized: boolean = false

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


flags

Private flags: Set<QueryFlag> = new Set([QueryFlag.CONVERT_CUSTOM_TYPES])

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


helper

Private Readonly helper: QueryBuilderHelper = new QueryBuilderHelper(this.entityName, this.alias, this._aliasMap, this.subQueries, this.metadata, this.knex, this.platform)

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


knex

Private Readonly knex: any = this.driver.getConnection(this.connectionType).getKnex()

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


lockMode

Private Optional lockMode: LockMode

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


metadata

Private Readonly metadata: MetadataStorage

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


platform

Private Readonly platform: AbstractSqlPlatform = this.driver.getPlatform()

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


subQueries

Private subQueries: Dictionary<string>

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


type

type: QueryType

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

Methods

addSelect

addSelect(fields: Field<T> | Field<T>[]): this

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

Parameters:

NameType
fieldsField<T> | Field<T>[]

Returns: this


andWhere

andWhere(cond: QBFilterQuery<T>): this

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

Parameters:

NameType
condQBFilterQuery<T>

Returns: this

andWhere(cond: string, params?: any[]): this

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

Parameters:

NameType
condstring
params?any[]

Returns: this


as

as(alias: string): KnexQueryBuilder

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

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: KnexQueryBuilder


autoJoinPivotTable

PrivateautoJoinPivotTable(field: string): void

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

Parameters:

NameType
fieldstring

Returns: void


cache

cache(config?: boolean | number | [string, number]): this

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

Parameters:

NameTypeDefault value
configboolean | number | [string, number]true

Returns: this


clone

clone(): QueryBuilder<T>

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

Returns: QueryBuilder<T>


count

count(field?: string | string[], distinct?: boolean): this

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

Parameters:

NameTypeDefault value
field?string | string[]-
distinctbooleanfalse

Returns: this


delete

delete(cond?: QBFilterQuery): this

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

Parameters:

NameTypeDefault value
condQBFilterQuery{}

Returns: this


execute

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

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

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
method"all" | "get" | "run""all"
mapResultsbooleantrue

Returns: Promise<U>


finalize

Privatefinalize(): void

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

Returns: void


getAliasForJoinPath

getAliasForJoinPath(path: string): string | undefined

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

Parameters:

NameType
pathstring

Returns: string | undefined


getFieldsForJoinedLoad

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

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

Type parameters:

NameType
UAnyEntity<U>

Parameters:

NameType
propEntityProperty<U>
aliasstring

Returns: Field<U>[]


getFormattedQuery

getFormattedQuery(): string

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

Returns raw interpolated query string with all the parameters inlined.

Returns: string


getKnex

getKnex(): KnexQueryBuilder

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

Returns: KnexQueryBuilder


getKnexQuery

getKnexQuery(): KnexQueryBuilder

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

Returns: KnexQueryBuilder


getNextAlias

getNextAlias(prefix?: string): string

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

Parameters:

NameTypeDefault value
prefixstring"e"

Returns: string


getParams

getParams(): readonly Value[]

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

Returns the list of all parameters for this query.

Returns: readonly Value[]


getQuery

getQuery(): string

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

Returns the query with parameters as wildcards.

Returns: string


getQueryBase

PrivategetQueryBase(): KnexQueryBuilder

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

Returns: KnexQueryBuilder


getResult

getResult(): Promise<T[]>

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

Alias for qb.getResultList()

Returns: Promise<T[]>


getResultList

getResultList(): Promise<T[]>

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

Executes the query, returning array of results

Returns: Promise<T[]>


getSingleResult

getSingleResult(): Promise<T | null>

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

Executes the query, returning the first result or null

Returns: Promise<T | null>


groupBy

groupBy(fields: string | keyof T | (string | keyof T)[]): this

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

Parameters:

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

Returns: this


having

having(cond?: QBFilterQuery | string, params?: any[]): this

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

Parameters:

NameTypeDefault value
condQBFilterQuery | string{}
params?any[]-

Returns: this


init

Privateinit(type: QueryType, data?: any, cond?: any): this

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

Parameters:

NameType
typeQueryType
data?any
cond?any

Returns: this


insert

insert(data: any): this

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

Parameters:

NameType
dataany

Returns: this


join

join(field: string, alias: string, cond?: QBFilterQuery, type?: "leftJoin" | "innerJoin" | "pivotJoin", path?: string): this

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

Parameters:

NameTypeDefault value
fieldstring-
aliasstring-
condQBFilterQuery{}
type"leftJoin" | "innerJoin" | "pivotJoin""innerJoin"
path?string-

Returns: this


joinAndSelect

joinAndSelect(field: string, alias: string, cond?: QBFilterQuery, type?: "leftJoin" | "innerJoin" | "pivotJoin", path?: string): this

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

Parameters:

NameTypeDefault value
fieldstring-
aliasstring-
condQBFilterQuery{}
type"leftJoin" | "innerJoin" | "pivotJoin""innerJoin"
path?string-

Returns: this


joinReference

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

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

Parameters:

NameType
fieldstring
aliasstring
condDictionary
type"leftJoin" | "innerJoin" | "pivotJoin"
path?string

Returns: EntityProperty


leftJoin

leftJoin(field: string, alias: string, cond?: QBFilterQuery): this

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

Parameters:

NameTypeDefault value
fieldstring-
aliasstring-
condQBFilterQuery{}

Returns: this


leftJoinAndSelect

leftJoinAndSelect(field: string, alias: string, cond?: QBFilterQuery): this

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

Parameters:

NameTypeDefault value
fieldstring-
aliasstring-
condQBFilterQuery{}

Returns: this


limit

limit(limit?: number, offset?: number): this

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

Parameters:

NameTypeDefault value
limit?number-
offsetnumber0

Returns: this


offset

offset(offset?: number): this

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

Parameters:

NameType
offset?number

Returns: this


orWhere

orWhere(cond: QBFilterQuery<T>): this

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

Parameters:

NameType
condQBFilterQuery<T>

Returns: this

orWhere(cond: string, params?: any[]): this

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

Parameters:

NameType
condstring
params?any[]

Returns: this


orderBy

orderBy(orderBy: QueryOrderMap): this

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

Parameters:

NameType
orderByQueryOrderMap

Returns: this


populate

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

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

internal

Parameters:

NameType
populatePopulateOptions<T>[]

Returns: this


prepareFields

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

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

Type parameters:

NameTypeDefault
TAnyEntity<T>-
Ustring | Rawstring \

Parameters:

NameTypeDefault value
fieldsField<T>[]-
type"where" | "groupBy" | "sub-query""where"

Returns: U[]


raw

raw(sql: string): Raw

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

Parameters:

NameType
sqlstring

Returns: Raw


ref

ref(field: string): any

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

internal

Parameters:

NameType
fieldstring

Returns: any


select

select(fields: Field<T> | Field<T>[], distinct?: boolean): this

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

Parameters:

NameTypeDefault value
fieldsField<T> | Field<T>[]-
distinctbooleanfalse

Returns: this


setFlag

setFlag(flag: QueryFlag): this

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

Parameters:

NameType
flagQueryFlag

Returns: this


setLockMode

setLockMode(mode?: LockMode): this

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

Parameters:

NameType
mode?LockMode

Returns: this


truncate

truncate(): this

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

Returns: this


unsetFlag

unsetFlag(flag: QueryFlag): this

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

Parameters:

NameType
flagQueryFlag

Returns: this


update

update(data: any): this

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

Parameters:

NameType
dataany

Returns: this


where

where(cond: QBFilterQuery<T>, operator?: keyof typeof GroupOperator): this

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

Parameters:

NameType
condQBFilterQuery<T>
operator?keyof typeof GroupOperator

Returns: this

where(cond: string, params?: any[], operator?: keyof typeof GroupOperator): this

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

Parameters:

NameType
condstring
params?any[]
operator?keyof typeof GroupOperator

Returns: this


withSchema

withSchema(schema?: string): this

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

Parameters:

NameType
schema?string

Returns: this


withSubQuery

withSubQuery(subQuery: KnexQueryBuilder, alias: string): this

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

Parameters:

NameType
subQueryKnexQueryBuilder
aliasstring

Returns: this


wrapModifySubQuery

PrivatewrapModifySubQuery(meta: EntityMetadata): void

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

Parameters:

NameType
metaEntityMetadata

Returns: void


wrapPaginateSubQuery

PrivatewrapPaginateSubQuery(meta: EntityMetadata): void

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

Parameters:

NameType
metaEntityMetadata

Returns: void

Last updated on by Martin Adámek