Skip to main content
Version: Next

QueryBuilder <Entity, RootAlias, Hint, Context>

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

Methods

addSelect

andHaving

andWhere

  • andWhere(cond: QBFilterQuery<Entity>): this
  • andWhere(cond: string, params?: any[]): this

applyFilters

  • Apply filters to the QB where condition.


    Parameters

    Returns Promise<void>

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

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

    Returns this

clone

  • clone(reset?: boolean | string[]): QueryBuilder<Entity, never, never, never>
  • Parameters

    • optionalreset: boolean | string[]

    Returns QueryBuilder<Entity, never, never, never>

comment

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


    Parameters

    • comment: string | string[]

    Returns this

count

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

    • optionalfield: EntityKeyOrString<Entity> | EntityKeyOrString<Entity>[]
    • distinct: boolean = false

    Returns CountQueryBuilder<Entity>

delete

distinct

distinctOn

  • distinctOn(fields: EntityKeyOrString<Entity> | EntityKeyOrString<Entity>[]): SelectQueryBuilder<Entity, RootAlias, Hint, Context>
  • postgres only


    Parameters

    • fields: EntityKeyOrString<Entity> | EntityKeyOrString<Entity>[]

    Returns SelectQueryBuilder<Entity, RootAlias, Hint, Context>

execute

  • execute(method?: get | all | run, options?: boolean | ExecuteOptions): 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).


    Parameters

    • optionalmethod: get | all | run
    • optionaloptions: boolean | ExecuteOptions

    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.


    Parameters

    • target: QueryBuilder<Entity, never, never, never>
    • optionalaliasName: string

    Returns SelectQueryBuilder<Entity, RootAlias, Hint, Context>

getCount

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


    Parameters

    • optionalfield: EntityKeyOrString<Entity> | EntityKeyOrString<Entity>[]
    • optionaldistinct: boolean

    Returns Promise<number>

getFormattedQuery

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


    Returns string

getKnex

  • getKnex(processVirtualEntity?: boolean): QueryBuilder<any, any>
  • Parameters

    • processVirtualEntity: boolean = true

    Returns QueryBuilder<any, any>

getKnexQuery

  • getKnexQuery(processVirtualEntity?: boolean): QueryBuilder<any, any>
  • Parameters

    • processVirtualEntity: boolean = true

    Returns QueryBuilder<any, any>

getLoggerContext

  • getLoggerContext(): T
  • Gets logger context for this query builder.


    Returns T

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<Loaded<Entity, Hint>[]>
  • Alias for qb.getResultList()


    Returns Promise<Loaded<Entity, Hint>[]>

getResultAndCount

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


    Returns Promise<[Entity[], number]>

getResultList

  • getResultList(limit?: number): Promise<Loaded<Entity, Hint>[]>
  • Executes the query, returning array of results


    Parameters

    • optionallimit: number

    Returns Promise<Loaded<Entity, Hint>[]>

getSingleResult

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


    Returns Promise<null | Entity>

groupBy

  • groupBy(fields: EntityKeyOrString<Entity> | readonly EntityKeyOrString<Entity>[]): SelectQueryBuilder<Entity, RootAlias, Hint, Context>
  • Parameters

    • fields: EntityKeyOrString<Entity> | readonly EntityKeyOrString<Entity>[]

    Returns SelectQueryBuilder<Entity, RootAlias, Hint, Context>

hasFlag

having

hintComment

  • hintComment(comment: string | string[]): this
  • 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 this

ignore

  • ignore(): this
  • Returns this

indexHint

  • indexHint(sql: string): this
  • Adds index hint to the FROM clause.


    Parameters

    • sql: string

    Returns this

