From 679c197c7091e7020c2a37df1847960c5532234d Mon Sep 17 00:00:00 2001 From: Mike Keen Date: Wed, 22 Dec 2021 22:07:59 -0500 Subject: [PATCH] feat: project pagination --- src/controllers/project.controller.js | 14 +++++++++----- src/models/projects/projects.mock.js | 2 +- src/models/projects/projects.model.js | 14 ++++++++++---- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/controllers/project.controller.js b/src/controllers/project.controller.js index ba828720..336adccc 100644 --- a/src/controllers/project.controller.js +++ b/src/controllers/project.controller.js @@ -11,6 +11,8 @@ import { RelatedProject, } from '../models'; +import { paginationParams } from './helpers'; + export const create = async (req, res) => { // When creating new projects assign a uuid to is so // multiple organizations will always have unique ids @@ -30,19 +32,20 @@ export const create = async (req, res) => { }; export const findAll = async (req, res) => { + const { page, limit, search, orgUid } = req.query; + if (req.query.useMock) { - res.json(ProjectMock.findAll()); + res.json(ProjectMock.findAll({ ...paginationParams(page, limit) })); return; } const dialect = sequelize.getDialect(); - const { search, orgUid } = req.query; if (search) { if (dialect === 'sqlite') { - res.json(await Project.findAllSqliteFts(search, orgUid)); + res.json(await Project.findAllSqliteFts(search, orgUid, paginationParams(page, limit))); } else if (dialect === 'mysql') { - res.json(await Project.findAllMySQLFts(search, orgUid)); + res.json(await Project.findAllMySQLFts(search, orgUid, paginationParams(page, limit))); } } else { const query = { @@ -60,7 +63,8 @@ export const findAll = async (req, res) => { orgUid, }; } - res.json(await Project.findAll(query)); + + res.json(await Project.findAll({ ...query, ...paginationParams(page, limit) })); } }; diff --git a/src/models/projects/projects.mock.js b/src/models/projects/projects.mock.js index 35b95896..4902024a 100644 --- a/src/models/projects/projects.mock.js +++ b/src/models/projects/projects.mock.js @@ -1,7 +1,7 @@ import stub from './projects.stub.json'; export const ProjectMock = { - findAll: () => stub, + findAll: ({ limit, offset }) => stub.slice(offset * limit, (offset + 1) * limit), findOne: (id) => { return stub.find((record) => record.id == id); }, diff --git a/src/models/projects/projects.model.js b/src/models/projects/projects.model.js index 7abc46a0..15e15618 100644 --- a/src/models/projects/projects.model.js +++ b/src/models/projects/projects.model.js @@ -23,7 +23,8 @@ class Project extends Model { Project.hasMany(ProjectLocation); } - static findAllMySQLFts(searchStr, orgUid) { + static findAllMySQLFts(searchStr, orgUid, pagination) { + const { offset, limit } = pagination; let sql = ` SELECT * FROM projects WHERE MATCH ( warehouseProjectId, @@ -50,28 +51,33 @@ class Project extends Model { sql = `${sql} AND orgUid = :orgUid`; } - sql = `${sql} ORDER BY relevance DESC`; + sql = `${sql} ORDER BY relevance DESC LIMIT :limit OFFSET :offset`; return sequelize.query(sql, { model: Project, replacements: { search: searchStr, orgUid }, mapToModel: true, // pass true here if you have any mapped fields + offset, + limit, }); } - static findAllSqliteFts(searchStr, orgUid) { + static findAllSqliteFts(searchStr, orgUid, pagination ) { + const { offset, limit } = pagination; let sql = `SELECT * FROM projects_fts WHERE projects_fts MATCH :search`; if (orgUid) { sql = `${sql} AND orgUid = :orgUid`; } - sql = `${sql} ORDER BY rank DESC`; + sql = `${sql} ORDER BY rank DESC LIMIT :limit OFFSET :offset`; return sequelize.query(sql, { model: Project, replacements: { search: `${searchStr}*`, orgUid }, mapToModel: true, // pass true here if you have any mapped fields + offset, + limit, }); } }