1.0.1 • Published 6 years ago

rest-filter-to-sql v1.0.1

Weekly downloads
3
License
MIT
Repository
github
Last release
6 years ago

rest-filter-to-sql

Simple conversion from REST filter operators to SQL WHERE clauses

Installation

$ npm install rest-filter-to-sql

Usage

const filter = require('rest-filter-to-sql');

let obj = {
  name: 'Bill Murray',
  rated: { $gt: 90},
  movies: { $in: ['Groundhog Day', 'Lost In Translation']}
}

let sql = filter.parse(obj)
console.log(sql)

// WHERE name = 'Bill Murray'
// AND rated > 90
// AND movies IN ('Groundhog Day','Lost In Translation')

Supported operators

Logical:

These expect an array as the object value.

  • $or OR
  • $and AND
  • $in IN
  • $not NOT IN

Comparison:

These expect a number as the object value.

  • $gt >
  • $gte >=
  • $lt <
  • $lte <=

String:

These expect a string as the object value.

  • $contains
  • $containsi (case insensitive)
  • $startsWith
  • $startsWithi (case insensitive)
  • $endsWith
  • $endsWithi (case insensitive)