innerJoin

  • innerJoin(field: QueryBuilder<any, any> | QueryBuilder<any, never, never, never> | Field, alias: Alias, cond?: QBFilterQuery, schema?: string): SelectQueryBuilder<Entity, RootAlias, Hint, { [ K in string | number | symbol ]: IsNever<Context, true, false> extends true ? AddToContext<GetType<Entity, object, Field>, object, Field, Alias, false> : Context & AddToContext<GetType<Entity, Context, Field>, Context, Field, Alias, false>[K] }>
  • Parameters

    • field: QueryBuilder<any, any> | QueryBuilder<any, never, never, never> | Field
    • alias: Alias
    • cond: QBFilterQuery = {}
    • optionalschema: string

    Returns SelectQueryBuilder<Entity, RootAlias, Hint, { [ K in string | number | symbol ]: IsNever<Context, true, false> extends true ? AddToContext<GetType<Entity, object, Field>, object, Field, Alias, false> : Context & AddToContext<GetType<Entity, Context, Field>, Context, Field, Alias, false>[K] }>

innerJoinAndSelect

  • innerJoinAndSelect(field: Field | [field: Field, qb: QueryBuilder<any, any> | QueryBuilder<any, never, never, never>], alias: Alias, cond?: QBFilterQuery, fields?: string[], schema?: string): SelectQueryBuilder<Entity, RootAlias, ModifyHint<RootAlias, Context, Hint, Field, true> & {}, { [ K in string | number | symbol ]: IsNever<Context, true, false> extends true ? AddToContext<GetType<Entity, object, Field>, object, Field, Alias, true> : Context & AddToContext<GetType<Entity, Context, Field>, Context, Field, Alias, true>[K] }>
  • Parameters

    • field: Field | [field: Field, qb: QueryBuilder<any, any> | QueryBuilder<any, never, never, never>]
    • alias: Alias
    • cond: QBFilterQuery = {}
    • optionalfields: string[]
    • optionalschema: string

    Returns SelectQueryBuilder<Entity, RootAlias, ModifyHint<RootAlias, Context, Hint, Field, true> & {}, { [ K in string | number | symbol ]: IsNever<Context, true, false> extends true ? AddToContext<GetType<Entity, object, Field>, object, Field, Alias, true> : Context & AddToContext<GetType<Entity, Context, Field>, Context, Field, Alias, true>[K] }>

innerJoinLateral

  • innerJoinLateral(field: QueryBuilder<any, any> | QueryBuilder<any, never, never, never>, alias: string, cond?: QBFilterQuery, schema?: string): this
  • Parameters

    • field: QueryBuilder<any, any> | QueryBuilder<any, never, never, never>
    • alias: string
    • cond: QBFilterQuery = {}
    • optionalschema: string

    Returns this

innerJoinLateralAndSelect

  • innerJoinLateralAndSelect(field: [field: Field, qb: QueryBuilder<any, any> | QueryBuilder<any, never, never, never>], alias: Alias, cond?: QBFilterQuery, fields?: string[], schema?: string): SelectQueryBuilder<Entity, RootAlias, ModifyHint<RootAlias, Context, Hint, Field, true> & {}, { [ K in string | number | symbol ]: IsNever<Context, true, false> extends true ? AddToContext<GetType<Entity, object, Field>, object, Field, Alias, true> : Context & AddToContext<GetType<Entity, Context, Field>, Context, Field, Alias, true>[K] }>
  • Parameters

    • field: [field: Field, qb: QueryBuilder<any, any> | QueryBuilder<any, never, never, never>]
    • alias: Alias
    • cond: QBFilterQuery = {}
    • optionalfields: string[]
    • optionalschema: string

    Returns SelectQueryBuilder<Entity, RootAlias, ModifyHint<RootAlias, Context, Hint, Field, true> & {}, { [ K in string | number | symbol ]: IsNever<Context, true, false> extends true ? AddToContext<GetType<Entity, object, Field>, object, Field, Alias, true> : Context & AddToContext<GetType<Entity, Context, Field>, Context, Field, Alias, true>[K] }>

insert

