Skip to main content
Module

x/typeorm/src/query-builder/SelectQueryBuilder.ts>SelectQueryBuilder

Forked from https://github.com/typeorm/typeorm
Latest
class SelectQueryBuilder
implements WhereExpression
import { SelectQueryBuilder } from "https://deno.land/x/typeorm@v0.2.23-rc10/src/query-builder/SelectQueryBuilder.ts";

Allows to build complex sql queries in a fashion way and execute those queries.

Methods

protected
buildEscapedEntityColumnSelects(aliasName: string, metadata: EntityMetadata): SelectQuery[]

Creates "GROUP BY" part of SQL query.

Creates "HAVING" part of SQL query.

protected
createJoinExpression(): string

Creates "JOIN" part of SQL query.

protected
createLimitOffsetExpression(): string

Creates "LIMIT" and "OFFSET" parts of SQL query.

protected
createLockExpression(): string

Creates "LOCK" part of SQL query.

protected
createOrderByCombinedWithSelectExpression(parentAlias: string): [string, OrderByCondition]

Creates "ORDER BY" part of SQL query.

Creates select | select distinct part of SQL query.

Creates "SELECT FROM" part of SQL query.

protected
executeCountQuery(queryRunner: QueryRunner): Promise<number>
protected
executeEntitiesAndRawResults(queryRunner: QueryRunner): Promise<{ entities: Entity[]; raw: any[]; }>

Executes sql generated by query builder and returns object with raw results and entities created from them.

protected
findEntityColumnSelects(aliasName: string, metadata: EntityMetadata): SelectQuery[]
protected
join(
direction: "INNER" | "LEFT",
entityOrProperty: Function | string | ((qb: SelectQueryBuilder<any>) => SelectQueryBuilder<any>),
aliasName: string,
condition?: string,
parameters?: ObjectLiteral,
mapToProperty?: string,
isMappingMany?: boolean,
): void
protected
loadRawResults(queryRunner: QueryRunner)

Loads raw results from the database.

protected
mergeExpressionMap(expressionMap: Partial<QueryExpressionMap>): this

Merges into expression map given expression map properties.

protected
normalizeNumber(num: any)

Normalizes a give number - converts to int if possible.

Creates a query builder used to execute sql queries inside this query builder.

addFrom<T>(entityTarget: (qb: SelectQueryBuilder<any>) => SelectQueryBuilder<any>, aliasName: string): SelectQueryBuilder<T>

Specifies FROM which entity's table select/update/delete will be executed. Also sets a main string alias of the selection data.

addFrom<T>(entityTarget: ObjectType<T> | string, aliasName: string): SelectQueryBuilder<T>

Specifies FROM which entity's table select/update/delete will be executed. Also sets a main string alias of the selection data.

addGroupBy(groupBy: string): this

Adds GROUP BY condition in the query builder.

addOrderBy(
sort: string,
order?: "ASC" | "DESC",
nulls?: "NULLS FIRST" | "NULLS LAST",
): this

Adds ORDER BY condition in the query builder.

addSelect(selection: (qb: SelectQueryBuilder<any>) => SelectQueryBuilder<any>, selectionAliasName?: string): this

Adds new selection to the SELECT query.

addSelect(selection: string, selectionAliasName?: string): this

Adds new selection to the SELECT query.

addSelect(selection: string[]): this

Adds new selection to the SELECT query.

andHaving(having: string, parameters?: ObjectLiteral): this

Adds new AND HAVING condition in the query builder. Additionally you can add parameters used in where expression.

andWhere(where: string | Brackets | ((qb: this) => string), parameters?: ObjectLiteral): this

Adds new AND WHERE condition in the query builder. Additionally you can add parameters used in where expression.

andWhereInIds(ids: any | any[]): this

Adds new AND WHERE with conditions for the given ids.

Ids are mixed. It means if you have single primary key you can pass a simple id values, for example [1, 2, 3]. If you have multiple primary keys you need to pass object with property names and values specified, for example [{ firstId: 1, secondId: 2 }, { firstId: 2, secondId: 3 }, ...]

