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>

type

Methods

addSelect

andHaving

andOrderBy

andWhere

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

applyFilters

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


    Parameters

    Returns Promise<void>

as

  • 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

cache

  • cache(config): this
  • Parameters

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

    Returns this

clone

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

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

    Returns QueryBuilder<Entity, never, never, never>

comment

  • 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

count

  • Parameters

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

    Returns CountQueryBuilder<Entity>

delete

distinct

distinctOn

  • postgres only


    Parameters

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

    Returns SelectQueryBuilder<Entity, RootAlias, Hint, Context>

execute

  • execute<U>(method, options): 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, 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>

getFormattedQuery

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


    Returns string

getLoggerContext

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


    Returns T

getNativeQuery

  • getNativeQuery(processVirtualEntity): NativeQueryBuilder
  • Parameters

    • processVirtualEntity: boolean = true

    Returns NativeQueryBuilder

getParams

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


    Returns readonly unknown[]

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): Promise<Loaded<Entity, Hint>[]>
  • Executes the query, returning array of results mapped to entity instances.


    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

  • Parameters

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

    Returns SelectQueryBuilder<Entity, RootAlias, Hint, Context>

hasFlag

  • hasFlag(flag): boolean

having

hintComment

  • 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

ignore

  • ignore(): this
  • Returns this

indexHint

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


    Parameters

    • sql: undefined | string

    Returns this

innerJoin

  • innerJoin<Field, Alias>(field, alias, cond, schema): 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

    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, Alias>(field, alias, cond, fields, schema): 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

    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, alias, cond, schema): this

innerJoinLateralAndSelect

  • innerJoinLateralAndSelect<Field, Alias>(field, alias, cond, fields, schema): 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

    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, Alias>(field, alias, cond, type, path, schema): 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

    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, Alias>(field, alias, cond, type, path, fields, schema): 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

    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, Alias>(field, alias, cond, schema): 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

    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, Alias>(field, alias, cond, fields, schema): 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

    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, alias, cond, schema): this

leftJoinLateralAndSelect

  • leftJoinLateralAndSelect<Field, Alias>(field, alias, cond, fields, schema): 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

    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

  • Parameters

    • optionallimit: number
    • offset: number = 0

    Returns SelectQueryBuilder<Entity, RootAlias, Hint, Context>

merge

  • merge(data): this

offset

  • Parameters

    • optionaloffset: number

    Returns SelectQueryBuilder<Entity, RootAlias, Hint, Context>

onConflict

orderBy

orHaving

orWhere

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

returning

  • returning(fields): this
  • Parameters

    Returns this

select

setFlag

  • setFlag(flag): this

setFlushMode

  • setFlushMode(flushMode): this
  • Parameters

    Returns this

setLockMode

  • setLockMode(mode, tables): this
  • Parameters

    • optionalmode: LockMode
    • optionaltables: string[]

    Returns this

setLoggerContext

  • setLoggerContext(context): void

stream

  • stream(options): AsyncIterableIterator<Loaded<Entity, Hint>, 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, Hint>, any, any>

toQuery

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

    • params: readonly unknown[]
    • sql: string

toRaw

  • Returns raw fragment representation of this QueryBuilder.


    Returns RawQueryFragment

truncate

unsetFlag

  • unsetFlag(flag): this

update

where

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

    Returns this

withSchema

  • withSchema(schema): this
  • Parameters

    • optionalschema: string

    Returns this

withSubQuery

  • withSubQuery(subQuery, alias): this
  • Parameters

    Returns this