mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-08 03:20:12 +08:00
refactor: add Pagination class
This commit is contained in:
1 parent
19cd10ab7b
commit
68db16419d
9 files changed
+99
-89
No files matched your search
@@ -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 } }
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user