cache(enabled: boolean): this

Enables or disables query result caching.

cache(milliseconds: number): this

Enables query result caching and sets in milliseconds in which cache will expire. If not set then global caching time will be used.

cache(id: any, milliseconds?: number): this

Enables query result caching and sets cache id and milliseconds in which cache will expire.

distinct(distinct?: boolean): this

Sets whether the selection is DISTINCT.

distinctOn(distinctOn: string[]): this

Sets the distinct on clause for Postgres.

from<T>(entityTarget: (qb: SelectQueryBuilder<any>) => SelectQueryBuilder<any>, aliasName: string): SelectQueryBuilder<T>

Specifies FROM which entity's table select/update/delete will be executed. Also sets a main string alias of the selection data. Removes all previously set from-s.

from<T>(entityTarget: ObjectType<T> | string, aliasName: string): SelectQueryBuilder<T>

Specifies FROM which entity's table select/update/delete will be executed. Also sets a main string alias of the selection data. Removes all previously set from-s.

getCount(): Promise<number>

Gets count - number of entities selected by sql generated by this query builder. Count excludes all limitations set by setFirstResult and setMaxResults methods call.

getMany(): Promise<Entity[]>

Gets entities returned by execution of generated query builder sql.

getManyAndCount(): Promise<[Entity[], number]>

Executes built SQL query and returns entities and overall entities count (without limitation). This method is useful to build pagination.

getOne(): Promise<Entity | undefined>

Gets single entity returned by execution of generated query builder sql.

getQuery(): string

Gets generated sql query without parameters being replaced.

getRawAndEntities(): Promise<{ entities: Entity[]; raw: any[]; }>

Executes sql generated by query builder and returns object with raw results and entities created from them.

getRawMany(): Promise<any[]>

Gets all raw results returned by execution of generated query builder sql.

getRawOne(): Promise<any>

Gets first raw result returned by execution of generated query builder sql.

groupBy(): this

Sets GROUP BY condition in the query builder. If you had previously GROUP BY expression defined, calling this function will override previously set GROUP BY conditions.

groupBy(groupBy: string): this

Sets GROUP BY condition in the query builder. If you had previously GROUP BY expression defined, calling this function will override previously set GROUP BY conditions.

having(having: string, parameters?: ObjectLiteral): this

Sets HAVING condition in the query builder. If you had previously HAVING expression defined, calling this function will override previously set HAVING conditions. Additionally you can add parameters used in where expression.

innerJoin(
subQueryFactory: (qb: SelectQueryBuilder<any>) => SelectQueryBuilder<any>,
alias: string,
condition?: string,
parameters?: ObjectLiteral,
): this

INNER JOINs (without selection) given subquery. You also need to specify an alias of the joined data. Optionally, you can add condition and parameters used in condition.

innerJoin(
property: string,
alias: string,
condition?: string,
parameters?: ObjectLiteral,
): this

INNER JOINs (without selection) entity's property. Given entity property should be a relation. You also need to specify an alias of the joined data. Optionally, you can add condition and parameters used in condition.

innerJoin(
entity: Function | string,
alias: string,
condition?: string,
parameters?: ObjectLiteral,
): this

INNER JOINs (without selection) given entity's table. You also need to specify an alias of the joined data. Optionally, you can add condition and parameters used in condition.

innerJoin(
tableName: string,
alias: string,
condition?: string,
parameters?: ObjectLiteral,
): this

INNER JOINs (without selection) given table. You also need to specify an alias of the joined data. Optionally, you can add condition and parameters used in condition.

innerJoinAndMapMany(
mapToProperty: string,
subQueryFactory: (qb: SelectQueryBuilder<any>) => SelectQueryBuilder<any>,
alias: string,
condition?: string,
parameters?: ObjectLiteral,
): this

