Add paginated user listing on the db level
This commit is contained in:
@@ -8,6 +8,7 @@ exports = module.exports = {
|
||||
getByResetToken: getByResetToken,
|
||||
getOwner: getOwner,
|
||||
getAllWithGroupIds: getAllWithGroupIds,
|
||||
getAllWithGroupIdsPaged: getAllWithGroupIdsPaged,
|
||||
getAllAdmins: getAllAdmins,
|
||||
add: add,
|
||||
del: del,
|
||||
@@ -115,6 +116,30 @@ function getAllWithGroupIds(callback) {
|
||||
});
|
||||
}
|
||||
|
||||
function getAllWithGroupIdsPaged(page, perPage, callback) {
|
||||
assert.strictEqual(typeof page, 'number');
|
||||
assert.strictEqual(typeof perPage, 'number');
|
||||
assert.strictEqual(typeof callback, 'function');
|
||||
|
||||
var query = `SELECT ${USERS_FIELDS},GROUP_CONCAT(groupMembers.groupId) AS groupIds
|
||||
FROM users LEFT OUTER JOIN groupMembers ON users.id = groupMembers.userId
|
||||
GROUP BY users.id
|
||||
ORDER BY users.username
|
||||
ASC LIMIT ${(page-1)*perPage},${perPage}`;
|
||||
|
||||
database.query(query, function (error, results) {
|
||||
if (error) return callback(new DatabaseError(DatabaseError.INTERNAL_ERROR, error));
|
||||
|
||||
results.forEach(function (result) {
|
||||
result.groupIds = result.groupIds ? result.groupIds.split(',') : [ ];
|
||||
});
|
||||
|
||||
results.forEach(postProcess);
|
||||
|
||||
callback(null, results);
|
||||
});
|
||||
}
|
||||
|
||||
function getAllAdmins(callback) {
|
||||
assert.strictEqual(typeof callback, 'function');
|
||||
|
||||
|
||||
Reference in New Issue
Block a user