mongo-query-builder-chain
Advanced tools
Comparing version 1.0.6 to 1.0.7
@@ -23,7 +23,7 @@ const mongoQBModule = require('./module'); | ||
.addToQuery(['field3'], 0) | ||
.addToQuery([], mongoQB.$language()) | ||
.addToQuery([], mongoQB.$language(), true) | ||
.projectionAdd(['projection_field2', 'projection_field3', 'projection_field4']) | ||
.projectionAdd(['projection_field1', 'projection_field5'], 0) | ||
.projectionRemove('projection_field2') | ||
.$or([], [mongoQB.clause('clause_field1', 'clause_value1'), mongoQB.clause('clause_field2', 'clause_value2')]) | ||
.$or([], [mongoQB.clause('clause_field1', 'clause_value1'), mongoQB.$language(), mongoQB.$search('search_value')]) | ||
.$orRemoveFrom([], 'clause_field1') | ||
@@ -30,0 +30,0 @@ .$orClear() |
103
module.js
@@ -112,3 +112,3 @@ var helpers = require('./helpers'); | ||
addToQuery(keyPath = [], value) { | ||
addToQuery(keyPath = [], value, merge = false) { | ||
if (Array.isArray(keyPath)) { | ||
@@ -125,3 +125,5 @@ let parent = this.body.query; | ||
} else { | ||
parent[key] = value; | ||
if (!merge) { | ||
parent[key] = value; | ||
} | ||
} | ||
@@ -132,2 +134,8 @@ } else { | ||
}); | ||
if (merge) { | ||
if (!helpers.isOfPrimitiveType(['object'], value)) { | ||
throw "Argument value must be object!"; | ||
} | ||
parent = Object.assign(parent, value); | ||
} | ||
return this; | ||
@@ -279,21 +287,32 @@ } else { | ||
$language(value = 'none') { | ||
if (helpers.isOfPrimitiveType(['string'], value)) { | ||
if (!this.body.query.$text) { | ||
this.body.query.$text = {}; | ||
} | ||
this.body.query.$text.$language = value; | ||
return this.body.query.$text; | ||
} else { | ||
if (!helpers.isOfPrimitiveType(['string'], value)) { | ||
throw "Argument value must be string!"; | ||
} | ||
let textObject = { | ||
$text: {} | ||
}; | ||
textObject.$text.$language = value; | ||
return textObject; | ||
} | ||
$languageRemove() { | ||
if (this.body.query.$text) { | ||
delete this.body.query.$text.$language; | ||
$languageRemove(textClauseLocation = []) { | ||
if (!Array.isArray(textClauseLocation)) { | ||
throw "Argument keyPath must be an array!"; | ||
} | ||
let parent = this.body.query; | ||
textClauseLocation.forEach(key => { | ||
if (helpers.isOfPrimitiveType(['string'], key)) { | ||
if (parent[key] === null) { | ||
return; | ||
} | ||
parent = parent[key]; | ||
} else { | ||
throw "Argument keyPath contains a non string element!"; | ||
} | ||
}); | ||
if (!Object.keys(this.body.query.$text).length) { | ||
delete this.body.query.$text; | ||
} | ||
let textClause = parent.$text; | ||
if (textClause) { | ||
delete textClause.$language; | ||
} | ||
@@ -305,21 +324,32 @@ | ||
$search(value) { | ||
if (helpers.isOfPrimitiveType(['string'], value)) { | ||
if (!this.body.query.$text) { | ||
this.body.query.$text = {}; | ||
} | ||
this.body.query.$text.$search = value; | ||
return this.body.query.$text; | ||
} else { | ||
if (!helpers.isOfPrimitiveType(['string'], value)) { | ||
throw "Argument value must be string!"; | ||
} | ||
let textObject = { | ||
$text: {} | ||
}; | ||
textObject.$text.$search = value; | ||
return textObject; | ||
} | ||
$searchRemove() { | ||
if (this.body.query.$text) { | ||
delete this.body.query.$text.$search; | ||
$searchRemove(textClauseLocation = []) { | ||
if (!Array.isArray(textClauseLocation)) { | ||
throw "Argument keyPath must be an array!"; | ||
} | ||
let parent = this.body.query; | ||
textClauseLocation.forEach(key => { | ||
if (helpers.isOfPrimitiveType(['string'], key)) { | ||
if (parent[key] === null) { | ||
return; | ||
} | ||
parent = parent[key]; | ||
} else { | ||
throw "Argument keyPath contains a non string element!"; | ||
} | ||
}); | ||
if (!Object.keys(this.body.query.$text).length) { | ||
delete this.body.query.$text; | ||
} | ||
let textClause = parent.$text; | ||
if (textClause) { | ||
delete textClause.$search; | ||
} | ||
@@ -367,3 +397,14 @@ | ||
} | ||
parent.$or = parent.$or.concat(...clauses); | ||
clauses.forEach(clause => { | ||
let clausePrimaryKeys = Object.keys(clause); | ||
let orClauseMatched = parent.$or.find(orClause => JSON.stringify(Object.keys(orClause)) === JSON.stringify(clausePrimaryKeys)); | ||
if (orClauseMatched) { | ||
orClauseMatched[[clausePrimaryKeys[0]]] = Object.assign(orClauseMatched[clausePrimaryKeys[0]], clause[clausePrimaryKeys[0]]); | ||
parent.$or = parent.$or.filter(orClause => JSON.stringify(Object.keys(orClause)) !== JSON.stringify(clausePrimaryKeys)); | ||
parent.$or.push(orClauseMatched); | ||
} else { | ||
parent.$or.push(clause); | ||
} | ||
}); | ||
return this; | ||
@@ -370,0 +411,0 @@ } |
@@ -20,3 +20,3 @@ { | ||
"type": "module", | ||
"version": "1.0.6" | ||
"version": "1.0.7" | ||
} |
@@ -33,5 +33,5 @@ # mongo-query-builder-chain | ||
Add a key _field11_ under _field1_ with a value to an existing query | ||
Add a key _field11_ under _field1_ with a value to an existing query and ability to _merge_ if a value with the same key already exists (in case _value_ is of object type) | ||
```javascript | ||
mongoQuery.addToQuery(['field1', 'field11'], value); | ||
mongoQuery.addToQuery(['field1', 'field11'], value, merge); | ||
``` | ||
@@ -102,11 +102,11 @@ If the key already exists, only its value will be changed. The value can be primitive, object with containing the reserved supported operators. The location of the key inside the query is determined by the provided array with the value at the beginning denoting the root location and the value at the end denoting the exact name of the key. If any portion of the path does not exist in the current body of the query, it is automatically created. It returns the current state of the _mongoQuery_ object. | ||
``` | ||
It returns the object containing only the **$text** operator and its value - the **$search** operator as key and its value provided by the argument. If the **$text** key does not exist in the query, it is added automatically. | ||
It returns the object containing the **$text** operator and its value - the **$search** operator as key and its value provided by the argument. | ||
Remove the current text search | ||
Remove the current text search on the _keyPath_ location | ||
```javascript | ||
mongoQuery.$searchRemove() | ||
mongoQuery.$searchRemove(keyPath) | ||
``` | ||
If the **$search** key is the last remaining in the **$text** operator, the **$text** key will be removed automatically. It returns the current state of the _mongoQuery_ object. | ||
Adds a text language by a specified _value_ (the default _value_ is "none") | ||
Add a text language by a specified _value_ (the default _value_ is "none") | ||
```javascript | ||
@@ -117,5 +117,5 @@ mongoQuery.$language(value) | ||
Remove the current text language | ||
Remove the current text language on the _keyPath_ location | ||
```javascript | ||
mongoQuery.$languageRemove() | ||
mongoQuery.$languageRemove(keyPath) | ||
``` | ||
@@ -122,0 +122,0 @@ If the **$language** key is the last remaining in the **$text** operator, the **$text** key will be removed automatically. It returns the current state of the _mongoQuery_ object. |
22555
486