diff --git a/src/database/mongo/hash.js b/src/database/mongo/hash.js index 6db3b5a4a9..f40a756600 100644 --- a/src/database/mongo/hash.js +++ b/src/database/mongo/hash.js @@ -174,6 +174,31 @@ module.exports = function(db, module) { }); }; + module.isObjectFields = function(key, fields, callback) { + if (!key) { + return callback(); + } + + var data = {}; + fields.forEach(function(field) { + field = helpers.fieldToString(field); + data[field] = ''; + }); + + db.collection('objects').findOne({_key: key}, {fields: data}, function(err, item) { + if (err) { + return callback(err); + } + var results = []; + + fields.forEach(function(field, index) { + results[index] = !!item && item[field] !== undefined && item[field] !== null; + }); + + callback(null, results); + }); + }; + module.deleteObjectField = function(key, field, callback) { callback = callback || helpers.noop; if (!key || !field) { diff --git a/tests/database/hash.js b/tests/database/hash.js index c67b57fd64..5d1d002c3b 100644 --- a/tests/database/hash.js +++ b/tests/database/hash.js @@ -222,7 +222,7 @@ describe('Hash methods', function() { }); it('should return false if field does not exist', function(done) { - db.isObjectField('testObject1', 'field1', function(err, value) { + db.isObjectField('hashTestObject', 'field1', function(err, value) { assert.equal(err, null); assert.equal(arguments.length, 2); assert.equal(value, false); @@ -240,6 +240,27 @@ describe('Hash methods', function() { }); }); + + describe('isObjectFields()', function() { + it('should return an array of false if object does not exist', function(done) { + db.isObjectFields('doesnotexist', ['field1', 'field2'], function(err, values) { + assert.equal(err, null); + assert.equal(arguments.length, 2); + assert.deepEqual(values, [false, false]); + done(); + }); + }); + + it('should return false if field does not exist', function(done) { + db.isObjectFields('hashTestObject', ['name', 'age', 'field1'], function(err, values) { + assert.equal(err, null); + assert.equal(arguments.length, 2); + assert.deepEqual(values, [true, true, false]); + done(); + }); + }); + }); + describe('deleteObjectField()', function() { before(function(done) { db.setObject('testObject10', {foo: 'bar', delete: 'this'}, done);