@@ -5,13 +5,17 @@ var async = require("async");
55var crypto = require ( "crypto" ) ;
66var debug = require ( "debug" ) ( "jsonApi:store:relationaldb" ) ;
77var Joi = require ( "joi" ) ;
8+ var semver = require ( "semver" ) ;
89var _ = {
910 pick : require ( "lodash.pick" ) ,
1011 assign : require ( "lodash.assign" ) ,
1112 omit : require ( "lodash.omit" )
1213} ;
1314
15+ var MIN_SERVER_VERSION = "1.10.0" ;
16+
1417var SqlStore = module . exports = function SqlStore ( config ) {
18+ SqlStore . _checkMinServerVersion ( ) ;
1519 this . config = config ;
1620} ;
1721
@@ -22,6 +26,14 @@ SqlStore._sequelizeInstances = Object.create(null);
2226 */
2327SqlStore . prototype . ready = false ;
2428
29+ SqlStore . _checkMinServerVersion = function ( ) {
30+ var serverVersion = require ( 'jsonapi-server' ) . _version ;
31+ if ( ! serverVersion ) return ;
32+ if ( semver . lt ( serverVersion , MIN_SERVER_VERSION ) ) {
33+ throw new Error ( "This version of jsonapi-store-mongodb requires jsonapi-server>=" + MIN_SERVER_VERSION + "." ) ;
34+ }
35+ } ;
36+
2537/**
2638 initialise gets invoked once for each resource that uses this hander.
2739 In this instance, we're instantiating a Sequelize instance and building models.
@@ -252,35 +264,42 @@ SqlStore.prototype._filterInclude = function(relationships) {
252264SqlStore . prototype . _generateSearchBlock = function ( request ) {
253265 var self = this ;
254266
255- var attributesToFilter = _ . omit ( request . params . filter , Object . keys ( self . relations ) ) ;
256- var searchBlock = self . _recurseOverSearchBlock ( attributesToFilter ) ;
267+ var attributesToFilter = _ . omit ( request . params . processedFilter , Object . keys ( self . relations ) ) ;
268+ var searchBlock = SqlStore . _getSearchBlock ( attributesToFilter ) ;
257269 return searchBlock ;
258270} ;
259271
260- SqlStore . prototype . _recurseOverSearchBlock = function ( obj ) {
261- var self = this ;
262- if ( ! obj ) return { } ;
272+ SqlStore . _scalarFilterElementToWhereObj = function ( element ) {
273+ var value = element . value ;
274+ if ( ! element . operator ) return value ;
275+ var whereObj = {
276+ ">" : { $gt : value } ,
277+ "<" : { $lt : value } ,
278+ "~" : { $like : value } ,
279+ ":" : { $like : "%" + value + "%" }
280+ } [ element . operator ] ;
281+ return whereObj ;
282+ } ;
283+
284+ SqlStore . _filterElementToSearchBlock = function ( filterElement ) {
285+ if ( ! filterElement ) return { } ;
286+ var whereObjs = filterElement . map ( function ( scalarFilterElement ) {
287+ return SqlStore . _scalarFilterElementToWhereObj ( scalarFilterElement ) ;
288+ } ) ;
289+ if ( ! whereObjs . length ) return { } ;
290+ if ( filterElement . length === 1 ) {
291+ return whereObjs [ 0 ] ;
292+ }
293+ return { $or : whereObjs } ;
294+ } ;
295+
296+ SqlStore . _getSearchBlock = function ( filter ) {
297+ if ( ! filter ) return { } ;
263298 var searchBlock = { } ;
264299
265- Object . keys ( obj ) . forEach ( function ( attributeName ) {
266- var textToMatch = obj [ attributeName ] ;
267- if ( textToMatch instanceof Array ) {
268- searchBlock [ attributeName ] = { $or : textToMatch . map ( function ( i ) {
269- return self . _recurseOverSearchBlock ( { i : i } ) . i ;
270- } ) } ;
271- } else if ( textToMatch instanceof Object ) {
272- // Do nothing, its a nested filter
273- } else if ( textToMatch [ 0 ] === ">" ) {
274- searchBlock [ attributeName ] = { $gt : textToMatch . substring ( 1 ) } ;
275- } else if ( textToMatch [ 0 ] === "<" ) {
276- searchBlock [ attributeName ] = { $lt : textToMatch . substring ( 1 ) } ;
277- } else if ( textToMatch [ 0 ] === "~" ) {
278- searchBlock [ attributeName ] = { $like : textToMatch . substring ( 1 ) } ;
279- } else if ( textToMatch [ 0 ] === ":" ) {
280- searchBlock [ attributeName ] = { $like : "%" + textToMatch . substring ( 1 ) + "%" } ;
281- } else {
282- searchBlock [ attributeName ] = textToMatch ;
283- }
300+ Object . keys ( filter ) . forEach ( function ( attributeName ) {
301+ var filterElement = filter [ attributeName ] ;
302+ searchBlock [ attributeName ] = SqlStore . _filterElementToSearchBlock ( filterElement ) ;
284303 } ) ;
285304
286305 return searchBlock ;
0 commit comments