I want to filter an array of objects based on a query string provided by the user in Node.js. There are packages such as rql or orql that can achieve this, but they seem to have problem with strings containing colon, such as time stamps. The code using rql:
var rql = require("rql/js-array");
var query1 = "time=lt=2018-08-06";
var query2 = "time=lt=2018-08-06T13:45:36.000Z"
var products = [
{price:14.99, rating: 5, time: "2018-08-04T13:45:36.000Z"},
{price:5.99, rating: 3, time: "2018-08-05T13:45:36.000Z"},
{price: 16.5, rating:2, time: "2018-08-06T13:45:36.000Z"}];
console.log(rql.query(query1, {}, products)); // works
console.log(rql.query(query2, {}, products)); // gives error
The error message:
URIError: Unknown converter 2018-08-06T13
at stringToValue (/home/runner/node_modules/rql/parser.js:186:10)
at /home/runner/node_modules/rql/parser.js:99:20
at String.replace (<anonymous>)
at parse (/home/runner/node_modules/rql/parser.js:71:33)
at Object.query (/home/runner/node_modules/rql/js-array.js:358:10)
Similarly in orql:
var orql = require('orql');
var query1 = "time=lt=2018-08-06";
var query2 = "time=lt=2018-08-06T13:45:36.000Z"
var products = [
{price:14.99, rating: 5, time: "2018-08-04T13:45:36.000Z"},
{price:5.99, rating: 3, time: "2018-08-05T13:45:36.000Z"},
{price: 16.5, rating:2, time: "2018-08-06T13:45:36.000Z"}];
console.log(orql(products, query1)); // works
console.log(orql(products, query2)); // gives error
The error message:
TypeError: Cannot read property 'name' of null
at rqlNodeToFunc (/home/runner/node_modules/orql/index.js:351:20)
at Function.rql.compile (/home/runner/node_modules/orql/index.js:65:14)
at rql (/home/runner/node_modules/orql/index.js:48:14)
Any idea how to do query on arrays of objects with timestamps?