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 -11
View File
@@ -1,16 +1,9 @@
import mongoose from 'mongoose'
import { Command } from '@linux-odyssey/models'
// eslint-disable-next-line import/prefer-default-export
export async function errorCommands({ nextKey, itemsPerPage, order }) {
let matchStage = {}
if (nextKey) {
const key = new mongoose.Types.ObjectId(nextKey)
matchStage = { _id: order.matchStage(key) }
}
console.log(matchStage, itemsPerPage, nextKey, order)
export async function errorCommands(pagination) {
const commands = await Command.aggregate([
{ $match: matchStage },
pagination.matchStage('_id'),
{
$match: {
$and: [
@@ -38,8 +31,8 @@ export async function errorCommands({ nextKey, itemsPerPage, order }) {
},
},
{ $unwind: '$session.user' },
{ $sort: { _id: order.order() } },
{ $limit: itemsPerPage },
pagination.order('_id'),
pagination.limit(),
])
return commands.map(({ _id, command, error, createdAt, session }) => {
@@ -0,0 +1,42 @@
/* eslint-disable lines-between-class-members */
/* eslint-disable class-methods-use-this */
// eslint-disable-next-line max-classes-per-file
import mongoose from 'mongoose'
class Pagination {
nextKey = null
itemsPerPage = 50
constructor(itemsPerPage = 50, nextKey = null) {
this.itemsPerPage = itemsPerPage
if (nextKey) {
this.nextKey = new mongoose.Types.ObjectId(nextKey)
}
}
limit() {
return { $limit: this.itemsPerPage }
}
}
export class Asc extends Pagination {
matchStage(field) {
const stage = this.nextKey ? { [field]: { $gt: this.nextKey } } : {}
return { $match: stage }
}
order(field) {
return { $sort: { [field]: 1 } }
}
}
export class Desc extends Pagination {
matchStage(field) {
const stage = this.nextKey ? { [field]: { $lt: this.nextKey } } : {}
return { $match: stage }
}
order(field) {
return { $sort: { [field]: -1 } }
}
}
+4 -12
View File
@@ -1,4 +1,3 @@
import mongoose from 'mongoose'
import { Command, Session } from '@linux-odyssey/models'
export function sessionCount() {
@@ -31,16 +30,9 @@ function formatTime(time) {
return `${hours}h ${minutes % 60}m ${seconds % 60}s`
}
export async function sessionList({ nextKey, itemsPerPage, order }) {
let matchStage = {}
if (nextKey) {
const key = new mongoose.Types.ObjectId(nextKey)
matchStage = { _id: order.matchStage(key) }
}
export async function sessionList(pagination) {
const sessions = await Session.aggregate([
{
$match: matchStage,
},
pagination.matchStage('_id'),
{
$lookup: {
from: 'users',
@@ -58,8 +50,8 @@ export async function sessionList({ nextKey, itemsPerPage, order }) {
as: 'commands',
},
},
{ $sort: { _id: order.order() } },
{ $limit: itemsPerPage },
pagination.order('_id'),
pagination.limit(),
])
return sessions.map(
+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 }) => {