join

  • join(field: QueryBuilder<any, any> | Field | QueryBuilder<any, never, never, never>, alias: Alias, cond?: QBFilterQuery, type?: JoinType, path?: string, schema?: string): SelectQueryBuilder<Entity, RootAlias, Hint, { [ K in string | number | symbol ]: IsNever<Context, true, false> extends true ? AddToContext<GetType<Entity, object, Field>, object, Field, Alias, false> : Context & AddToContext<GetType<Entity, Context, Field>, Context, Field, Alias, false>[K] }>
  • Parameters

    • field: QueryBuilder<any, any> | Field | QueryBuilder<any, never, never, never>
    • alias: Alias
    • cond: QBFilterQuery = {}
    • type: JoinType = JoinType.innerJoin
    • optionalpath: string
    • optionalschema: string

    Returns SelectQueryBuilder<Entity, RootAlias, Hint, { [ K in string | number | symbol ]: IsNever<Context, true, false> extends true ? AddToContext<GetType<Entity, object, Field>, object, Field, Alias, false> : Context & AddToContext<GetType<Entity, Context, Field>, Context, Field, Alias, false>[K] }>

joinAndSelect

  • joinAndSelect(field: Field | [field: Field, qb: QueryBuilder<any, any> | QueryBuilder<any, never, never, never>], alias: Alias, cond?: QBFilterQuery, type?: JoinType, path?: string, fields?: string[], schema?: string): SelectQueryBuilder<Entity, RootAlias, ModifyHint<RootAlias, Context, Hint, Field, true> & {}, { [ K in string | number | symbol ]: IsNever<Context, true, false> extends true ? AddToContext<GetType<Entity, object, Field>, object, Field, Alias, true> : Context & AddToContext<GetType<Entity, Context, Field>, Context, Field, Alias, true>[K] }>
  • Parameters

    • field: Field | [field: Field, qb: QueryBuilder<any, any> | QueryBuilder<any, never, never, never>]
    • alias: Alias
    • cond: QBFilterQuery = {}
    • type: JoinType = JoinType.innerJoin
    • optionalpath: string
    • optionalfields: string[]
    • optionalschema: string

    Returns SelectQueryBuilder<Entity, RootAlias, ModifyHint<RootAlias, Context, Hint, Field, true> & {}, { [ K in string | number | symbol ]: IsNever<Context, true, false> extends true ? AddToContext<GetType<Entity, object, Field>, object, Field, Alias, true> : Context & AddToContext<GetType<Entity, Context, Field>, Context, Field, Alias, true>[K] }>

leftJoin

  • leftJoin(field: QueryBuilder<any, any> | QueryBuilder<any, never, never, never> | Field, alias: Alias, cond?: QBFilterQuery, schema?: string): SelectQueryBuilder<Entity, RootAlias, Hint, { [ K in string | number | symbol ]: IsNever<Context, true, false> extends true ? AddToContext<GetType<Entity, object, Field>, object, Field, Alias, false> : Context & AddToContext<GetType<Entity, Context, Field>, Context, Field, Alias, false>[K] }>
  • Parameters

    • field: QueryBuilder<any, any> | QueryBuilder<any, never, never, never> | Field
    • alias: Alias
    • cond: QBFilterQuery = {}
    • optionalschema: string

    Returns SelectQueryBuilder<Entity, RootAlias, Hint, { [ K in string | number | symbol ]: IsNever<Context, true, false> extends true ? AddToContext<GetType<Entity, object, Field>, object, Field, Alias, false> : Context & AddToContext<GetType<Entity, Context, Field>, Context, Field, Alias, false>[K] }>

