Skip to content

Commit

Permalink
Feat: proper pagination responses
Browse files Browse the repository at this point in the history
  • Loading branch information
mkeen committed Dec 28, 2021
1 parent 36009e8 commit 5fb4019
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 31 deletions.
12 changes: 12 additions & 0 deletions src/controllers/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,16 @@ export const paginationParams = (page, limit) => {
limit: limit ? limit : 15,
offset: page ? page - 1 : 1,
}
}

export const optionallyPaginatedResponse = ({count, rows}, page, limit) => {
if (page) {
return {
page,
pageCount: Math.max(count / (limit || 15)),
data: rows,
}
} else {
return rows;
}
}
19 changes: 12 additions & 7 deletions src/controllers/project.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
RelatedProject,
} from '../models';

import { paginationParams } from './helpers';
import { optionallyPaginatedResponse, paginationParams } from "./helpers";

export const create = async (req, res) => {
// When creating new projects assign a uuid to is so
Expand Down Expand Up @@ -41,13 +41,15 @@ export const findAll = async (req, res) => {

const dialect = sequelize.getDialect();

let results;

if (search) {
if (dialect === 'sqlite') {
res.json(await Project.findAllSqliteFts(search, orgUid, paginationParams(page, limit)));
results = await Project.findAllSqliteFts(search, orgUid, paginationParams(page, limit));
} else if (dialect === 'mysql') {
res.json(await Project.findAllMySQLFts(search, orgUid, paginationParams(page, limit)));
results = await Project.findAllMySQLFts(search, orgUid, paginationParams(page, limit));
}
return;
return res.json(optionallyPaginatedResponse(results, page, limit));
}

if (req.query.onlyEssentialColumns) {
Expand All @@ -69,8 +71,11 @@ export const findAll = async (req, res) => {
};
}

res.json(await Project.findAll({ ...query, ...paginationParams(page, limit) }));
return;
return res.json(optionallyPaginatedResponse(
await Project.findAndCountAll({ ...query, ...paginationParams(page, limit) }),
page,
limit,
));
}

const query = {
Expand All @@ -83,7 +88,7 @@ export const findAll = async (req, res) => {
],
};

res.json(await Project.findAll(query));
return await Project.findAll(query);
};

export const findOne = async (req, res) => {
Expand Down
30 changes: 14 additions & 16 deletions src/controllers/units.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { uuid as uuidv4 } from 'uuidv4';
import { Staging, UnitMock, Unit, Qualification, Vintage } from '../models';
import { paginationParams } from './helpers';
import { optionallyPaginatedResponse, paginationParams } from "./helpers";

export const create = async (req, res) => {
try {
Expand Down Expand Up @@ -37,24 +38,21 @@ export const findAll = async (req, res) => {
}

if (req.query.onlyEssentialColumns) {
res.json(
await Unit.findAll({
attributes: [
'orgUid',
'unitLink',
'registry',
'unitType',
'unitCount',
'unitStatus',
'unitStatusDate',
],
}),
);
return;
return res.json(optionallyPaginatedResponse(await Unit.findAndCountAll({
attributes: [
'orgUid',
'unitLink',
'registry',
'unitType',
'unitCount',
'unitStatus',
'unitStatusDate',
],
}), page, limit));
}

res.json(
await Unit.findAll({
optionallyPaginatedResponse(await Unit.findAndCountAll({
include: [
{
model: Qualification,
Expand All @@ -63,7 +61,7 @@ export const findAll = async (req, res) => {
Vintage,
],
...paginationParams(page, limit),
}),
}), page, limit),
);
};

Expand Down
22 changes: 14 additions & 8 deletions src/models/projects/projects.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { Rating } from './../ratings';
import { CoBenefit } from './../co-benefits';

import ModelTypes from './projects.modeltypes.cjs';
import { optionallyPaginatedResponse } from "../../controllers/helpers.js";

class Project extends Model {
static associate() {
Expand All @@ -23,7 +24,7 @@ class Project extends Model {
Project.hasMany(ProjectLocation);
}

static findAllMySQLFts(searchStr, orgUid, pagination) {
static async findAllMySQLFts(searchStr, orgUid, pagination) {
const { offset, limit } = pagination;
let sql = `
SELECT * FROM projects WHERE MATCH (
Expand Down Expand Up @@ -53,13 +54,18 @@ class Project extends Model {

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,
});
const count = await Project.count()

return {
count,
rows: await 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, pagination ) {
Expand Down

0 comments on commit 5fb4019

Please sign in to comment.