add type to dbal

v1.18.x
Barış Soner Uşaklı 7 years ago
parent 92684757c6
commit 108f84fd19

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

@ -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) {

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

Loading…
Cancel
Save