From 8ed44f3b4ead5355d60a79721a24803edf46eb79 Mon Sep 17 00:00:00 2001 From: Mike Keen Date: Wed, 5 Jan 2022 15:18:26 -0500 Subject: [PATCH] fix: pagination optional --- src/models/projects/projects.model.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/models/projects/projects.model.js b/src/models/projects/projects.model.js index 6c7b6861..db272c0e 100644 --- a/src/models/projects/projects.model.js +++ b/src/models/projects/projects.model.js @@ -85,9 +85,13 @@ class Project extends Model { replacements })).length; + if (limit && offset) { + sql = `${sql} ORDER BY relevance DESC LIMIT :limit OFFSET :offset`; + } + return { count, - rows: await sequelize.query(`${sql} ORDER BY relevance DESC LIMIT :limit OFFSET :offset`, { + rows: await sequelize.query(sql, { model: Project, replacements: {...replacements, ...{offset, limit}}, mapToModel: true, // pass true here if you have any mapped fields @@ -114,9 +118,13 @@ class Project extends Model { replacements })).length; + if (limit && offset) { + sql = `${sql} ORDER BY rank DESC LIMIT :limit OFFSET :offset`; + } + return { count, - rows: await sequelize.query(`${sql} ORDER BY rank DESC LIMIT :limit OFFSET :offset`, { + rows: await sequelize.query(sql, { model: Project, mapToModel: true, // pass true here if you have any mapped fields replacements: {...replacements, ...{offset, limit}}