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;
}
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.');
this._union = {
@ -132,6 +132,7 @@ export default class ModelQuery<M extends Model> implements WhereFieldConsumer<M
sortBy: raw ? sortBy : inputToFieldOrValue(sortBy),
direction: direction,
limit: limit,
offset: offset,
};
return this;
}
@ -207,7 +208,8 @@ export default class ModelQuery<M extends Model> implements WhereFieldConsumer<M
if (this._union) {
const unionOrderBy = this._union.sortBy ? ` ORDER BY ${this._union.sortBy} ${this._union.direction}` : '';
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;
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;
groupWhere(setter: (query: WhereFieldConsumer<M>) => void, operator?: WhereOperator): this;
@ -463,4 +465,5 @@ type ModelQueryUnion = {
sortBy: string,
direction: SortDirection,
limit?: number,
offset?: number,
};