ModelQuery: add offset parameter to union()

This commit is contained in:
Alice Gaudon 2020-09-11 15:15:15 +02:00
parent fd6e384a12
commit fec607da20

View File

@ -124,7 +124,7 @@ export default class ModelQuery<M extends Model> implements WhereFieldConsumer<M
return this; return this;
} }
public union(query: ModelQuery<any>, sortBy: string, direction: SortDirection = 'ASC', raw: boolean = false, limit?: number): this { public union(query: ModelQuery<any>, sortBy: string, direction: SortDirection = 'ASC', raw: boolean = false, limit?: number, offset?: number): this {
if (this.type !== QueryType.SELECT) throw new Error('Union queries are only implemented with SELECT.'); if (this.type !== QueryType.SELECT) throw new Error('Union queries are only implemented with SELECT.');
this._union = { this._union = {
@ -132,6 +132,7 @@ export default class ModelQuery<M extends Model> implements WhereFieldConsumer<M
sortBy: raw ? sortBy : inputToFieldOrValue(sortBy), sortBy: raw ? sortBy : inputToFieldOrValue(sortBy),
direction: direction, direction: direction,
limit: limit, limit: limit,
offset: offset,
}; };
return this; return this;
} }
@ -207,7 +208,8 @@ export default class ModelQuery<M extends Model> implements WhereFieldConsumer<M
if (this._union) { if (this._union) {
const unionOrderBy = this._union.sortBy ? ` ORDER BY ${this._union.sortBy} ${this._union.direction}` : ''; const unionOrderBy = this._union.sortBy ? ` ORDER BY ${this._union.sortBy} ${this._union.direction}` : '';
const unionLimit = typeof this._union.limit === 'number' ? ` LIMIT ${this._union.limit}` : ''; const unionLimit = typeof this._union.limit === 'number' ? ` LIMIT ${this._union.limit}` : '';
query = `(${query}) UNION ${this._union.query.toString(false)}${unionOrderBy}${unionLimit}`; const unionOffset = typeof this._union.offset === 'number' ? ` OFFSET ${this._union.offset}` : '';
query = `(${query}) UNION ${this._union.query.toString(false)}${unionOrderBy}${unionLimit}${unionOffset}`;
} }
break; break;
case QueryType.UPDATE: case QueryType.UPDATE:
@ -448,7 +450,7 @@ class WhereFieldValueGroup {
} }
} }
interface WhereFieldConsumer<M extends Model> { export interface WhereFieldConsumer<M extends Model> {
where(field: string, value: string | Date | ModelQuery<any> | any, test?: WhereTest, operator?: WhereOperator): this; where(field: string, value: string | Date | ModelQuery<any> | any, test?: WhereTest, operator?: WhereOperator): this;
groupWhere(setter: (query: WhereFieldConsumer<M>) => void, operator?: WhereOperator): this; groupWhere(setter: (query: WhereFieldConsumer<M>) => void, operator?: WhereOperator): this;
@ -463,4 +465,5 @@ type ModelQueryUnion = {
sortBy: string, sortBy: string,
direction: SortDirection, direction: SortDirection,
limit?: number, limit?: number,
offset?: number,
}; };