11"use strict" ;
22var _ = {
3- omit : require ( "lodash.omit " )
3+ omitBy : require ( "lodash.omitby " )
44} ;
55var async = require ( "async" ) ;
66var debug = require ( "./debugging" ) ;
77var mongodb = require ( "mongodb" ) ;
88var Joi = require ( "joi" ) ;
9+ var semver = require ( "semver" ) ;
10+
11+ var MIN_SERVER_VERSION = "1.10.0" ;
912
1013var 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 */
2121MongoStore . 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+
2433MongoStore . _mongoUuid = function ( uuid ) {
2534 return new mongodb . Binary ( uuid , mongodb . Binary . SUBTYPE_UUID ) ;
2635} ;
@@ -32,7 +41,7 @@ MongoStore._isRelationshipAttribute = function(attribute) {
3241
3342
3443MongoStore . _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
8476MongoStore . 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) {
333316MongoStore . 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
0 commit comments