sequelize-querystring

Convert the request query string into sequelize compatible where and sort clauses.

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
sequelize-querystring
0.10.05 years ago7 years agoMinified + gzip package size for sequelize-querystring in KB

Readme

Node Sequelize query string
JavaScript Style Guide License
The aim of this module it to convert the request query string into sequelize compatible where and sort clauses.
The reason for the existence of this module is resumed in one single word: minimalist.

Features so far:

  • Convert filter attribute into where clauses
  • Multiple filter values (comma separated)
  • Nested properties
  • Supports almost every operator
Tested operators: values: gt, gte, lt, lte, eq, ne, not, in, notIn, like, notLike, iLike, notILike arrays: overlap, contains, contained, between, notBetween
  • Convert sort attribute into order clause
  • Multiple sort values (comma separated)

Usage

Example in a hapijs compatible request handler:
const sqs = require('sequelize-querystring')
const ArticlesModel = require('./models/articles')

// list articles
exports.list = (req, res) => {
  return ArticlesModel.findAndCountAll({
    offset: req.query.offset || 0,
    limit: req.query.limit || 10,
    where: req.query.filter ? sqs.find(req.query.filter) : {},
    order: req.query.sort ? sqs.sort(req.query.sort) : []
  })
  .then((results) => {
    results.rows = results.rows.map((o) => { return o.get() })
    res(results)
  })
  .catch((err) => {
    log.error(err)
    res(err).code(500)
  })
}

The compatible query string to filter and sort is like:
http://localhost:3000/v1/articles?filter=author eq joaquim&sort=createdAt desc

The handler has pagination and by default it replies the first 10 items. To get the next page you could simply request:
http://localhost:3000/v1/articles?filter=author eq joaquim&limit=10&offset=10

The filter is also compatible with multiple values operator 'or', 'in', 'notIn', 'overlap', 'contains', 'contained', 'between', 'notBetween' (Every values are separated with a space):
http://localhost:3000/v1/articles?filter=date between 1995 2020

Contribute

If you want to contribute your're welcome. Keep in mind:
  • Test your fixes or improvements (npm test should pass)
  • Keep it simple