Skip to content

Commit

Permalink
fixing issue where there was an incompatibility with mquery (TypeErro…
Browse files Browse the repository at this point in the history
…r: Cannot read property op of undefined)
  • Loading branch information
brozeph committed Mar 4, 2016
1 parent bdfd766 commit 8224698
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 37 deletions.
1 change: 1 addition & 0 deletions history.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# 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
Expand Down
100 changes: 64 additions & 36 deletions lib/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,50 +38,79 @@ module.exports = function(mongoose) {
return val;
};

var applyGreaterThan = function (spec, clause) {
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(analyzeWhereSpec(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) {
val.forEach(function (term) {
clause(key, term);
if (isOptional) {
self.or(key, term);
} else {
self.where(key, term);
}
});
};

Expand All @@ -92,7 +121,11 @@ module.exports = function(mongoose) {
if (Array.isArray(val)) {
bulkApply(key, val);
} else {
clause(key, val);
if (isOptional) {
self.or(key, val);
} else {
self.where(key, val);
}
}
}
}
Expand Down Expand Up @@ -149,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;
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"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"
},
Expand Down

0 comments on commit 8224698

Please sign in to comment.