leftJoinAndSelect

  • leftJoinAndSelect(field: Field | [field: Field, qb: QueryBuilder<any, any> | QueryBuilder<any, never, never, never>], alias: Alias, cond?: QBFilterQuery, fields?: string[], schema?: string): SelectQueryBuilder<Entity, RootAlias, ModifyHint<RootAlias, Context, Hint, Field, true> & {}, { [ K in string | number | symbol ]: IsNever<Context, true, false> extends true ? AddToContext<GetType<Entity, object, Field>, object, Field, Alias, true> : Context & AddToContext<GetType<Entity, Context, Field>, Context, Field, Alias, true>[K] }>
  • Parameters

    • field: Field | [field: Field, qb: QueryBuilder<any, any> | QueryBuilder<any, never, never, never>]
    • alias: Alias
    • cond: QBFilterQuery = {}
    • optionalfields: string[]
    • optionalschema: string

    Returns SelectQueryBuilder<Entity, RootAlias, ModifyHint<RootAlias, Context, Hint, Field, true> & {}, { [ K in string | number | symbol ]: IsNever<Context, true, false> extends true ? AddToContext<GetType<Entity, object, Field>, object, Field, Alias, true> : Context & AddToContext<GetType<Entity, Context, Field>, Context, Field, Alias, true>[K] }>

leftJoinLateral

  • leftJoinLateral(field: QueryBuilder<any, any> | QueryBuilder<any, never, never, never>, alias: string, cond?: QBFilterQuery, schema?: string): this
  • Parameters

    • field: QueryBuilder<any, any> | QueryBuilder<any, never, never, never>
    • alias: string
    • cond: QBFilterQuery = {}
    • optionalschema: string

    Returns this

leftJoinLateralAndSelect

  • leftJoinLateralAndSelect(field: [field: Field, qb: QueryBuilder<any, any> | QueryBuilder<any, never, never, never>], alias: Alias, cond?: QBFilterQuery, fields?: string[], schema?: string): SelectQueryBuilder<Entity, RootAlias, ModifyHint<RootAlias, Context, Hint, Field, true> & {}, { [ K in string | number | symbol ]: IsNever<Context, true, false> extends true ? AddToContext<GetType<Entity, object, Field>, object, Field, Alias, true> : Context & AddToContext<GetType<Entity, Context, Field>, Context, Field, Alias, true>[K] }>
  • Parameters

    • field: [field: Field, qb: QueryBuilder<any, any> | QueryBuilder<any, never, never, never>]
    • alias: Alias
    • cond: QBFilterQuery = {}
    • optionalfields: string[]
    • optionalschema: string

    Returns SelectQueryBuilder<Entity, RootAlias, ModifyHint<RootAlias, Context, Hint, Field, true> & {}, { [ K in string | number | symbol ]: IsNever<Context, true, false> extends true ? AddToContext<GetType<Entity, object, Field>, object, Field, Alias, true> : Context & AddToContext<GetType<Entity, Context, Field>, Context, Field, Alias, true>[K] }>

limit

  • limit(limit?: number, offset?: number): SelectQueryBuilder<Entity, RootAlias, Hint, Context>
  • Parameters

    • optionallimit: number
    • offset: number = 0

    Returns SelectQueryBuilder<Entity, RootAlias, Hint, Context>

merge

offset

  • Parameters

    • optionaloffset: number

    Returns SelectQueryBuilder<Entity, RootAlias, Hint, Context>

onConflict

orderBy

orHaving

orWhere

  • orWhere(cond: QBFilterQuery<Entity>): this
  • orWhere(cond: string, params?: any[]): this

returning

  • returning(fields?: Field<Entity> | Field<Entity>[]): this
  • Parameters

    Returns this

select

setFlag

setFlushMode

  • Parameters

    Returns this

setLockMode

  • setLockMode(mode?: LockMode, tables?: string[]): this
  • Parameters

    • optionalmode: LockMode
    • optionaltables: string[]

    Returns this

setLoggerContext

then

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


    Parameters

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

        Returns Promise<number | Loaded<Entity, Hint>[] | QueryResult<Entity>>

      toQuery

      • toQuery(): { _sql: Sql; params: readonly unknown[]; sql: string }
      • Returns { _sql: Sql; params: readonly unknown[]; sql: string }

        • _sql: Sql
        • params: readonly unknown[]
        • sql: string

      truncate

      unsetFlag

      update

      where

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

        Returns this

      withSchema

      • withSchema(schema?: string): this
      • Parameters

        • optionalschema: string

        Returns this

      withSubQuery

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

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

        Returns this