diff --git a/src/db/Model.ts b/src/db/Model.ts index c4c2fc1..5d6965a 100644 --- a/src/db/Model.ts +++ b/src/db/Model.ts @@ -16,9 +16,10 @@ export default abstract class Model { return models.length > 0 ? models[0] : null; } - public static async paginate(request: Request, perPage: number = 20): Promise { + public static async paginate(request: Request, perPage: number = 20, query?: Query): Promise { let page = request.params.page ? parseInt(request.params.page) : 1; - let query: Query = this.select().limit(perPage, (page - 1) * perPage).withTotalRowCount(); + if (!query) query = this.select(); + query = query.limit(perPage, (page - 1) * perPage).withTotalRowCount(); if (request.params.sortBy) { const dir = request.params.sortDirection; query = query.sortBy(request.params.sortBy, dir === 'ASC' || dir === 'DESC' ? dir : undefined);