Skip to main content
Version: Next

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

inheritedalias

  • get alias(): string
  • Returns string

inheritedhelper

  • get helper(): QueryBuilderHelper
  • Returns QueryBuilderHelper

inheritedmainAlias

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

inheritedtype

  • Returns QueryType

Methods

inheritedaddSelect

inheritedandHaving

  • Parameters

    Returns SelectQueryBuilder<Entity, any, any, never>

inheritedandOrderBy

inheritedandWhere

  • andWhere(cond): this
  • andWhere(cond, params): this
  • Parameters

    Returns this

inheritedapplyFilters

  • applyFilters(filterOptions): Promise<void>
  • Apply filters to the QB where condition.


    Parameters

    Returns Promise<void>

inheritedas

  • as(alias): NativeQueryBuilder
  • as<T>(targetEntity, alias): NativeQueryBuilder
  • Returns native query builder instance with sub-query aliased with given alias.


    Parameters

    • alias: string

    Returns NativeQueryBuilder

inheritedcache

  • cache(config): this
  • Parameters

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

    Returns this

inheritedclone

  • clone(reset, preserve): QueryBuilder<Entity, never, never, never>
  • Parameters

    • optionalreset: boolean | string[]
    • optionalpreserve: string[]

    Returns QueryBuilder<Entity, never, never, never>

inheritedcomment

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


    Parameters

    • comment: undefined | string | string[]

    Returns this

inheritedcount

  • Parameters

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

    Returns CountQueryBuilder<Entity>

inheriteddelete

inheriteddistinct

inheriteddistinctOn

  • postgres only


    Parameters

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

    Returns SelectQueryBuilder<Entity, any, any, never>

execute

  • execute<Result>(method, mapResults): Promise<Result>
  • execute<Result>(method, mapResults): Promise<Result>
  • execute<Result>(method, mapResults): Promise<Result>
  • execute<Result>(method, mapResults): 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).


    Parameters

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

    Returns Promise<Result>

inheritedfrom

  • 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, any, any, never>

inheritedgetCount

  • getCount(field, distinct): 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>

inheritedgetFormattedQuery

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


    Returns string

inheritedgetLoggerContext

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


    Returns T

inheritedgetNativeQuery

  • getNativeQuery(processVirtualEntity): NativeQueryBuilder
  • Parameters

    • processVirtualEntity: boolean = true

    Returns NativeQueryBuilder

inheritedgetParams

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


    Returns readonly unknown[]

inheritedgetQuery

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


    Returns string

inheritedgetResult

  • getResult(): Promise<Loaded<Entity, any>[]>
  • Alias for qb.getResultList()


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

inheritedgetResultAndCount

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


    Returns Promise<[Entity[], number]>

inheritedgetResultList

  • getResultList(limit): Promise<Loaded<Entity, any>[]>
  • Executes the query, returning array of results mapped to entity instances.


    Parameters

    • optionallimit: number

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

inheritedgetSingleResult

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


    Returns Promise<null | Entity>

inheritedgroupBy

  • Parameters

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

    Returns SelectQueryBuilder<Entity, any, any, never>

inheritedhasFlag

  • hasFlag(flag): boolean
  • Parameters

    Returns boolean

inheritedhaving

  • Parameters

    • cond: string | QBFilterQuery = {}
    • optionalparams: any[]
    • optionaloperator: $and | $or

    Returns SelectQueryBuilder<Entity, any, any, never>

inheritedhintComment

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

    Returns this

inheritedignore

  • ignore(): this
  • Returns this

inheritedindexHint

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


    Parameters

    • sql: undefined | string

    Returns this

inheritedinnerJoin

  • innerJoin<Field, Alias>(field, alias, cond, schema): SelectQueryBuilder<Entity, any, any, { [ K in string ]: AddToContext<GetType<Entity, object, Field>, object, Field, Alias, false>[K] }>
  • Parameters

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

inheritedinnerJoinAndSelect

  • innerJoinAndSelect<Field, Alias>(field, alias, cond, fields, schema): SelectQueryBuilder<Entity, any, any, { [ K in string ]: AddToContext<GetType<Entity, object, Field>, object, Field, Alias, true>[K] }>
  • Parameters

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

inheritedinnerJoinLateral

  • innerJoinLateral(field, alias, cond, schema): this