INNER JOINs given subquery, SELECTs the data returned by a join and MAPs all that data to some entity's property. This is extremely useful when you want to select some data and map it to some virtual property. It will assume that there are multiple rows of selecting data, and mapped result will be an array. Given entity property should be a relation. You also need to specify an alias of the joined data. Optionally, you can add condition and parameters used in condition.

innerJoinAndMapMany(
mapToProperty: string,
property: string,
alias: string,
condition?: string,
parameters?: ObjectLiteral,
): this

INNER JOINs entity's property, SELECTs the data returned by a join and MAPs all that data to some entity's property. This is extremely useful when you want to select some data and map it to some virtual property. It will assume that there are multiple rows of selecting data, and mapped result will be an array. Given entity property should be a relation. You also need to specify an alias of the joined data. Optionally, you can add condition and parameters used in condition.

innerJoinAndMapMany(
mapToProperty: string,
entity: Function | string,
alias: string,
condition: string,
parameters?: ObjectLiteral,
): this

INNER JOINs entity's table, SELECTs the data returned by a join and MAPs all that data to some entity's property. This is extremely useful when you want to select some data and map it to some virtual property. It will assume that there are multiple rows of selecting data, and mapped result will be an array. You also need to specify an alias of the joined data. Optionally, you can add condition and parameters used in condition.

innerJoinAndMapMany(
mapToProperty: string,
tableName: string,
alias: string,
condition: string,
parameters?: ObjectLiteral,
): this

INNER JOINs table, SELECTs the data returned by a join and MAPs all that data to some entity's property. This is extremely useful when you want to select some data and map it to some virtual property. It will assume that there are multiple rows of selecting data, and mapped result will be an array. You also need to specify an alias of the joined data. Optionally, you can add condition and parameters used in condition.

innerJoinAndMapOne(
mapToProperty: string,
subQueryFactory: (qb: SelectQueryBuilder<any>) => SelectQueryBuilder<any>,
alias: string,
condition?: string,
parameters?: ObjectLiteral,
): this

INNER JOINs given subquery, SELECTs the data returned by a join and MAPs all that data to some entity's property. This is extremely useful when you want to select some data and map it to some virtual property. It will assume that there is a single row of selecting data, and mapped result will be a single selected value. Given entity property should be a relation. You also need to specify an alias of the joined data. Optionally, you can add condition and parameters used in condition.

innerJoinAndMapOne(
mapToProperty: string,
property: string,
alias: string,
condition?: string,
parameters?: ObjectLiteral,
): this

INNER JOINs entity's property, SELECTs the data returned by a join and MAPs all that data to some entity's property. This is extremely useful when you want to select some data and map it to some virtual property. It will assume that there is a single row of selecting data, and mapped result will be a single selected value. Given entity property should be a relation. You also need to specify an alias of the joined data. Optionally, you can add condition and parameters used in condition.

innerJoinAndMapOne(
mapToProperty: string,
entity: Function | string,
alias: string,
condition: string,
parameters?: ObjectLiteral,
): this

INNER JOINs entity's table, SELECTs the data returned by a join and MAPs all that data to some entity's property. This is extremely useful when you want to select some data and map it to some virtual property. It will assume that there is a single row of selecting data, and mapped result will be a single selected value. You also need to specify an alias of the joined data. Optionally, you can add condition and parameters used in condition.

innerJoinAndMapOne(
mapToProperty: string,
tableName: string,
alias: string,
condition: string,
parameters?: ObjectLiteral,
): this

INNER JOINs table, SELECTs the data returned by a join and MAPs all that data to some entity's property. This is extremely useful when you want to select some data and map it to some virtual property. It will assume that there is a single row of selecting data, and mapped result will be a single selected value. You also need to specify an alias of the joined data. Optionally, you can add condition and parameters used in condition.

innerJoinAndSelect(
subQueryFactory: (qb: SelectQueryBuilder<any>) => SelectQueryBuilder<any>,
alias: string,
condition?: string,
parameters?: ObjectLiteral,
): this

