From fa42b8991b8f399b1d1c51823cc3108e5352dda3 Mon Sep 17 00:00:00 2001 From: Alice Gaudon Date: Sun, 14 Jun 2020 21:47:51 +0200 Subject: [PATCH] Add query customization to model pagination --- src/db/Model.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) 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);