express-parse-query-sequelize

Express.js middleware used to parse http query to Sequelize.js.

Usage no npm install needed!

<script type="module">
  import expressParseQuerySequelize from 'https://cdn.skypack.dev/express-parse-query-sequelize';
</script>

README

Express.js Parse Query To Sequelize.js

Express.js middleware used to parse http query to Sequelize.js.

npm package version number Actions Status License

It uses npm, TypeScript compiler, Jest, ESLint, Prettier, husky, pinst, commitlint. The production files include ES Modules and TypeScript declaration files.

Github Visual Studio Code Windows Mac OS Linux npm Typescript express Jest ESLint Prettier

Installation

npm install express-parse-query-sequelize

Usage

Server

import parserQueryMiddleware from "express-parse-query-sequelize";

app.use(parserQueryMiddleware);
// req.queryParsed contains a content of req.query parsed to sequelize

app.get("/user", (req: Request, res: Response) => {
  UserModel.findAndCountAll(req.queryParsed)
    .then((users) => {
      res.status(200).json(users);
    })
    .catch((err: Error) => res.status(500).json(err));
});

Where

// Where name equals "john" and email equals "john@doe.com"
?eq=name:john,email:john@doe.com

// Where name is not equals "john"
?!eq=name:john

// Where id greater than "1"
?gt=id:1

// Where id less than "10"
?lt=id:10

// Where name like "%john%doe%"
?like=name:*john*doe*

// Where id equals "1" and name like "john"
?and[eq]=id:1&and[like]=name:john

// Where id equals "1" or name like "john"
?or[eq]=id:1&and[like]=name:john

Sorting

// Sort by id in ascending order
?sort_by=id:asc

// Sort by id in descending order
?sort_by=id:desc

// Sort by id in descending order and name in ascending order
?sort_by=id:desc,name:asc

Grouping

// Group by
?group_by=id,name

Fields

?fields=id,name,email

Operators

gt -----------> greater than
gte ----------> greater or equals than
lt -----------> less than
lte ----------> less than or equals
eq -----------> equals
!eq ----------> not equals
like ---------> like
!like --------> not like
between ------> between
!between -----> not between
in -----------> in
!in ----------> not in
and[op] ------> and operator
or[op] -------> or operator

Development

Install dependencies with npm

npm i

Note: make necessary changes in package.json the version of the package.

Test

Test your code with Jest framework:

npm run test

Note: this package uses husky, pinst and commitlint to automatically execute test and lint commit message before every commit.

Build

Build production (distribution) files in your dist folder:

npm run build

It generates ES Modules (in dist/ folder), as well as TypeScript declaration files (in dist/types folder).

publish

Publish the library on the npm:

npm run publishnpm