Skip to content

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

License

Notifications You must be signed in to change notification settings

toffick/node-sequelize-querystring

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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
  • 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

Contribute

If you want to contribute your're welcome. Keep in mind:

  • Test your fixes or improvements (npm test should pass)
  • Keep it simple

About

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

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%