From 108f84fd199f2b28c889695c81f57ff5cda53892 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Fri, 20 Oct 2017 13:51:02 -0400 Subject: [PATCH] add type to dbal --- src/database/mongo/main.js | 22 +++++++++++++ src/database/redis/main.js | 6 ++++ test/database/keys.js | 65 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+) diff --git a/src/database/mongo/main.js b/src/database/mongo/main.js index 9381fbbc39..134e6f801d 100644 --- a/src/database/mongo/main.js +++ b/src/database/mongo/main.js @@ -79,6 +79,28 @@ module.exports = function (db, module) { }); }; + module.type = function (key, callback) { + db.collection('objects').findOne({ _key: key }, function (err, data) { + if (err) { + return callback(err); + } + if (!data) { + return callback(null, null); + } + var keys = Object.keys(data); + if (keys.length === 4 && data.hasOwnProperty('_key') && data.hasOwnProperty('score') && data.hasOwnProperty('value')) { + return callback(null, 'zset'); + } else if (keys.length === 3 && data.hasOwnProperty('_key') && data.hasOwnProperty('members')) { + return callback(null, 'set'); + } else if (keys.length === 3 && data.hasOwnProperty('_key') && data.hasOwnProperty('array')) { + return callback(null, 'list'); + } else if (keys.length === 3 && data.hasOwnProperty('_key') && data.hasOwnProperty('value')) { + return callback(null, 'string'); + } + callback(null, 'hash'); + }); + }; + module.expire = function (key, seconds, callback) { module.expireAt(key, Math.round(Date.now() / 1000) + seconds, callback); }; diff --git a/src/database/redis/main.js b/src/database/redis/main.js index baca6b0ffe..cb738eb3d2 100644 --- a/src/database/redis/main.js +++ b/src/database/redis/main.js @@ -60,6 +60,12 @@ module.exports = function (redisClient, module) { }); }; + module.type = function (key, callback) { + redisClient.type(key, function (err, type) { + callback(err, type !== 'none' ? type : null); + }); + }; + module.expire = function (key, seconds, callback) { callback = callback || function () {}; redisClient.expire(key, seconds, function (err) { diff --git a/test/database/keys.js b/test/database/keys.js index a18eee181e..330d84bdd9 100644 --- a/test/database/keys.js +++ b/test/database/keys.js @@ -170,4 +170,69 @@ describe('Key methods', function () { }); }); }); + + describe('type', function () { + it('should return null if key does not exist', function (done) { + db.type('doesnotexist', function (err, type) { + assert.ifError(err); + assert.strictEqual(type, null); + done(); + }); + }); + + it('should return hash as type', function (done) { + db.setObject('typeHash', { foo: 1 }, function (err) { + assert.ifError(err); + db.type('typeHash', function (err, type) { + assert.ifError(err); + assert.equal(type, 'hash'); + done(); + }); + }); + }); + + it('should return zset as type', function (done) { + db.sortedSetAdd('typeZset', 123, 'value1', function (err) { + assert.ifError(err); + db.type('typeZset', function (err, type) { + assert.ifError(err); + assert.equal(type, 'zset'); + done(); + }); + }); + }); + + it('should return set as type', function (done) { + db.setAdd('typeSet', 'value1', function (err) { + assert.ifError(err); + db.type('typeSet', function (err, type) { + assert.ifError(err); + assert.equal(type, 'set'); + done(); + }); + }); + }); + + it('should return list as type', function (done) { + db.listAppend('typeList', 'value1', function (err) { + assert.ifError(err); + db.type('typeList', function (err, type) { + assert.ifError(err); + assert.equal(type, 'list'); + done(); + }); + }); + }); + + it('should return string as type', function (done) { + db.set('typeString', 'value1', function (err) { + assert.ifError(err); + db.type('typeString', function (err, type) { + assert.ifError(err); + assert.equal(type, 'string'); + done(); + }); + }); + }); + }); });