Skip to content

Commit

Permalink
feat: project pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
mkeen committed Dec 23, 2021
1 parent 69b8dda commit 679c197
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
14 changes: 9 additions & 5 deletions src/controllers/project.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = {
Expand All @@ -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) }));
}
};

Expand Down
2 changes: 1 addition & 1 deletion src/models/projects/projects.mock.js
Original file line number Diff line number Diff line change
@@ -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);
},
Expand Down
14 changes: 10 additions & 4 deletions src/models/projects/projects.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
});
}
}
Expand Down

0 comments on commit 679c197

Please sign in to comment.