Skip to main content
Version: 6.3

CountQueryBuilder <Entity>

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>
  • Returns Alias<Entity>

Methods

addSelect

andWhere

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

    Returns this

applyFilters

  • Apply filters to the QB where condition.


    Parameters

    Returns Promise<CountQueryBuilder<Entity>>

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

  • Parameters

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

    Returns CountQueryBuilder<Entity>

delete

distinct

distinctOn

  • postgres only


    Parameters

    • fields: string | string[]

    Returns SelectQueryBuilder<Entity, any, any, never>

execute

  • execute<Result>(method?: get | all | run, mapResults?: boolean): Promise<Result>
  • execute<Result>(method: all, mapResults?: boolean): Promise<Result>
  • execute<Result>(method: get, mapResults?: boolean): Promise<Result>
  • execute<Result>(method: run, mapResults?: boolean): Promise<Result>
  • 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

    • Result = { count: number }[]

    Parameters

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

    Returns Promise<Result>

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

    • Entity: Partial<Entity> = Partial<any>

    Parameters

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

    Returns SelectQueryBuilder<Entity, any, any, never>

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(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>(): 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, any>[]>
  • Alias for qb.getResultList()


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

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, any>[]>
  • Executes the query, returning array of results


    Parameters

    • optionallimit: number

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

getSingleResult

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


    Returns Promise<null | Entity>

groupBy

  • groupBy(fields: string | keyof Entity | readonly (string | keyof Entity)[]): SelectQueryBuilder<Entity, any, any, never>
  • Parameters

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

    Returns SelectQueryBuilder<Entity, any, any, never>

hasFlag

  • Parameters

    Returns boolean

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, Alias>(field: QueryBuilder<any, any> | QueryBuilder<any, never, never, never> | Field, alias: Alias, cond?: QBFilterQuery, schema?: string): SelectQueryBuilder<Entity, any, any, { [ K in string ]: AddToContext<GetType<Entity, object, Field>, object, Field, Alias, false>[K] }>
  • Type parameters

    • Field: string | AnyString
    • Alias: string

    Parameters

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

    Returns SelectQueryBuilder<Entity, any, any, { [ K in string ]: AddToContext<GetType<Entity, object, Field>, object, Field, Alias, false>[K] }>

innerJoinAndSelect

  • innerJoinAndSelect<Field, Alias>(field: Field | [field: Field, qb: QueryBuilder<any, any> | QueryBuilder<any, never, never, never>], alias: Alias, cond?: QBFilterQuery, fields?: string[], schema?: string): SelectQueryBuilder<Entity, any, any, { [ K in string ]: AddToContext<GetType<Entity, object, Field>, object, Field, Alias, true>[K] }>
  • Type parameters

    • Field: string | AnyString
    • Alias: string

    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, any, any, { [ K in string ]: AddToContext<GetType<Entity, object, Field>, object, 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, Alias>(field: [field: Field, qb: QueryBuilder<any, any> | QueryBuilder<any, never, never, never>], alias: Alias, cond?: QBFilterQuery, fields?: string[], schema?: string): SelectQueryBuilder<Entity, any, any, { [ K in string ]: AddToContext<GetType<Entity, object, Field>, object, Field, Alias, true>[K] }>
  • Type parameters

    • Field: string | AnyString
    • Alias: string

    Parameters

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

    Returns SelectQueryBuilder<Entity, any, any, { [ K in string ]: AddToContext<GetType<Entity, object, Field>, object, Field, Alias, true>[K] }>

insert

join

  • join<Field, Alias>(field: QueryBuilder<any, any> | QueryBuilder<any, never, never, never> | Field, alias: Alias, cond?: QBFilterQuery, type?: JoinType, path?: string, schema?: string): SelectQueryBuilder<Entity, any, any, { [ K in string ]: AddToContext<GetType<Entity, object, Field>, object, Field, Alias, false>[K] }>
  • Type parameters

    • Field: string | AnyString
    • Alias: string

    Parameters

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

    Returns SelectQueryBuilder<Entity, any, any, { [ K in string ]: AddToContext<GetType<Entity, object, Field>, object, Field, Alias, false>[K] }>

joinAndSelect

  • joinAndSelect<Field, Alias>(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, any, any, { [ K in string ]: AddToContext<GetType<Entity, object, Field>, object, Field, Alias, true>[K] }>
  • Type parameters

    • Field: string | AnyString
    • Alias: string

    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, any, any, { [ K in string ]: AddToContext<GetType<Entity, object, Field>, object, Field, Alias, true>[K] }>

leftJoin

  • leftJoin<Field, Alias>(field: QueryBuilder<any, any> | QueryBuilder<any, never, never, never> | Field, alias: Alias, cond?: QBFilterQuery, schema?: string): SelectQueryBuilder<Entity, any, any, { [ K in string ]: AddToContext<GetType<Entity, object, Field>, object, Field, Alias, false>[K] }>
  • Type parameters

    • Field: string | AnyString
    • Alias: string

    Parameters

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

    Returns SelectQueryBuilder<Entity, any, any, { [ K in string ]: AddToContext<GetType<Entity, object, Field>, object, Field, Alias, false>[K] }>

leftJoinAndSelect

  • leftJoinAndSelect<Field, Alias>(field: Field | [field: Field, qb: QueryBuilder<any, any> | QueryBuilder<any, never, never, never>], alias: Alias, cond?: QBFilterQuery, fields?: string[], schema?: string): SelectQueryBuilder<Entity, any, any, { [ K in string ]: AddToContext<GetType<Entity, object, Field>, object, Field, Alias, true>[K] }>
  • Type parameters

    • Field: string | AnyString
    • Alias: string

    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, any, any, { [ K in string ]: AddToContext<GetType<Entity, object, Field>, object, 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, Alias>(field: [field: Field, qb: QueryBuilder<any, any> | QueryBuilder<any, never, never, never>], alias: Alias, cond?: QBFilterQuery, fields?: string[], schema?: string): SelectQueryBuilder<Entity, any, any, { [ K in string ]: AddToContext<GetType<Entity, object, Field>, object, Field, Alias, true>[K] }>
  • Type parameters

    • Field: string | AnyString
    • Alias: string

    Parameters

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

    Returns SelectQueryBuilder<Entity, any, any, { [ K in string ]: AddToContext<GetType<Entity, object, Field>, object, Field, Alias, true>[K] }>

limit

  • Parameters

    • optionallimit: number
    • offset: number = 0

    Returns SelectQueryBuilder<Entity, any, any, never>

merge

  • Parameters

    Returns this

offset

  • Parameters

    • optionaloffset: number

    Returns SelectQueryBuilder<Entity, any, any, never>

onConflict

orWhere

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

    Returns this

orderBy

returning

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

    Returns this

select

  • Parameters

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

    Returns SelectQueryBuilder<Entity, any, any, never>

setFlag

  • Parameters

    Returns this

setFlushMode

  • Parameters

    Returns this

setLockMode

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

    • optionalmode: LockMode
    • optionaltables: string[]

    Returns this

setLoggerContext

  • Sets logger context for this query builder.


    Parameters

    Returns void

then

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


    Type parameters

    • TResult1 = number
    • TResult2 = never

    Parameters

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

        Returns Promise<number>

      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

      • Parameters

        Returns this

      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