mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-01 11:35:55 +01:00
add type to dbal
This commit is contained in:
@@ -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();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user