Skip to content

Commit fbeed58

Browse files
committed
Merge pull request #29 from holidayextras/use-processed-filter-from-latest-server
Use processed filter from latest server
2 parents 74c8578 + 19473a7 commit fbeed58

File tree

3 files changed

+38
-52
lines changed

3 files changed

+38
-52
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
- 2016-05-31 - v1.4.0
2+
- 2016-05-31 - Use latest `jsonapi-server` processed filter
13
- 2016-04-22 - v1.3.0
24
- 2016-04-22 - Improved index creation
35
- 2016-04-22 - Better error handling on .find()

lib/mongoHandler.js

Lines changed: 21 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,35 @@
11
"use strict";
22
var _ = {
3-
omit: require("lodash.omit")
3+
omitBy: require("lodash.omitby")
44
};
55
var async = require("async");
66
var debug = require("./debugging");
77
var mongodb = require("mongodb");
88
var Joi = require("joi");
9+
var semver = require("semver");
10+
11+
var MIN_SERVER_VERSION = "1.10.0";
912

1013
var MongoStore = module.exports = function MongoStore(config) {
14+
MongoStore._checkMinServerVersion();
1115
this._config = config;
1216
};
1317

14-
15-
var FILTER_OPERATORS = ["<", ">", "~", ":"];
16-
17-
1818
/**
1919
Handlers readiness status. This should be set to `true` once all handlers are ready to process requests.
2020
*/
2121
MongoStore.prototype.ready = false;
2222

2323

24+
MongoStore._checkMinServerVersion = function() {
25+
var serverVersion = require('jsonapi-server')._version;
26+
if (!serverVersion) return;
27+
if (semver.lt(serverVersion, MIN_SERVER_VERSION)) {
28+
throw new Error("This version of jsonapi-store-mongodb requires jsonapi-server>=" + MIN_SERVER_VERSION + ".");
29+
}
30+
};
31+
32+
2433
MongoStore._mongoUuid = function(uuid) {
2534
return new mongodb.Binary(uuid, mongodb.Binary.SUBTYPE_UUID);
2635
};
@@ -32,7 +41,7 @@ MongoStore._isRelationshipAttribute = function(attribute) {
3241

3342

3443
MongoStore._toMongoDocument = function(resource) {
35-
var document = _.omit(resource, function(value) { return value === undefined; });
44+
var document = _.omitBy(resource, function(value) { return value === undefined; });
3645
document._id = MongoStore._mongoUuid(document.id);
3746
return document;
3847
};
@@ -51,26 +60,9 @@ MongoStore._getRelationshipAttributeNames = function(attributes) {
5160
};
5261

5362

54-
MongoStore._splitFilterElement = function(filterElementStr) {
55-
if (FILTER_OPERATORS.indexOf(filterElementStr[0]) !== -1) {
56-
return { operator: filterElementStr[0], value: filterElementStr.substring(1) };
57-
}
58-
return { operator: null, value: filterElementStr };
59-
};
60-
61-
62-
MongoStore._filterElementToMongoExpr = function(attributeConfig, filterElementStr) {
63-
var filterElement = MongoStore._splitFilterElement(filterElementStr);
63+
MongoStore._filterElementToMongoExpr = function(filterElement) {
6464
var value = filterElement.value;
65-
if (!attributeConfig._settings) { // not a relationship attribute
66-
var validationResult = attributeConfig.validate(filterElement.value);
67-
if (validationResult.error) return null;
68-
value = validationResult.value;
69-
}
7065
if (!filterElement.operator) return value;
71-
if (["~", ":"].indexOf(filterElement.operator) !== -1 && typeof value !== "string") {
72-
return null;
73-
}
7466
var mongoExpr = {
7567
">": { $gt: value },
7668
"<": { $lt: value },
@@ -83,24 +75,15 @@ MongoStore._filterElementToMongoExpr = function(attributeConfig, filterElementSt
8375

8476
MongoStore.prototype._getSearchCriteria = function(request) {
8577
var self = this;
86-
if (!request.params.filter) return { };
87-
88-
var criteria = Object.keys(request.params.filter).map(function(attribute) {
78+
var filter = request.processedFilter;
79+
if (!filter) return { };
80+
var criteria = Object.keys(filter).map(function(attribute) {
81+
var values = filter[attribute].map(MongoStore._filterElementToMongoExpr);
8982
var attributeConfig = self.resourceConfig.attributes[attribute];
90-
// If the filter attribute doesn't exist, skip it
91-
if (!attributeConfig) return null;
92-
93-
var values = request.params.filter[attribute];
9483
// Relationships need to be queried via .id
9584
if (attributeConfig._settings) {
9685
attribute += ".id";
97-
// Filters on nested resources should be skipped
98-
if (values instanceof Object) return null;
9986
}
100-
101-
values = [].concat(values); // Coerce values to an array to simplify the logic
102-
var filterElemForAttrToMongoExpr = MongoStore._filterElementToMongoExpr.bind(null, attributeConfig);
103-
values = values.map(filterElemForAttrToMongoExpr);
10487
values = values.reduce(function(mongoExpressions, mongoExpr) {
10588
if (mongoExpr !== null) {
10689
var mongoExprForAttr = { };
@@ -333,7 +316,7 @@ MongoStore.prototype.delete = function(request, callback) {
333316
MongoStore.prototype.update = function(request, partialResource, callback) {
334317
var collection = this._db.collection(request.params.type);
335318
var documentId = MongoStore._mongoUuid(request.params.id);
336-
var partialDocument = _.omit(partialResource, function(value) { return value === undefined; });
319+
var partialDocument = _.omitBy(partialResource, function(value) { return value === undefined; });
337320
debug("findOneAndUpdate", JSON.stringify(partialDocument));
338321
collection.findOneAndUpdate({
339322
_id: documentId

package.json

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jsonapi-store-mongodb",
3-
"version": "1.3.0",
3+
"version": "1.4.0",
44
"description": "MongoDB data store for jsonapi-server.",
55
"main": "lib/mongoHandler.js",
66
"repository": {
@@ -25,21 +25,22 @@
2525
"node": "*"
2626
},
2727
"dependencies": {
28-
"async": "1.5.0",
29-
"debug": "2.2.0",
30-
"joi": "6.10.1",
31-
"lodash.omit": "3.1.0",
32-
"mongodb": "2.0.48"
28+
"async": "^1.5.2",
29+
"debug": "^2.2.0",
30+
"joi": "^6.10.1",
31+
"lodash.omitby": "^4.4.0",
32+
"mongodb": "^2.1.20",
33+
"semver": "^5.1.0"
3334
},
3435
"devDependencies": {
35-
"blanket": "1.1.7",
36-
"coveralls": "2.11.2",
37-
"eslint": "0.24.1",
38-
"jsonapi-server": "1.4.0",
39-
"mocha": "2.2.5",
40-
"mocha-lcov-reporter": "0.0.2",
41-
"mocha-performance": "0.1.0",
42-
"plato": "1.5.0",
36+
"blanket": "^1.2.3",
37+
"coveralls": "^2.11.9",
38+
"eslint": "^2.11.0",
39+
"jsonapi-server": "^1.10.0",
40+
"mocha": "^2.5.3",
41+
"mocha-lcov-reporter": "^1.2.0",
42+
"mocha-performance": "^0.1.1",
43+
"plato": "^1.5.0",
4344
"v8-profiler": "^5.6.0"
4445
},
4546
"scripts": {

0 commit comments

Comments
 (0)