INNER JOINs given subquery and adds all selection properties to SELECT.. You also need to specify an alias of the joined data. Optionally, you can add condition and parameters used in condition.

innerJoinAndSelect(
property: string,
alias: string,
condition?: string,
parameters?: ObjectLiteral,
): this

INNER JOINs entity's property and adds all selection properties to SELECT. Given entity property should be a relation. You also need to specify an alias of the joined data. Optionally, you can add condition and parameters used in condition.

innerJoinAndSelect(
entity: Function | string,
alias: string,
condition?: string,
parameters?: ObjectLiteral,
): this

INNER JOINs entity and adds all selection properties to SELECT. You also need to specify an alias of the joined data. Optionally, you can add condition and parameters used in condition.

innerJoinAndSelect(
tableName: string,
alias: string,
condition?: string,
parameters?: ObjectLiteral,
): this

INNER JOINs table and adds all selection properties to SELECT. You also need to specify an alias of the joined data. Optionally, you can add condition and parameters used in condition.

leftJoin(
subQueryFactory: (qb: SelectQueryBuilder<any>) => SelectQueryBuilder<any>,
alias: string,
condition?: string,
parameters?: ObjectLiteral,
): this

LEFT JOINs (without selection) given subquery. You also need to specify an alias of the joined data. Optionally, you can add condition and parameters used in condition.

leftJoin(
property: string,
alias: string,
condition?: string,
parameters?: ObjectLiteral,
): this

LEFT JOINs (without selection) entity's property. Given entity property should be a relation. You also need to specify an alias of the joined data. Optionally, you can add condition and parameters used in condition.

leftJoin(
entity: Function | string,
alias: string,
condition?: string,
parameters?: ObjectLiteral,
): this

LEFT JOINs (without selection) entity's table. You also need to specify an alias of the joined data. Optionally, you can add condition and parameters used in condition.

leftJoin(
tableName: string,
alias: string,
condition?: string,
parameters?: ObjectLiteral,
): this

LEFT JOINs (without selection) given table. You also need to specify an alias of the joined data. Optionally, you can add condition and parameters used in condition.

leftJoinAndMapMany(
mapToProperty: string,
subQueryFactory: (qb: SelectQueryBuilder<any>) => SelectQueryBuilder<any>,
alias: string,
condition?: string,
parameters?: ObjectLiteral,
): this

LEFT JOINs given subquery, SELECTs the data returned by a join and MAPs all that data to some entity's property. This is extremely useful when you want to select some data and map it to some virtual property. It will assume that there are multiple rows of selecting data, and mapped result will be an array. Given entity property should be a relation. You also need to specify an alias of the joined data. Optionally, you can add condition and parameters used in condition.

leftJoinAndMapMany(
mapToProperty: string,
property: string,
alias: string,
condition?: string,
parameters?: ObjectLiteral,
): this

LEFT JOINs entity's property, SELECTs the data returned by a join and MAPs all that data to some entity's property. This is extremely useful when you want to select some data and map it to some virtual property. It will assume that there are multiple rows of selecting data, and mapped result will be an array. Given entity property should be a relation. You also need to specify an alias of the joined data. Optionally, you can add condition and parameters used in condition.

leftJoinAndMapMany(
mapToProperty: string,
entity: Function | string,
alias: string,
condition: string,
parameters?: ObjectLiteral,
): this

LEFT JOINs entity's table, SELECTs the data returned by a join and MAPs all that data to some entity's property. This is extremely useful when you want to select some data and map it to some virtual property. It will assume that there are multiple rows of selecting data, and mapped result will be an array. You also need to specify an alias of the joined data. Optionally, you can add condition and parameters used in condition.

leftJoinAndMapMany(
mapToProperty: string,
tableName: string,
alias: string,
condition: string,
parameters?: ObjectLiteral,
): this

LEFT JOINs table, SELECTs the data returned by a join and MAPs all that data to some entity's property. This is extremely useful when you want to select some data and map it to some virtual property. It will assume that there are multiple rows of selecting data, and mapped result will be an array. You also need to specify an alias of the joined data. Optionally, you can add condition and parameters used in condition.

