0.0.3 • Published 3 years ago

jsonquerybuilder v0.0.3

Weekly downloads
-
License
ISC
Repository
-
Last release
3 years ago

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 on
query the query which gets added onto the path
options Optional fields 1. multiple (Boolean) For adding multiple value to the specified path. Note, query parameter must be an array if this is set to true. 2. overwrite (Boolean) If set to true, the path would be overwritten (the previous value would be replaced). 3. rank (Number) This decides ordering of the query being 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 set
query the query to be set
options Optional fields 1. 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.

License

MIT