diff --git a/history.md b/history.md index 9cff821..c226af2 100644 --- a/history.md +++ b/history.md @@ -1,3 +1,8 @@ +# v0.2.15 / 2016-03-04 + +* Fixed issue where there was an incompatibility with mquery module in mongoose +* Updated dependencies + # v0.2.14 / 2016-02-22 * Increasing code coverage of unit tests with minor refactors diff --git a/lib/filter.js b/lib/filter.js index 79c8cf9..7095ccf 100644 --- a/lib/filter.js +++ b/lib/filter.js @@ -16,51 +16,101 @@ module.exports = function(mongoose) { optional = options.filters.optional || {}, self = this; - var applyGreaterThan = function (spec, clause) { + var analyzeWhereSpec = function (val) { + if (typeof val === 'string') { + switch (val.toLowerCase()) { + case 'null' : return null; + case 'true' : return true; + case 'false' : return false; + default : + if (!isNaN(val)) { + // val is a number + if (val.indexOf('.') > -1) { + // val is a float + return parseFloat(val); + } else { + return parseInt(val); + } + } + } + } + + return val; + }; + + var applyGreaterThan = function (spec, isOptional) { + if (typeof isOptional === 'undefined') { + isOptional = false; + } + for (var key in spec) { if (spec.hasOwnProperty(key)) { - clause(key).gt(spec[key]); + (isOptional ? self.or(key) : self.where(key)).gt(spec[key]); } } }; - var applyGreaterThanEqual = function (spec, clause) { + var applyGreaterThanEqual = function (spec, isOptional) { + if (typeof isOptional === 'undefined') { + isOptional = false; + } + for (var key in spec) { if (spec.hasOwnProperty(key)) { - clause(key).gte(spec[key]); + (isOptional ? self.or(key) : self.where(key)).gte(spec[key]); } } }; - var applyLesserThan = function (spec, clause) { + var applyLesserThan = function (spec, isOptional) { + if (typeof isOptional === 'undefined') { + isOptional = false; + } + for (var key in spec) { if (spec.hasOwnProperty(key)) { - clause(key).lt(spec[key]); + (isOptional ? self.or(key) : self.where(key)).lt(spec[key]); } } }; - var applyLesserThanEqual = function (spec, clause) { + var applyLesserThanEqual = function (spec, isOptional) { + if (typeof isOptional === 'undefined') { + isOptional = false; + } + for (var key in spec) { if (spec.hasOwnProperty(key)) { - clause(key).lte(spec[key]); + (isOptional ? self.or(key) : self.where(key)).lte(spec[key]); } } }; - var applyNotEqual = function (spec, clause) { + var applyNotEqual = function (spec, isOptional) { + if (typeof isOptional === 'undefined') { + isOptional = false; + } + for (var key in spec) { if (spec.hasOwnProperty(key)) { - clause(key).ne(parseWhereSpec(spec[key])); + (isOptional ? self.or(key) : self.where(key)) + .ne(analyzeWhereSpec(spec[key])); } } }; - var applyRegex = function (spec, buildRegex, clause) { + var applyRegex = function (spec, buildRegex, isOptional) { + if (typeof isOptional === 'undefined') { + isOptional = false; + } + var bulkApply = function (key, val) { - console.log('bulk applying key: ', key); val.forEach(function (term) { - clause(key, term); + if (isOptional) { + self.or(key, term); + } else { + self.where(key, term); + } }); }; @@ -71,32 +121,14 @@ module.exports = function(mongoose) { if (Array.isArray(val)) { bulkApply(key, val); } else { - clause(key, val); - } - } - } - }; - - var parseWhereSpec = function (val) { - if (typeof val === 'string') { - switch (val.toLowerCase()) { - case 'null' : return null; - case 'true' : return true; - case 'false' : return false; - default : - if (!isNaN(val)) { - // val is a number - if (val.indexOf('.') > -1) { - // val is a float - return parseFloat(val); - } else { - return parseInt(val); - } + if (isOptional) { + self.or(key, val); + } else { + self.where(key, val); } + } } } - - return val; }; var regexContains = function (val) { @@ -130,7 +162,7 @@ module.exports = function(mongoose) { }); } - val = parseWhereSpec(val); + val = analyzeWhereSpec(val); if (typeof val === 'string') { return new RegExp('^' + sanitize(val) + '$', 'i'); @@ -150,48 +182,43 @@ module.exports = function(mongoose) { }; // MANDATORY - applyRegex(mandatory.contains, regexContains, self.where); - applyRegex(mandatory.endsWith, regexEndsWith, self.where); - applyRegex(mandatory.startsWith, regexStartsWith, self.where); - applyRegex(mandatory.exact, regexExact, self.where); + applyRegex(mandatory.contains, regexContains); + applyRegex(mandatory.endsWith, regexEndsWith); + applyRegex(mandatory.startsWith, regexStartsWith); + applyRegex(mandatory.exact, regexExact); applyGreaterThan( - mandatory.greaterThan || mandatory.gt || {}, - self.where); + mandatory.greaterThan || mandatory.gt || {}); applyGreaterThanEqual( - mandatory.greaterThanEqual || mandatory.gte || {}, - self.where); + mandatory.greaterThanEqual || mandatory.gte || {}); applyLesserThan( - mandatory.lessThan || mandatory.lt || {}, - self.where); + mandatory.lessThan || mandatory.lt || {}); applyLesserThanEqual( - mandatory.lessThanEqual || mandatory.lte || {}, - self.where); + mandatory.lessThanEqual || mandatory.lte || {}); applyNotEqual( - mandatory.notEqual || mandatory.notEqualTo || mandatory.ne || {}, - self.where); + mandatory.notEqual || mandatory.notEqualTo || mandatory.ne || {}); // OPTIONAL - applyRegex(optional.contains, regexContains, self.or); - applyRegex(optional.endsWith, regexEndsWith, self.or); - applyRegex(optional.startsWith, regexStartsWith, self.or); - applyRegex(optional.exact, regexExact, self.or); + applyRegex(optional.contains, regexContains, true); + applyRegex(optional.endsWith, regexEndsWith, true); + applyRegex(optional.startsWith, regexStartsWith, true); + applyRegex(optional.exact, regexExact, true); applyGreaterThan( optional.greaterThan || optional.gt || {}, - self.or); + true); applyGreaterThanEqual( optional.greaterThanEqual || optional.gte || {}, - self.or); + true); applyLesserThan( optional.lessThan || optional.lt || {}, - self.or); + true); applyLesserThanEqual( optional.lessThanEqual || optional.lte || {}, - self.or); + true); applyNotEqual( optional.notEqual || optional.notEqualTo || optional.ne || {}, - self.or); + true); return self; }; diff --git a/package.json b/package.json index cf95831..056e148 100644 --- a/package.json +++ b/package.json @@ -1,21 +1,22 @@ { "name": "mongoose-middleware", "description": "Middleware for mongoose that makes filtering, sorting, pagination and projection chainable and simple to apply", - "version": "0.2.14", + "version": "0.2.15", "scripts": { "test": "gulp test-all" }, "devDependencies": { - "chai": "^1.10.0", - "del": "^1.1.1", - "gulp": "^3.8.10", - "gulp-coveralls": "^0.1.3", - "gulp-istanbul": "^0.5.0", - "gulp-jshint": "^1.9.0", - "gulp-mocha": "^2.0.0", - "jshint-stylish": "^1.0.0", - "mongoose": "~3.8.21", - "run-sequence": "^1.0.2" + "chai": "^3.5.0", + "del": "^2.2.0", + "gulp": "^3.9.1", + "gulp-coveralls": "^0.1.4", + "gulp-istanbul": "^0.10.3", + "gulp-jshint": "^2.0.0", + "gulp-mocha": "^2.2.0", + "jshint": "^2.9.1", + "jshint-stylish": "^2.1.0", + "mongoose": "^4.4.6", + "run-sequence": "^1.1.5" }, "keywords": [ "mongo", @@ -23,6 +24,7 @@ "mongoose middlware", "mongoose-middleware" ], + "license": "MIT", "repository": { "type": "git", "url": "git@github.com:PlayNetwork/mongoose-middleware.git" diff --git a/test/lib/keyword.js b/test/lib/keyword.js index 546dd5d..b701023 100644 --- a/test/lib/keyword.js +++ b/test/lib/keyword.js @@ -99,7 +99,7 @@ describe('keyword', function () { should.exist(query); orClauseItems[0].should.have.length(2); - orClauseItems[0][1].peePatches.$in.should.exist(); + should.exist(orClauseItems[0][1].peePatches.$in); }); it ('should search for keyword occurrences with multiple words', function () { diff --git a/test/lib/page.js b/test/lib/page.js index 83e9445..e465043 100644 --- a/test/lib/page.js +++ b/test/lib/page.js @@ -1,3 +1,4 @@ +/* jshint -W030 */ // allows use of .empty var chai = require('chai'), should = chai.should(); @@ -64,7 +65,7 @@ describe('page', function () { .find() .page(null, function (err, data) { should.not.exist(err); - data.should.not.be.empty(); + data.should.not.be.empty; skip.should.equals(0); return done(); @@ -78,7 +79,7 @@ describe('page', function () { .find() .page(null, function (err, data) { should.not.exist(err); - data.should.not.be.empty(); + data.should.not.be.empty; limit.should.equals(25); skip.should.equals(0); @@ -98,7 +99,7 @@ describe('page', function () { .find() .page(options, function (err, data) { should.not.exist(err); - data.should.not.be.empty(); + data.should.not.be.empty; limit.should.equals(25); skip.should.equals(0); @@ -148,7 +149,7 @@ describe('page', function () { data.options.start.should.equals(0); data.options.count.should.equals(50); - data.results.should.be.empty(); + data.results.should.be.empty; data.total.should.equals(total); return done();