Commit 74b5652d authored by William Naslund's avatar William Naslund

Added limit and offset to query

parent 696a24ba
......@@ -23,7 +23,7 @@ export class PGQueryAdapter implements DBQueryAdapter {
sql += ' ';
// FROM
sql += `FROM ${baseInfo.name} AS __`;
sql += `FROM ${baseInfo.options.schema}.${baseInfo.name} AS __`;
for(const parentField in req.parents) {
sql += ' ' + this.getParentJoins(`__${parentField}`, parentField, '', req.parents[parentField]).join(' ');
}
......@@ -39,6 +39,14 @@ export class PGQueryAdapter implements DBQueryAdapter {
sql += 'ORDER BY ' + req.orderBy.trim() + ' ';
}
// LIMIT and OFFSET
if(req.limit != null) {
sql += 'LIMIT ' + req.limit + ' ';
}
if(req.offset != null) {
sql += 'OFFSET ' + req.offset + ' ';
}
const recordList: DBQueryResponse[] = [];
for(const row of await this.db.command.query(sql, req.whereArgs)) {
const record: DBQueryResponse = {
......
......@@ -92,6 +92,18 @@ export class DBQuery<T extends DBModel> {
return this;
}
/** Limits the number of rows in the result */
public limit(count: number): this {
this.req.limit = count;
return this;
}
/** Offsets the results */
public offset(count: number): this {
this.req.offset = count;
return this;
}
/** Adds an order by condition to this query */
public orderBy(sql: string): this {
this.req.orderBy = sql;
......@@ -156,6 +168,8 @@ export interface DBQueryRequest<T extends DBModel = any> {
where: string;
whereArgs?: any[];
orderBy?: string;
limit?: number;
offset?: number;
parents: { [name: string]: DBQueryParent<any>; }
children: { [name: string]: DBQueryRequest; }
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment