UNPKG

ts-db-helper

Version:
130 lines (129 loc) 4.24 kB
import { CompositeClause } from './composite-clause.model'; import { IQueryHelper } from '../interfaces/i-query-helper.interface'; import { IJoin } from '../interfaces/i-join.interface'; import { DbTable } from '../structure/db-table.model'; import { ClauseGroup } from './clause-group.model'; import { Clause } from './clause.model'; import { QueryPart } from './query-part.model'; import { QuerySelect } from './query-select.model'; import { DbHelperModel } from '../db-helper-model.model'; /** * @public * @class QueryJoin * * @description * For design reasons this class should not be used directly. Use this class with {@link Join} function. * * @param T @extends DbHelperModel a model declared with table and column annotations * * @example * ```typescript * // Create new model instance * const todo = new Todo(); * // manipulates todo instance and then insert it * const select = Select(Todo); * const join = Join(Task).type(JoinType.LEFT).on({id: new ColumnClauseValue('taskId', select)}); * select.join(join).where({isDone: false}).execRaw().subscribe((qr: QueryResult<any>) => { * // collect todo and task, task column will be prefixed with join.alias value * }, (err) => { * console.error(err); * }); * ``` * * @author Olivier Margarit * @since 0.2 */ export declare class QueryJoin<T extends DbHelperModel> implements IJoin, IQueryHelper { private model; /** * @private * @property {string} joinType * * @default JoinType.DEFAULT */ private joinType; /** * @private * @property {ClauseGroup} onGroup group of 'on' join conditions */ private onGroup; /** * @public * @property {string} alias alias can be setted by hand or will be automatically setted by the main query */ alias: string; /** * @public * @constructor * @param {{new(): T} | QuerySelect<T>} model the model or sub query select to join */ constructor(model: { new (): T; } | QuerySelect<T>); /** * @public * @method type set join type to join query part * * @param {string} type see JoinTypes class to check possible values * * @return {QueryJoin<T>} the current instance to chain operations */ type(type: string): QueryJoin<T>; /** * @public * @method on method to add on join clauses * * @param {Clause|Clause[]|ClauseGroup|CompositeClause|{[index: string]: any}} clauses on join clauses * * @return {QueryJoin<T>} the current instance to chain operations */ on(clauses: Clause | Clause[] | ClauseGroup | CompositeClause | { [index: string]: any; }): QueryJoin<T>; /** * @public * @method getProjectedTable get a virtual table to customize query projection and model retrieving * * @return {DbTable} the virtual model of the joined query */ getProjectedTable(): DbTable; /** * @public * @method build should be removed to be a part of the private API * * @return {DbQuery} of the query with the string part and * clauses params. */ build(): QueryPart; } /** * @public * @function Join * * @description * Create a join part of a query to retrieve linked datas to a main query * * @param T @extends DbHelperModel a model declared with table and column annotations * * @example * ```typescript * // Create new model instance * const todo = new Todo(); * // manipulates todo instance and then insert it * const select = Select(Todo); * const join = Join(Task).type(JoinType.LEFT).on({id: new ColumnClauseValue('taskId', select)}); * select.join(join).where({isDone: false}).execRaw().subscribe((qr: QueryResult<any>) => { * // collect todo and task, task column will be prefixed with join.alias value * }, (err) => { * console.error(err); * }); * ``` * * @return {QueryJoin<T>} a query join instance * * @author Olivier Margarit * @since 0.2 */ export declare function Join<T extends DbHelperModel>(model: { new (): T; } | QuerySelect<T>): QueryJoin<T>;