From 9ec9f654c79bcba69c16ac434800b783edceb3e8 Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Fri, 11 Oct 2013 11:50:00 -0700 Subject: [PATCH] Remove undefined values from the query object --- lib/dao.js | 13 +++++++++---- lib/utils.js | 22 +++++++++++++++++++++- test/util.test.js | 25 ++++++++++++++++++++++++- 3 files changed, 54 insertions(+), 6 deletions(-) diff --git a/lib/dao.js b/lib/dao.js index 6d2675f9..65cbcf97 100644 --- a/lib/dao.js +++ b/lib/dao.js @@ -16,7 +16,10 @@ var Inclusion = require('./include.js'); var Relation = require('./relations.js'); var geo = require('./geo'); var Memory = require('./connectors/memory').Memory; -var fieldsToArray = require('./utils').fieldsToArray; +var utils = require('./utils'); +var fieldsToArray = utils.fieldsToArray; +var removeUndefined = utils.removeUndefined; + /** * DAO class - base class for all persist objects @@ -69,7 +72,6 @@ DataAccessObject._forDB = function (data) { return res; }; - /** * Create new instance of Model class, saved in database * @@ -258,7 +260,7 @@ setRemoting(DataAccessObject.upsert, { * @param {Function} cb - callback called with (err, instance) */ DataAccessObject.findOrCreate = function findOrCreate(query, data, callback) { - if (typeof query === 'undefined') { + if (query === undefined) { query = {where: {}}; } if (typeof data === 'function' || typeof data === 'undefined') { @@ -373,7 +375,8 @@ DataAccessObject.find = function find(params, cb) { if(fields) { params.fields = fieldsToArray(fields, Object.keys(this.definition.properties)); } - + + params = removeUndefined(params); if(near) { if(supportsGeo) { // convert it @@ -495,6 +498,7 @@ DataAccessObject.destroyAll = function destroyAll(where, cb) { }.bind(this)); } else { // Support an optional where object + where = removeUndefined(where); this.dataSource.connector.destroyAll(this.modelName, where, function (err, data) { cb && cb(err, data); }.bind(this)); @@ -539,6 +543,7 @@ DataAccessObject.count = function (where, cb) { cb = where; where = null; } + where = removeUndefined(where); this.dataSource.connector.count(this.modelName, cb, where); }; diff --git a/lib/utils.js b/lib/utils.js index 07c45b0a..3b844c17 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -1,6 +1,9 @@ exports.safeRequire = safeRequire; exports.fieldsToArray = fieldsToArray; exports.selectFields = selectFields; +exports.removeUndefined = removeUndefined; + +var traverse = require('traverse'); function safeRequire(module) { try { @@ -66,4 +69,21 @@ function selectFields(fields) { } return result; } -} \ No newline at end of file +} + +/** + * Remove undefined values from the queury object + * @param query + * @returns {exports.map|*} + */ +function removeUndefined(query) { + if(typeof query !== 'object' || query === null) { + return query; + } + return traverse(query).map(function (x) { + if(x === undefined) { + this.remove(); + } + return x; + }); +} diff --git a/test/util.test.js b/test/util.test.js index 6a4c9eb6..d4943d9e 100644 --- a/test/util.test.js +++ b/test/util.test.js @@ -1,5 +1,8 @@ var should = require('./init.js'); -var fieldsToArray = require('../lib/utils').fieldsToArray; +var utils = require('../lib/utils'); +var fieldsToArray = utils.fieldsToArray; +var removeUndefined = utils.removeUndefined; + describe('util.fieldsToArray', function(){ it('Turn objects and strings into an array of fields to include when finding models', function() { @@ -24,4 +27,24 @@ describe('util.fieldsToArray', function(){ sample({'bat': 0}).expect(['foo', 'bar', 'baz']); sample({'bat': false}).expect(['foo', 'bar', 'baz']); }); +}); + +describe('util.removeUndefined', function(){ + it('Remove undefined values from the query object', function() { + var q1 = {where: {x: 1, y: undefined}}; + should.deepEqual(removeUndefined(q1), {where: {x: 1}}); + + var q2 = {where: {x: 1, y: 2}}; + should.deepEqual(removeUndefined(q2), {where: {x: 1, y: 2}}); + + var q3 = {where: {x: 1, y: {in: [2, undefined]}}}; + should.deepEqual(removeUndefined(q3), {where: {x: 1, y: {in: [2]}}}); + + should.equal(removeUndefined(null), null); + + should.equal(removeUndefined(undefined), undefined); + + should.equal(removeUndefined('x'), 'x'); + + }); }); \ No newline at end of file