refactor: add Pagination class

This commit is contained in:
Chen, Tai-Lin committed 2023-12-21 17:09:20 +08:00
1 parent 19cd10ab7b
commit 68db16419d
9 files changed
+99 -89

No files matched your search

+4 -10
View File
@@ -12,15 +12,9 @@ export function userCount() {
return User.find().count()
}
export async function userList({ nextKey, itemsPerPage, order }) {
let matchStage = {}
if (nextKey) {
const key = new mongoose.Types.ObjectId(nextKey)
matchStage = { user: order.matchStage(key) }
}
console.log(matchStage)
export async function userList(pagination) {
const users = await Session.aggregate([
{ $match: matchStage },
pagination.matchStage('user'),
{
$group: {
_id: '$user',
@@ -37,8 +31,8 @@ export async function userList({ nextKey, itemsPerPage, order }) {
},
},
{ $unwind: '$userData' },
{ $sort: { 'userData._id': order.order() } },
{ $limit: itemsPerPage },
pagination.order('userData._id'),
pagination.limit(),
])
return users.map(({ userData, count, lastActivityAt }) => {