@digital-envision/sequelize-fqp
v0.1.5
Published
Filter Query Parser for Sequelize ORM
Downloads
7
Readme
Sequelize Filter Query Parser
Filter Query Parser for Sequelize ORM
Using filter-query-parser as the base
How To Use
- Create a new Middleware that can be use for all routes e.g.
parserMw
- Javascript
// Files : parser.js
const sequelizeFQP = require('@digital-envision/sequelize-fqp');
exports.queryParserMw = (req, res, next) => {
req.filterQueryParams = req.query.filters
? sequelizeFQP(req.query.filters)
: {};
next();
};
- Typescript
// Files : parser.js
import sequelizeFQP from '@digital-envision/sequelize-fqp';
export const queryParserMw = (req, res, next) => {
req.filterQueryParams = req.query.filters
? sequelizeFQP(req.query.filters)
: {};
next();
};
- Use the FQP Results in baseRepository
- Javascript
// Files : baseRepository.js
exports.findAll =
(model) =>
(conditions, filterQueryParams = {}, options = {}) => {
/* {...} */
const rules = [{ ...filterQueryParams }];
/* {...} */
const where = { ...conditions };
if (where[Op.and]) {
where[Op.and] = [...where[Op.and], ...rules];
} else {
where[Op.and] = rules;
}
/* {...} */
};
- Typescript
// Files : baseRepository.ts
export const findAll =
(model) =>
(conditions, filterQueryParams = {}, options = {}) => {
/* {...} */
const rules = [{ ...filterQueryParams }];
/* {...} */
const where = { ...conditions };
if (where[Op.and]) {
where[Op.and] = [...where[Op.and], ...rules];
} else {
where[Op.and] = rules;
}
/* {...} */
};