leftJoinAndMapOne(
mapToProperty: string,
subQueryFactory: (qb: SelectQueryBuilder<any>) => SelectQueryBuilder<any>,
alias: string,
condition?: string,
parameters?: ObjectLiteral,
): this

LEFT JOINs given subquery, SELECTs the data returned by a join and MAPs all that data to some entity's property. This is extremely useful when you want to select some data and map it to some virtual property. It will assume that there is a single row of selecting data, and mapped result will be a single selected value. Given entity property should be a relation. You also need to specify an alias of the joined data. Optionally, you can add condition and parameters used in condition.

leftJoinAndMapOne(
mapToProperty: string,
property: string,
alias: string,
condition?: string,
parameters?: ObjectLiteral,
): this

LEFT JOINs entity's property, SELECTs the data returned by a join and MAPs all that data to some entity's property. This is extremely useful when you want to select some data and map it to some virtual property. It will assume that there is a single row of selecting data, and mapped result will be a single selected value. Given entity property should be a relation. You also need to specify an alias of the joined data. Optionally, you can add condition and parameters used in condition.

leftJoinAndMapOne(
mapToProperty: string,
entity: Function | string,
alias: string,
condition: string,
parameters?: ObjectLiteral,
): this

LEFT JOINs entity's table, SELECTs the data returned by a join and MAPs all that data to some entity's property. This is extremely useful when you want to select some data and map it to some virtual property. It will assume that there is a single row of selecting data, and mapped result will be a single selected value. You also need to specify an alias of the joined data. Optionally, you can add condition and parameters used in condition.

leftJoinAndMapOne(
mapToProperty: string,
tableName: string,
alias: string,
condition: string,
parameters?: ObjectLiteral,
): this

LEFT JOINs table, SELECTs the data returned by a join and MAPs all that data to some entity's property. This is extremely useful when you want to select some data and map it to some virtual property. It will assume that there is a single row of selecting data, and mapped result will be a single selected value. You also need to specify an alias of the joined data. Optionally, you can add condition and parameters used in condition.

leftJoinAndSelect(
subQueryFactory: (qb: SelectQueryBuilder<any>) => SelectQueryBuilder<any>,
alias: string,
condition?: string,
parameters?: ObjectLiteral,
): this

LEFT JOINs given subquery and adds all selection properties to SELECT.. You also need to specify an alias of the joined data. Optionally, you can add condition and parameters used in condition.

leftJoinAndSelect(
property: string,
alias: string,
condition?: string,
parameters?: ObjectLiteral,
): this

LEFT JOINs entity's property and adds all selection properties to SELECT. Given entity property should be a relation. You also need to specify an alias of the joined data. Optionally, you can add condition and parameters used in condition.

leftJoinAndSelect(
entity: Function | string,
alias: string,
condition: string,
parameters?: ObjectLiteral,
): this

LEFT JOINs entity and adds all selection properties to SELECT. You also need to specify an alias of the joined data. Optionally, you can add condition and parameters used in condition.

leftJoinAndSelect(
tableName: string,
alias: string,
condition?: string,
parameters?: ObjectLiteral,
): this

LEFT JOINs table and adds all selection properties to SELECT. You also need to specify an alias of the joined data. Optionally, you can add condition and parameters used in condition.

limit(limit?: number): this

Set's LIMIT - maximum number of rows to be selected. NOTE that it may not work as you expect if you are using joins. If you want to implement pagination, and you are having join in your query, then use instead take method instead.

loadAllRelationIds(options?: { relations?: string[]; disableMixedMap?: boolean; }): this

Loads all relation ids for all relations of the selected entity. All relation ids will be mapped to relation property themself. If array of strings is given then loads only relation ids of the given properties.

loadRelationCountAndMap(
mapToProperty: string,
relationName: string,
aliasName?: string,
queryBuilderFactory?: (qb: SelectQueryBuilder<any>) => SelectQueryBuilder<any>,
): this

