mongo-query-builder-chain
Advanced tools
Comparing version 1.0.5 to 1.0.6
@@ -18,2 +18,3 @@ const mongoQBModule = require('./module'); | ||
}); | ||
console.log(mongoQB.body); | ||
@@ -27,2 +28,5 @@ console.log(mongoQB.addToQuery(['field1'], mongoQB.$ne("-1")) | ||
.projectionRemove('projection_field2') | ||
.$or([], [mongoQB.clause('clause_field1', 'clause_value1'), mongoQB.clause('clause_field2', 'clause_value2')]) | ||
.$orRemoveFrom([], 'clause_field1') | ||
.$orClear() | ||
.sort("field0", 1) | ||
@@ -29,0 +33,0 @@ .sort("field1.field11.field111", 1) |
109
module.js
@@ -136,2 +136,17 @@ var helpers = require('./helpers'); | ||
clause(key, value) { | ||
if (!helpers.isOfPrimitiveType(['string'], key)) { | ||
throw "Argument key must be string!"; | ||
} | ||
if (!helpers.isOfPrimitiveType(['number', 'string'], value)) { | ||
throw "Argument value must be string or number!"; | ||
} | ||
let clause = {}; | ||
clause[key] = value; | ||
return clause; | ||
} | ||
removeFromQuery(keyPath = []) { | ||
@@ -264,6 +279,6 @@ if (Array.isArray(keyPath)) { | ||
if (!this.body.query.$text) { | ||
this.body.query['$text'] = {}; | ||
this.body.query.$text = {}; | ||
} | ||
this.body.query.$text['$language'] = value; | ||
this.body.query.$text.$language = value; | ||
return this.body.query.$text; | ||
@@ -290,6 +305,6 @@ } else { | ||
if (!this.body.query.$text) { | ||
this.body.query['$text'] = {}; | ||
this.body.query.$text = {}; | ||
} | ||
this.body.query.$text['$search'] = value; | ||
this.body.query.$text.$search = value; | ||
return this.body.query.$text; | ||
@@ -324,5 +339,87 @@ } else { | ||
// Logical query operators | ||
$or(keyPath = [], clauses = []) { | ||
if (!Array.isArray(keyPath) || !Array.isArray(clauses)) { | ||
throw "Each of the arguments must be an array!"; | ||
} | ||
let parent = this.body.query; | ||
keyPath.forEach((key, index) => { | ||
if (helpers.isOfPrimitiveType(['string'], key)) { | ||
if (!parent[key]) { | ||
parent[key] = {}; | ||
} | ||
if (index < keyPath.length - 1) { | ||
parent = parent[key]; | ||
} else { | ||
parent[key] = value; | ||
} | ||
} else { | ||
throw "Argument keyPath contains a non string element!"; | ||
} | ||
}); | ||
if (!parent.$or) { | ||
parent.$or = []; | ||
} | ||
parent.$or = parent.$or.concat(...clauses); | ||
return this; | ||
} | ||
$orClear(keyPath = []) { | ||
if (!Array.isArray(keyPath)) { | ||
throw "Argument keyPath must be an array!"; | ||
} | ||
let parent = this.body.query; | ||
keyPath.forEach(key => { | ||
if (helpers.isOfPrimitiveType(['string'], key)) { | ||
if (parent[key] === null) { | ||
return; | ||
} | ||
parent = parent[key]; | ||
} else { | ||
throw "Argument keyPath contains a non string element!"; | ||
} | ||
}); | ||
delete parent.$or; | ||
return this; | ||
} | ||
$orRemoveFrom(keyPath = [], clauseKey) { | ||
if (!Array.isArray(keyPath)) { | ||
throw "Argument keyPath must be an array!"; | ||
} | ||
if (!helpers.isOfPrimitiveType(['string'], clauseKey)) { | ||
throw "Argument clauseKey must be string!"; | ||
} | ||
let parent = this.body.query; | ||
keyPath.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 (parent.$or) { | ||
parent.$or = parent.$or.filter(orClause => !Object.keys(orClause).includes(clauseKey)); | ||
} | ||
if (!parent.$or.length) { | ||
delete parent.$or; | ||
} | ||
return this; | ||
} | ||
// Projection | ||
projectionAdd(keys, include = 1) { | ||
projectionAdd(keys = [], include = 1) { | ||
if (include === 0 || include === 1) { | ||
@@ -333,3 +430,3 @@ if (Array.isArray(keys)) { | ||
if (!this.body.$projection) { | ||
this.body['$projection'] = {}; | ||
this.body.$projection = {}; | ||
} | ||
@@ -336,0 +433,0 @@ |
@@ -20,3 +20,3 @@ { | ||
"type": "module", | ||
"version": "1.0.5" | ||
"version": "1.0.6" | ||
} |
@@ -39,2 +39,8 @@ # mongo-query-builder-chain | ||
Create a clause with a key _clause_field_ with a value _clause_value_ | ||
```javascript | ||
mongoQuery.clause('clause_field', 'clause_value') | ||
``` | ||
It returns an object with one key and its value. | ||
Remove the key _field1_ with its associated value if any | ||
@@ -56,3 +62,3 @@ ```javascript | ||
```javascript | ||
mongoQB.$eq(value) | ||
mongoQuery.$eq(value) | ||
``` | ||
@@ -63,3 +69,3 @@ It returns the object containing only the $eq operator as key and its value provided by the argument. | ||
```javascript | ||
mongoQB.$in(array) | ||
mongoQuery.$in(array) | ||
``` | ||
@@ -76,6 +82,16 @@ It returns the object containing only the **$in** operator as key and its value as array provided by the argument. | ||
```javascript | ||
mongoQB.$exists(value) | ||
mongoQuery.$exists(value) | ||
``` | ||
It returns the object containing only the **$exists** operator as key and its value provided by the argument. The default value is true. | ||
#### Date operators | ||
Partial support fo the current mongodb query date operators. | ||
Adds a date match by a specified _value_ | ||
```javascript | ||
mongoQuery.$date(value) | ||
``` | ||
It returns the object containing only the **$date** operator as key and its value provided by the argument. the provided argument must be of string type. | ||
#### Evaluation operators | ||
@@ -85,11 +101,11 @@ | ||
Adds a text search by a specified _value_ | ||
Add a text search by a specified _value_ | ||
```javascript | ||
mongoQB.$search(value) | ||
mongoQuery.$search(value) | ||
``` | ||
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. | ||
Removes the current text search | ||
Remove the current text search | ||
```javascript | ||
mongoQB.$searchRemove() | ||
mongoQuery.$searchRemove() | ||
``` | ||
@@ -100,22 +116,34 @@ 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. | ||
```javascript | ||
mongoQB.$language(value) | ||
mongoQuery.$language(value) | ||
``` | ||
It returns the object containing only the **$text** operator and its value with the **$language** operator as key and its value provided by the argument. If the **$text** key does not exist in the query, it is added automatically. | ||
Removes the current text language | ||
Remove the current text language | ||
```javascript | ||
mongoQB.$languageRemove() | ||
mongoQuery.$languageRemove() | ||
``` | ||
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. | ||
#### Date operators | ||
#### Logical operators | ||
Partial support fo the current mongodb query date operators. | ||
Partial support for the current mongodb query [logical operators](https://docs.mongodb.com/manual/reference/operator/query-logical/). | ||
Adds a date match by a specified _value_ | ||
Add an **$or** operator on a location _keyPath_ as an array of fields, on _clause1_ and _clause2_ | ||
```javascript | ||
mongoQB.$date(value) | ||
mongoQuery.$or(keyPath, [clause1, clause2]) | ||
``` | ||
It returns the object containing only the **$date** operator as key and its value provided by the argument. the provided argument must be of string type. | ||
It returns the current state of the _mongoQuery_ object containing the **$or** operator under the keyPath. | ||
Remove a clause from the **$or** operator under the location _keyPath_ | ||
```javascript | ||
mongoQuery.$orRemoveFrom(keyPath, 'clause_field1') | ||
``` | ||
If the clause with the key _'clause_field1'_ is the last remaining in the **$or** operator, the **$or** operator will be removed automatically from the query. It returns the current state of the _mongoQuery_ object. | ||
Remove the **$or** operator under the location _keyPath_ | ||
```javascript | ||
mongoQuery.$orClear(keyPath) | ||
``` | ||
If not specified, the default value of the _keyPath_ is an empty array. It returns the current state of the _mongoQuery_ object. | ||
## Sorting | ||
@@ -137,2 +165,4 @@ | ||
Partial support for the current mongodb query [projection](https://docs.mongodb.com/manual/reference/glossary/#term-projection). | ||
Add a projection criteria with keys _field1_, _field2_, _field3_ intended to be excluded | ||
@@ -150,3 +180,3 @@ ```javascript | ||
Removes a projection criteria with key _field4_ | ||
Remove a projection criteria with key _field4_ | ||
```javascript | ||
@@ -153,0 +183,0 @@ mongoQuery.projectionRemove('field4'); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
21052
446
185