Skip to main content
Version: Next

QueryBuilder <T>

SQL query builder with fluent interface.

const qb = orm.em.createQueryBuilder(Publisher);
qb.select('*')
.where({
name: 'test 123',
type: PublisherType.GLOBAL,
})
.orderBy({
name: QueryOrder.DESC,
type: QueryOrder.ASC,
})
.limit(2, 1);

const publisher = await qb.getSingleResult();

Hierarchy

Index

Accessors

alias

  • get alias(): string
  • Returns string

helper

  • get helper(): QueryBuilderHelper
  • Returns QueryBuilderHelper

mainAlias

  • get mainAlias(): Alias
  • Returns Alias

Methods

addSelect

andWhere

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

    • alias: string

    Returns QueryBuilder<any, any>

cache

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

    • config: number | boolean | [string, number] = true

    Returns QueryBuilder<T>

clone

count

  • Parameters

    • optionalfield: string | string[]
    • distinct: boolean = false

    Returns CountQueryBuilder<T>

delete

execute

  • execute<U>(method?: get | all | 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

    • U = any

    Parameters

    • method: get | all | run = 'all'
    • mapResults: boolean = true

    Returns Promise<U>

from

  • Specifies FROM which entity’s table select/update/delete will be executed, removing all previously set FROM-s. Allows setting a main string alias of the selection data.


    Type parameters

    • T: Partial<T> = Partial<any>

    Parameters

    Returns QueryBuilder<T>

getCount

  • getCount(field?: string | string[], distinct?: boolean): Promise<number>
  • Executes count query (without offset and limit), returning total count of results


    Parameters

    • optionalfield: string | string[]
    • optionaldistinct: boolean

    Returns Promise<number>

getFormattedQuery

  • getFormattedQuery(): string
  • Returns raw interpolated query string with all the parameters inlined.


    Returns string

getKnex

  • getKnex(): QueryBuilder<any, any>
  • Returns QueryBuilder<any, any>

getKnexQuery

  • getKnexQuery(): QueryBuilder<any, any>
  • Returns QueryBuilder<any, any>

getParams

  • getParams(): readonly Value[]
  • Returns the list of all parameters for this query.


    Returns readonly Value[]

getQuery

  • getQuery(): string
  • Returns the query with parameters as wildcards.


    Returns string

getResult

  • getResult(): Promise<T[]>
  • Alias for qb.getResultList()


    Returns Promise<T[]>

getResultList

  • getResultList(): Promise<T[]>
  • Executes the query, returning array of results


    Returns Promise<T[]>

getSingleResult

  • getSingleResult(): Promise<null | T>
  • Executes the query, returning the first result or null


    Returns Promise<null | T>

groupBy

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

    • fields: string | keyof T | readonly (string | keyof T)[]

    Returns QueryBuilder<T>

having

  • Parameters

    • cond: any = {}
    • optionalparams: any[]

    Returns QueryBuilder<T>

ignore

indexHint

  • Adds index hint to the FROM clause.


    Parameters

    • sql: string

    Returns QueryBuilder<T>

insert

join

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

    • field: string
    • alias: string
    • cond: any = {}
    • type: innerJoin | leftJoin | pivotJoin = 'innerJoin'
    • optionalpath: string

    Returns QueryBuilder<T>

joinAndSelect

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

    • field: string
    • alias: string
    • cond: any = {}
    • type: innerJoin | leftJoin | pivotJoin = 'innerJoin'
    • optionalpath: string

    Returns SelectQueryBuilder<T>

leftJoin

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

    • field: string
    • alias: string
    • cond: any = {}

    Returns QueryBuilder<T>

leftJoinAndSelect

limit

  • Parameters

    • optionallimit: number
    • offset: number = 0

    Returns QueryBuilder<T>

merge

offset

onConflict

orWhere

orderBy

raw

  • raw<R>(sql: string, bindings?: ValueDict | RawBinding[]): R
  • Type parameters

    • R = Raw<any>

    Parameters

    • sql: string
    • bindings: ValueDict | RawBinding[] = []

    Returns R

select

setFlag

setFlushMode

setLockMode

then

  • then<TResult1, TResult2>(onfulfilled?: null | (value: any) => TResult1 | PromiseLike<TResult1>, onrejected?: null | (reason: any) => TResult2 | PromiseLike<TResult2>): Promise<number | T[] | QueryResult<T>>
  • Provides promise-like interface so we can await the QB instance.


    Type parameters

    • TResult1 = any
    • TResult2 = never

    Parameters

    • optionalonfulfilled: null | (value: any) => TResult1 | PromiseLike<TResult1>
    • optionalonrejected: null | (reason: any) => TResult2 | PromiseLike<TResult2>

    Returns Promise<number | T[] | QueryResult<T>>

truncate

unsetFlag

update

where

withSchema

withSubQuery

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

    • subQuery: QueryBuilder<any, any>
    • alias: string

    Returns QueryBuilder<T>