@abskmj/query

1.0.0 • Public • Published

Query

It is a npm module that converts complex json with arrays and nested objects into url parameters or query string. It can also parse such query string back to json.

Example

const query = require('@abskmj/query');

// query to get a list of 10 people between age 17 and 66, sorted by their age 

let filters = {
    where: {
        age: { $gt: 17, $lt: 66 },
    },
    options: {
        limit: 10,
        sort: { age: -1 }
    }
}

const queryString = query.stringify(filters);

console.log(queryString);

/*
console:
where[age][$gt]=17&where[age][$lt]=66&options[limit]=10&options[sort][age]=-1
*/


const json = query.parse(queryString);

console.log(JSON.stringify(json, null, 2));

/*
console:
{
  "where": {
    "age": {
      "$gt": 17,
      "$lt": 66
    }
  },
  "options": {
    "limit": 10,
    "sort": {
      "age": -1
    }
  }
}
*/

Readme

Keywords

none

Package Sidebar

Install

npm i @abskmj/query

Weekly Downloads

126

Version

1.0.0

License

MIT

Unpacked Size

7.74 kB

Total Files

6

Last publish

Collaborators

  • abskmj