Merge pull request #24 from strongloop/undefined-query-value

Remove undefined values from the query object
This commit is contained in:
Raymond Feng 2013-10-11 11:56:26 -07:00
commit a9cf0567ae
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 Relation = require('./relations.js');
var geo = require('./geo'); var geo = require('./geo');
var Memory = require('./connectors/memory').Memory; 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 * DAO class - base class for all persist objects
@ -69,7 +72,6 @@ DataAccessObject._forDB = function (data) {
return res; return res;
}; };
/** /**
* Create new instance of Model class, saved in database * Create new instance of Model class, saved in database
* *
@ -258,7 +260,7 @@ setRemoting(DataAccessObject.upsert, {
* @param {Function} cb - callback called with (err, instance) * @param {Function} cb - callback called with (err, instance)
*/ */
DataAccessObject.findOrCreate = function findOrCreate(query, data, callback) { DataAccessObject.findOrCreate = function findOrCreate(query, data, callback) {
if (typeof query === 'undefined') { if (query === undefined) {
query = {where: {}}; query = {where: {}};
} }
if (typeof data === 'function' || typeof data === 'undefined') { if (typeof data === 'function' || typeof data === 'undefined') {
@ -374,6 +376,7 @@ DataAccessObject.find = function find(params, cb) {
params.fields = fieldsToArray(fields, Object.keys(this.definition.properties)); params.fields = fieldsToArray(fields, Object.keys(this.definition.properties));
} }
params = removeUndefined(params);
if(near) { if(near) {
if(supportsGeo) { if(supportsGeo) {
// convert it // convert it
@ -495,6 +498,7 @@ DataAccessObject.destroyAll = function destroyAll(where, cb) {
}.bind(this)); }.bind(this));
} else { } else {
// Support an optional where object // Support an optional where object
where = removeUndefined(where);
this.dataSource.connector.destroyAll(this.modelName, where, function (err, data) { this.dataSource.connector.destroyAll(this.modelName, where, function (err, data) {
cb && cb(err, data); cb && cb(err, data);
}.bind(this)); }.bind(this));
@ -539,6 +543,7 @@ DataAccessObject.count = function (where, cb) {
cb = where; cb = where;
where = null; where = null;
} }
where = removeUndefined(where);
this.dataSource.connector.count(this.modelName, cb, where); this.dataSource.connector.count(this.modelName, cb, where);
}; };

View File

@ -1,6 +1,9 @@
exports.safeRequire = safeRequire; exports.safeRequire = safeRequire;
exports.fieldsToArray = fieldsToArray; exports.fieldsToArray = fieldsToArray;
exports.selectFields = selectFields; exports.selectFields = selectFields;
exports.removeUndefined = removeUndefined;
var traverse = require('traverse');
function safeRequire(module) { function safeRequire(module) {
try { try {
@ -67,3 +70,20 @@ function selectFields(fields) {
return result; 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 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(){ describe('util.fieldsToArray', function(){
it('Turn objects and strings into an array of fields to include when finding models', function() { it('Turn objects and strings into an array of fields to include when finding models', function() {
@ -25,3 +28,23 @@ describe('util.fieldsToArray', function(){
sample({'bat': false}).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');
});
});