Skip to main content
Version: 5.9

SelectQueryBuilder <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

  • QueryBuilder<T>
    • SelectQueryBuilder

Index

Accessors

alias

  • get alias(): string
  • Returns string

helper

  • get helper(): QueryBuilderHelper
  • Returns QueryBuilderHelper

mainAlias

  • get mainAlias(): Alias<T>
  • Returns Alias<T>

Methods

[custom]

  • [custom](depth: number): string
  • Parameters

    • depth: number

    Returns string

addSelect

  • addSelect(fields: Field<T> | Field<T>[]): SelectQueryBuilder<T>
  • Parameters

    • fields: Field<T> | Field<T>[]

    Returns SelectQueryBuilder<T>

andWhere

  • andWhere(cond: QBFilterQuery<T>): SelectQueryBuilder<T>
  • andWhere(cond: string, params?: any[]): SelectQueryBuilder<T>
  • Parameters

    • cond: QBFilterQuery<T>

    Returns SelectQueryBuilder<T>

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]): SelectQueryBuilder<T>
  • Parameters

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

    Returns SelectQueryBuilder<T>

clone

  • clone(): QueryBuilder<T>
  • Returns QueryBuilder<T>

comment

  • comment(comment: string | string[]): SelectQueryBuilder<T>
  • Prepend comment to the sql query using the syntax /* ... *&#8205;/. Some characters are forbidden such as /*, *&#8205;/ and ?.


    Parameters

    • comment: string | string[]

    Returns SelectQueryBuilder<T>

count

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

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

    Returns CountQueryBuilder<T>

delete

  • delete(cond?: QBFilterQuery): DeleteQueryBuilder<T>
  • Parameters

    • optionalcond: QBFilterQuery

    Returns DeleteQueryBuilder<T>

distinct

  • distinct(): SelectQueryBuilder<T>
  • Returns SelectQueryBuilder<T>

distinctOn

  • distinctOn(fields: string | string[]): SelectQueryBuilder<T>
  • postgres only


    Parameters

    • fields: string | string[]

    Returns SelectQueryBuilder<T>

execute

  • execute<U>(method?: get | all | run, mapResults?: boolean): Promise<U>
  • execute<U>(method: all, mapResults?: boolean): Promise<U>
  • execute<U>(method: get, mapResults?: boolean): Promise<U>
  • execute<U>(method: 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 = T[]

    Parameters

    • optionalmethod: get | all | run
    • optionalmapResults: boolean

    Returns Promise<U>

from

  • from<T>(target: QueryBuilder<T>, aliasName?: string): QueryBuilder<T>
  • from<T>(target: EntityName<T>): QueryBuilder<T>
  • 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

    • target: QueryBuilder<T>
    • optionalaliasName: string

    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[]>

getResultAndCount

  • getResultAndCount(): Promise<[T[], number]>
  • Executes the query, returning both array of results and total count query (without offset and limit).


    Returns Promise<[T[], number]>

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)[]): SelectQueryBuilder<T>
  • Parameters

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

    Returns SelectQueryBuilder<T>

having

  • having(cond?: string | QBFilterQuery, params?: any[]): SelectQueryBuilder<T>
  • Parameters

    • cond: string | QBFilterQuery = {}
    • optionalparams: any[]

    Returns SelectQueryBuilder<T>

hintComment

  • hintComment(comment: string | string[]): SelectQueryBuilder<T>
  • Add hints to the query using comment-like syntax /*+ ... *&#8205;/. MySQL and Oracle use this syntax for optimizer hints. Also various DB proxies and routers use this syntax to pass hints to alter their behavior. In other dialects the hints are ignored as simple comments.


    Parameters

    • comment: string | string[]

    Returns SelectQueryBuilder<T>

ignore

  • ignore(): SelectQueryBuilder<T>
  • Returns SelectQueryBuilder<T>

indexHint

  • indexHint(sql: string): SelectQueryBuilder<T>
  • Adds index hint to the FROM clause.


    Parameters

    • sql: string

    Returns SelectQueryBuilder<T>

innerJoinAndSelect

  • innerJoinAndSelect(field: string, alias: string, cond?: QBFilterQuery, fields?: string[]): SelectQueryBuilder<T>
  • Parameters

    • field: string
    • alias: string
    • cond: QBFilterQuery = {}
    • optionalfields: string[]

    Returns SelectQueryBuilder<T>

insert

  • insert(data: RequiredEntityData<T> | RequiredEntityData<T>[]): InsertQueryBuilder<T>
  • Parameters

    • data: RequiredEntityData<T> | RequiredEntityData<T>[]

    Returns InsertQueryBuilder<T>

join

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

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

    Returns SelectQueryBuilder<T>

joinAndSelect

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

    • field: string
    • alias: string
    • cond: QBFilterQuery = {}
    • type: leftJoin | innerJoin | pivotJoin = 'innerJoin'
    • optionalpath: string
    • optionalfields: string[]

    Returns SelectQueryBuilder<T>

leftJoin

  • leftJoin(field: string, alias: string, cond?: QBFilterQuery): SelectQueryBuilder<T>
  • Parameters

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

    Returns SelectQueryBuilder<T>

leftJoinAndSelect

  • leftJoinAndSelect(field: string, alias: string, cond?: QBFilterQuery, fields?: string[]): SelectQueryBuilder<T>
  • Parameters

    • field: string
    • alias: string
    • cond: QBFilterQuery = {}
    • optionalfields: string[]

    Returns SelectQueryBuilder<T>

limit

  • limit(limit?: number, offset?: number): SelectQueryBuilder<T>
  • Parameters

    • optionallimit: number
    • offset: number = 0

    Returns SelectQueryBuilder<T>

merge

  • merge(data?: Field<T>[] | EntityData<T>): SelectQueryBuilder<T>
  • Parameters

    • optionaldata: Field<T>[] | EntityData<T>

    Returns SelectQueryBuilder<T>

offset

  • offset(offset?: number): SelectQueryBuilder<T>
  • Parameters

    • optionaloffset: number

    Returns SelectQueryBuilder<T>

onConflict

  • onConflict(fields?: Field<T> | Field<T>[]): SelectQueryBuilder<T>
  • Parameters

    • fields: Field<T> | Field<T>[] = []

    Returns SelectQueryBuilder<T>

orWhere

  • orWhere(cond: QBFilterQuery<T>): SelectQueryBuilder<T>
  • orWhere(cond: string, params?: any[]): SelectQueryBuilder<T>
  • Parameters

    • cond: QBFilterQuery<T>

    Returns SelectQueryBuilder<T>

orderBy

  • orderBy(orderBy: QBQueryOrderMap<T> | QBQueryOrderMap<T>[]): SelectQueryBuilder<T>
  • Parameters

    • orderBy: QBQueryOrderMap<T> | QBQueryOrderMap<T>[]

    Returns SelectQueryBuilder<T>

raw

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

    • R = Raw<any>

    Parameters

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

    Returns R

returning

  • returning(fields?: Field<T> | Field<T>[]): SelectQueryBuilder<T>
  • Parameters

    • optionalfields: Field<T> | Field<T>[]

    Returns SelectQueryBuilder<T>

select

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

    • fields: Field<T> | Field<T>[]
    • distinct: boolean = false

    Returns SelectQueryBuilder<T>

setFlag

  • setFlag(flag: QueryFlag): SelectQueryBuilder<T>
  • Parameters

    • flag: QueryFlag

    Returns SelectQueryBuilder<T>

setFlushMode

  • setFlushMode(flushMode?: FlushMode): SelectQueryBuilder<T>
  • Parameters

    • optionalflushMode: FlushMode

    Returns SelectQueryBuilder<T>

setLockMode

  • setLockMode(mode?: LockMode, tables?: string[]): SelectQueryBuilder<T>
  • Parameters

    • optionalmode: LockMode
    • optionaltables: string[]

    Returns SelectQueryBuilder<T>

then

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


    Type parameters

    • TResult1 = T[]
    • TResult2 = never

    Parameters

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

        Returns Promise<T[]>

      truncate

      • truncate(): TruncateQueryBuilder<T>
      • Returns TruncateQueryBuilder<T>

      unsetFlag

      • unsetFlag(flag: QueryFlag): SelectQueryBuilder<T>
      • Parameters

        • flag: QueryFlag

        Returns SelectQueryBuilder<T>

      update

      • update(data: EntityData<T>): UpdateQueryBuilder<T>
      • Parameters

        • data: EntityData<T>

        Returns UpdateQueryBuilder<T>

      where

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

        • cond: QBFilterQuery<T>
        • optionaloperator: $and | $or

        Returns SelectQueryBuilder<T>

      withSchema

      • withSchema(schema?: string): SelectQueryBuilder<T>
      • Parameters

        • optionalschema: string

        Returns SelectQueryBuilder<T>

      withSubQuery

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

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

        Returns SelectQueryBuilder<T>