feat: initial pagination

This commit is contained in:
arashsheyda
2023-04-26 18:31:25 +03:00
parent f40c48370c
commit 3ac731c5ee
3 changed files with 21 additions and 3 deletions

View File

@ -26,8 +26,15 @@ export function setupDatabaseRPC({ options }: NuxtDevtoolsServerContext): any {
const { _id, ...rest } = data
return await mongoose.connection.db.collection(collection).insertOne(rest)
},
async listDocuments(collection: string) {
return await mongoose.connection.db.collection(collection).find().toArray()
async countDocuments(collection: string) {
return await mongoose.connection.db.collection(collection).countDocuments()
},
async listDocuments(collection: string, options: { page: number; limit: number } = { page: 1, limit: 10 }) {
const skip = (options.page - 1) * options.limit
const cursor = mongoose.connection.db.collection(collection).find().skip(skip)
if (options.limit !== 0)
cursor.limit(options.limit)
return await cursor.toArray()
},
async getDocument(collection: string, document: {}) {
return await mongoose.connection.db.collection(collection).findOne({ document })