mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-12-21 16:00:26 +01:00
add llen to dbal
This commit is contained in:
@@ -100,4 +100,13 @@ module.exports = function (db, module) {
|
|||||||
callback(null, data.array);
|
callback(null, data.array);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
module.listLength = function (key, callback) {
|
||||||
|
db.collection('objects').aggregate([
|
||||||
|
{ $match: { _key: key } },
|
||||||
|
{ $project: { count: { $size: "$array" } } },
|
||||||
|
], function (err, result) {
|
||||||
|
callback(err, Array.isArray(result) && result.length && result[0].count);
|
||||||
|
});
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -56,4 +56,8 @@ module.exports = function (redisClient, module) {
|
|||||||
}
|
}
|
||||||
redisClient.lrange(key, start, stop, callback);
|
redisClient.lrange(key, start, stop, callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
module.listLength = function (key, callback) {
|
||||||
|
redisClient.llen(key, callback);
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -200,4 +200,19 @@ describe('List methods', function () {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
it('should get the length of a list', function (done) {
|
||||||
|
db.listAppend('getLengthList', 1, function (err) {
|
||||||
|
assert.ifError(err);
|
||||||
|
db.listAppend('getLengthList', 2, function (err) {
|
||||||
|
assert.ifError(err);
|
||||||
|
db.listLength('getLengthList', function (err, length) {
|
||||||
|
assert.ifError(err);
|
||||||
|
assert.equal(length, 2);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user