Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/krsbx/sequelize-fqp
Filter Query Parser for Sequelize ORM
https://github.com/krsbx/sequelize-fqp
filter javascript nodejs query sequelize typescript
Last synced: 25 days ago
JSON representation
Filter Query Parser for Sequelize ORM
- Host: GitHub
- URL: https://github.com/krsbx/sequelize-fqp
- Owner: krsbx
- License: mit
- Created: 2022-07-20T01:22:27.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-11-24T13:41:02.000Z (12 months ago)
- Last Synced: 2024-04-25T06:01:46.653Z (6 months ago)
- Topics: filter, javascript, nodejs, query, sequelize, typescript
- Language: TypeScript
- Homepage:
- Size: 770 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# Sequelize Filter Query Parser
Filter Query Parser for Sequelize ORM
Using [filter-query-parser](https://github.com/VJD7/filter-query-parser/) as the base
# How To Use
1. Create a new Middleware that can be use for all routes e.g. `parserMw`
- Javascript
```js
// Files : parser.jsconst sequelizeFQP = require('sequelize-filter-query-parser');
exports.queryParserMw = (req, res, next) => {
req.filterQueryParams = req.query.filters
? sequelizeFQP(req.query.filters)
: {};
next();
};
```- Typescript
```ts
// Files : parser.jsimport sequelizeFQP from 'sequelize-filter-query-parser';
export const queryParserMw = (req, res, next) => {
req.filterQueryParams = req.query.filters
? sequelizeFQP(req.query.filters)
: {};
next();
};
```2. Use FQP Results in baseRepository
- Javascript
```js
// Files : baseRepository.jsexports.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
```ts
// Files : baseRepository.tsexport 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;
}/* {...} */
};
```