Counts number of entities of entity's relation and maps the value into some entity's property. Optionally, you can add condition and parameters used in condition.

loadRelationIdAndMap(
mapToProperty: string,
relationName: string,
options?: { disableMixedMap?: boolean; },
): this

LEFT JOINs relation id and maps it into some entity's property. Optionally, you can add condition and parameters used in condition.

loadRelationIdAndMap(
mapToProperty: string,
relationName: string,
alias: string,
queryBuilderFactory: (qb: SelectQueryBuilder<any>) => SelectQueryBuilder<any>,
): this

LEFT JOINs relation id and maps it into some entity's property. Optionally, you can add condition and parameters used in condition.

offset(offset?: number): this

Set's OFFSET - selection offset. NOTE that it may not work as you expect if you are using joins. If you want to implement pagination, and you are having join in your query, then use instead skip method instead.

orderBy(): this

Sets ORDER BY condition in the query builder. If you had previously ORDER BY expression defined, calling this function will override previously set ORDER BY conditions.

Calling order by without order set will remove all previously set order bys.

orderBy(
sort: string,
order?: "ASC" | "DESC",
nulls?: "NULLS FIRST" | "NULLS LAST",
): this

Sets ORDER BY condition in the query builder. If you had previously ORDER BY expression defined, calling this function will override previously set ORDER BY conditions.

Sets ORDER BY condition in the query builder. If you had previously ORDER BY expression defined, calling this function will override previously set ORDER BY conditions.

orHaving(having: string, parameters?: ObjectLiteral): this

Adds new OR HAVING condition in the query builder. Additionally you can add parameters used in where expression.

orWhere(where: Brackets | string | ((qb: this) => string), parameters?: ObjectLiteral): this

Adds new OR WHERE condition in the query builder. Additionally you can add parameters used in where expression.

orWhereInIds(ids: any | any[]): this

Adds new OR WHERE with conditions for the given ids.

Ids are mixed. It means if you have single primary key you can pass a simple id values, for example [1, 2, 3]. If you have multiple primary keys you need to pass object with property names and values specified, for example [{ firstId: 1, secondId: 2 }, { firstId: 2, secondId: 3 }, ...]

select(): this

Creates SELECT query. Replaces all previous selections if they exist.

select(selection: (qb: SelectQueryBuilder<any>) => SelectQueryBuilder<any>, selectionAliasName?: string): this

Creates SELECT query. Replaces all previous selections if they exist.

select(selection: string, selectionAliasName?: string): this

Creates SELECT query and selects given data. Replaces all previous selections if they exist.

select(selection: string[]): this

Creates SELECT query and selects given data. Replaces all previous selections if they exist.

setLock(lockMode: "optimistic", lockVersion: number): this

Sets locking mode.

setLock(lockMode: "optimistic", lockVersion: Date): this

Sets locking mode.

setLock(lockMode: "pessimistic_read" | "pessimistic_write" | "dirty_read"): this

Sets locking mode.

Sets extra options that can be used to configure how query builder works.

skip(skip?: number): this

Sets number of entities to skip.

stream(): Promise<ReadStream>

Executes built SQL query and returns raw data stream.

Creates a subquery - query that can be used inside other queries.

take(take?: number): this

Sets maximal number of entities to take.

where(where:
| string
| ((qb: this) => string)
, parameters?: ObjectLiteral
): this

Sets WHERE condition in the query builder. If you had previously WHERE expression defined, calling this function will override previously set WHERE conditions. Additionally you can add parameters used in where expression.

whereInIds(ids: any | any[]): this

Adds new AND WHERE with conditions for the given ids.

Ids are mixed. It means if you have single primary key you can pass a simple id values, for example [1, 2, 3]. If you have multiple primary keys you need to pass object with property names and values specified, for example [{ firstId: 1, secondId: 2 }, { firstId: 2, secondId: 3 }, ...]