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

@ -0,0 +1,10 @@
import { useClipboard } from '@vueuse/core'
export function useCopy() {
const clipboard = useClipboard()
return (text: string) => {
clipboard.copy(text)
// TODO: show toast
}
}

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 })

View File

@ -8,7 +8,8 @@ export interface ServerFunctions {
// Database - documents
createDocument(collection: string, data: any): Promise<any>
listDocuments(collection: string): Promise<any>
countDocuments(collection: string): Promise<any>
listDocuments(collection: string, options: any): Promise<any>
getDocument(collection: string, id: string): Promise<any>
updateDocument(collection: string, data: any): Promise<any>
deleteDocument(collection: string, id: string): Promise<any>