Remove undefined values from the query object

This commit is contained in:
Raymond Feng 2013-10-11 11:50:00 -07:00
parent 9b602ac52c
commit 9ec9f654c7
3 changed files with 54 additions and 6 deletions

View File

@ -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);
};

View File

@ -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;
}
}
}
/**
* 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;
});
}

View File

@ -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');
});
});