Skip to content

Filter Query Parser for Sequelize ORM

License

Notifications You must be signed in to change notification settings

krsbx/sequelize-fqp

This branch is up to date with main.

Folders and files

NameName
Last commit message
Last commit date

Latest commit

c2ddfbe · Nov 24, 2023

History

57 Commits
Mar 6, 2023
Jul 20, 2022
Nov 24, 2023
Aug 12, 2022
Jul 20, 2022
Jul 20, 2022
Jul 26, 2022
Nov 24, 2023
Jul 20, 2022
Jul 26, 2022
Jul 26, 2022
Jul 20, 2022
Jul 21, 2022
Nov 24, 2023
Nov 24, 2023
Jul 26, 2022

Repository files navigation

Sequelize Filter Query Parser

Filter Query Parser for Sequelize ORM

Using 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
// Files :  parser.js

const sequelizeFQP = require('sequelize-filter-query-parser');

exports.queryParserMw = (req, res, next) => {
  req.filterQueryParams = req.query.filters
    ? sequelizeFQP(req.query.filters)
    : {};
  next();
};
  • Typescript
// Files :  parser.js

import sequelizeFQP from 'sequelize-filter-query-parser';

export const queryParserMw = (req, res, next) => {
  req.filterQueryParams = req.query.filters
    ? sequelizeFQP(req.query.filters)
    : {};
  next();
};
  1. Use 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;
    }

    /* {...} */
  };