Skip to content

Commit d0c5361

Browse files
committed
Update to latest jsonapi-server with parsed filter.
Update to support latest version of jsonapi-server, check that we have a minimum version that provides the `processedFilter` property and make use of that to avoid the duplicated effort.
1 parent 98f9446 commit d0c5361

File tree

2 files changed

+20
-36
lines changed

2 files changed

+20
-36
lines changed

lib/mongoHandler.js

Lines changed: 18 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,30 @@ 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
};
@@ -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 = { };

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,13 @@
3030
"joi": "^6.10.1",
3131
"lodash.omitby": "^4.4.0",
3232
"mongodb": "^2.1.20",
33+
"semver": "^5.1.0"
3334
},
3435
"devDependencies": {
35-
"jsonapi-server": "1.4.0",
3636
"blanket": "^1.2.3",
3737
"coveralls": "^2.11.9",
3838
"eslint": "^2.11.0",
39+
"jsonapi-server": "holidayextras/jsonapi-server#2ba05c6",
3940
"mocha": "^2.5.3",
4041
"mocha-lcov-reporter": "^1.2.0",
4142
"mocha-performance": "^0.1.1",

0 commit comments

Comments
 (0)