inheritedinnerJoinLateralAndSelect

  • innerJoinLateralAndSelect<Field, Alias>(field, alias, cond, fields, schema): SelectQueryBuilder<Entity, any, any, { [ K in string ]: AddToContext<GetType<Entity, object, Field>, object, Field, Alias, true>[K] }>
  • Parameters

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

inheritedinsert

inheritedjoin

  • join<Field, Alias>(field, alias, cond, type, path, schema): SelectQueryBuilder<Entity, any, any, { [ K in string ]: AddToContext<GetType<Entity, object, Field>, object, Field, Alias, false>[K] }>
  • Parameters

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

inheritedjoinAndSelect

  • joinAndSelect<Field, Alias>(field, alias, cond, type, path, fields, schema): SelectQueryBuilder<Entity, any, any, { [ K in string ]: AddToContext<GetType<Entity, object, Field>, object, Field, Alias, true>[K] }>
  • Parameters

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

inheritedleftJoin

  • leftJoin<Field, Alias>(field, alias, cond, schema): SelectQueryBuilder<Entity, any, any, { [ K in string ]: AddToContext<GetType<Entity, object, Field>, object, Field, Alias, false>[K] }>
  • Parameters

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

inheritedleftJoinAndSelect

  • leftJoinAndSelect<Field, Alias>(field, alias, cond, fields, schema): SelectQueryBuilder<Entity, any, any, { [ K in string ]: AddToContext<GetType<Entity, object, Field>, object, Field, Alias, true>[K] }>
  • Parameters

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

inheritedleftJoinLateral

  • leftJoinLateral(field, alias, cond, schema): this

inheritedleftJoinLateralAndSelect

  • leftJoinLateralAndSelect<Field, Alias>(field, alias, cond, fields, schema): SelectQueryBuilder<Entity, any, any, { [ K in string ]: AddToContext<GetType<Entity, object, Field>, object, Field, Alias, true>[K] }>
  • Parameters

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

inheritedlimit

  • Parameters

    • optionallimit: number
    • offset: number = 0

    Returns SelectQueryBuilder<Entity, any, any, never>

inheritedmerge

  • merge(data): this
  • Parameters

    Returns this

inheritedoffset

  • Parameters

    • optionaloffset: number

    Returns SelectQueryBuilder<Entity, any, any, never>

inheritedonConflict

inheritedorderBy

inheritedorHaving

inheritedorWhere

  • orWhere(cond): this
  • orWhere(cond, params): this
  • Parameters

    Returns this

inheritedreturning

  • returning(fields): this
  • Parameters

    Returns this

inheritedselect

  • Parameters

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

    Returns SelectQueryBuilder<Entity, any, any, never>

inheritedsetFlag

  • setFlag(flag): this
  • Parameters

    Returns this

inheritedsetFlushMode

  • setFlushMode(flushMode): this
  • Parameters

    Returns this

inheritedsetLockMode

  • setLockMode(mode, tables): this
  • Parameters

    • optionalmode: LockMode
    • optionaltables: string[]

    Returns this

inheritedsetLoggerContext

  • setLoggerContext(context): void
  • Sets logger context for this query builder.


    Parameters

    Returns void

inheritedstream

  • stream(options): AsyncIterableIterator<Loaded<Entity, any>, any, any>
  • Executes the query and returns an async iterable (async generator) that yields results one by one. By default, the results are merged and mapped to entity instances, without adding them to the identity map. You can disable merging and mapping by passing the options { mergeResults: false, mapResults: false }. This is useful for processing large datasets without loading everything into memory at once.

    const qb = em.createQueryBuilder(Book, 'b');
    qb.select('*').where({ title: '1984' }).leftJoinAndSelect('b.author', 'a');

    for await (const book of qb.stream()) {
    // book is an instance of Book entity
    console.log(book.title, book.author.name);
    }

    Parameters

    Returns AsyncIterableIterator<Loaded<Entity, any>, any, any>

inheritedtoQuery

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

    • params: readonly unknown[]
    • sql: string

inheritedtoRaw

  • Returns raw fragment representation of this QueryBuilder.


    Returns RawQueryFragment

inheritedtruncate

inheritedunsetFlag

  • unsetFlag(flag): this
  • Parameters

    Returns this

inheritedupdate

inheritedwhere

  • where(cond, operator): this
  • where(cond, params, operator): this
  • Parameters

    Returns this

inheritedwithSchema

  • withSchema(schema): this
  • Parameters

    • optionalschema: string

    Returns this

inheritedwithSubQuery

  • withSubQuery(subQuery, alias): this
  • Parameters

    Returns this