Comparing version 5.5.3 to 5.6.0
@@ -5,2 +5,12 @@ # Change Log | ||
<a name="5.6.0"></a> | ||
# [5.6.0](https://github.com/dmfay/massive-js/compare/v5.5.3...v5.6.0) (2018-11-27) | ||
### Features | ||
* nested conjunctions in criteria objects with 'and' key ([#651](https://github.com/dmfay/massive-js/issues/651)) ([7aebccc](https://github.com/dmfay/massive-js/commit/7aebccc)) | ||
<a name="5.5.3"></a> | ||
@@ -7,0 +17,0 @@ ## [5.5.3](https://github.com/dmfay/massive-js/compare/v5.5.1...v5.5.3) (2018-11-10) |
@@ -5,8 +5,8 @@ # Criteria Objects | ||
A criteria object is a "plain old JavaScript object" where keys represent the fields to search and values are prepared statement parameters. The key `or` is special and takes an array of nested criteria objects, at least one of which must be fully matched for a record to be included in the resultset. `or` may be nested recursively at any depth. | ||
A criteria object is a "plain old JavaScript object" where keys represent the fields to search and values are prepared statement parameters. The keys `or` and `and` are special and take an array of nested criteria objects. At least one of the nested criteria must be fully matched for a record to be included in the resultset with the former, and all for the latter. `or` and `and` may be nested recursively at any depth. | ||
```javascript | ||
// this will search for all active records where the name | ||
// contains 'homepage' or the JSON 'stats' field shows | ||
// more than 5 runs | ||
// this will search for all active records where the | ||
// name contains 'homepage' or the JSON 'stats' field | ||
// shows more than 5 runs | ||
@@ -13,0 +13,0 @@ const criteria = { |
@@ -400,3 +400,3 @@ 'use strict'; | ||
return resolve(classicStream.pipe(transformStream)); | ||
resolve(classicStream.pipe(transformStream)); | ||
}) | ||
@@ -403,0 +403,0 @@ .catch(reject)); |
@@ -130,2 +130,31 @@ 'use strict'; | ||
/** | ||
* Build an inner conjunction (logical OR). | ||
* | ||
* @param {Array} conditions - An array of nested criteria objects. Individual | ||
* objects will be arrayified (so an 'and' key can work with a single object). | ||
* @param {Number} offset - Offset prepared statement parameter ordinals. | ||
* @param {Function} generator - Generator function to use to build SQL | ||
* predicates. | ||
* @return {Object} A nested conjunction node. | ||
*/ | ||
const generateInnerConjunction = (conditions, offset, generator) => { | ||
return _.reduce(conditions, (conjunction, subconditions) => { | ||
// each member of an 'and' array is itself a conjunction, so build it and | ||
// integrate it into the conjunction predicate structure | ||
/* eslint-disable no-use-before-define */ | ||
const innerConjunction = generateConjunction(subconditions, conjunction.offset + conjunction.params.length, generator); | ||
/* eslint-enable no-use-before-define */ | ||
conjunction.params = conjunction.params.concat(innerConjunction.params); | ||
conjunction.predicates.push(`(${innerConjunction.predicates.join(' AND ')})`); | ||
return conjunction; | ||
}, { | ||
params: [], | ||
predicates: [], | ||
offset | ||
}); | ||
}; | ||
/** | ||
* Build a conjunction (logical AND). | ||
@@ -148,2 +177,9 @@ * | ||
return conjunction; | ||
} else if (key === 'and') { | ||
const inner_conjunction = generateInnerConjunction(value, conjunction.offset + conjunction.params.length, generator); | ||
conjunction.params = conjunction.params.concat(inner_conjunction.params); | ||
conjunction.predicates.push(`(${inner_conjunction.predicates.join(' AND ')})`); | ||
return conjunction; | ||
} | ||
@@ -150,0 +186,0 @@ |
{ | ||
"name": "massive", | ||
"version": "5.5.3", | ||
"version": "5.6.0", | ||
"description": "A small query tool for Postgres that embraces json and makes life simpler", | ||
@@ -5,0 +5,0 @@ "homepage": "https://dmfay.github.io/massive-js/", |
213801
2426