0.0.3 • Published 5 years ago
jsonquerybuilder
Licence
ISC
Version
0.0.3
Deps
0
Size
23 kB
Vulns
0
Weekly
0
JSON Query Builder
jsonQueryBuilder is a javascript project which makes it easier to write long and complex JSON queries
Installation
Use in Node.js
npm install jsonquerybuilder
Usage
let { Query } = require('jsonQueryBuilder');
let query = new Query();
query.setProperty(['query', 'sort'], { 'name': 1 });
query.addProperty(['query', 'bool', 'must'], { "match": { "name": "Joe" } } });
console.log(query.build());
// Prints -> {"query":{"sort":{"name":1},"bool":{"must":[{"match":{"name":"Joe"}}]}}}
Methods
Query
addProperty(path, query, { multiple, overwrite, rank })
Description
Adds the specified query in an array.
Parameters
path array of string representing the path where the query will get added onquery the query which gets added onto the pathoptions Optional fields
multiple(Boolean) For adding multiple value to the specified path. Note,queryparameter must be an array if this is set to true.overwrite(Boolean) If set to true, the path would be overwritten (the previous value would be replaced).rank(Number) This decides ordering of thequerybeing added. Queries with lower rank will get placed in the starting of the array. If it's not set, the query will get appended at the end of the array.
setProperty(path, query, { overwrite })
Description
Sets the value of the path to the specified query
Parameters
path array of string representing the path where the query will be setquery the query to be setoptions Optional fields
overwrite(Boolean) If set to true, the path would be overwritten (the previous value would be replaced).
deleteProperty(path)
Description
Deletes the value at the specified path.
Parameters
path array of string representing the path from where the value will be deleted
build()
Description
Builds and returns the JSON query.