From 3ac731c5eee656e79d5865abc8709892c812abba Mon Sep 17 00:00:00 2001 From: arashsheyda Date: Wed, 26 Apr 2023 18:31:25 +0300 Subject: [PATCH] feat: initial pagination --- client/composables/editor.ts | 10 ++++++++++ src/server-rpc/database.ts | 11 +++++++++-- src/types/rpc.ts | 3 ++- 3 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 client/composables/editor.ts diff --git a/client/composables/editor.ts b/client/composables/editor.ts new file mode 100644 index 0000000..f7237a9 --- /dev/null +++ b/client/composables/editor.ts @@ -0,0 +1,10 @@ +import { useClipboard } from '@vueuse/core' + +export function useCopy() { + const clipboard = useClipboard() + + return (text: string) => { + clipboard.copy(text) + // TODO: show toast + } +} diff --git a/src/server-rpc/database.ts b/src/server-rpc/database.ts index e04ff15..8c415eb 100644 --- a/src/server-rpc/database.ts +++ b/src/server-rpc/database.ts @@ -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 }) diff --git a/src/types/rpc.ts b/src/types/rpc.ts index ec2d469..53e2874 100644 --- a/src/types/rpc.ts +++ b/src/types/rpc.ts @@ -8,7 +8,8 @@ export interface ServerFunctions { // Database - documents createDocument(collection: string, data: any): Promise - listDocuments(collection: string): Promise + countDocuments(collection: string): Promise + listDocuments(collection: string, options: any): Promise getDocument(collection: string, id: string): Promise updateDocument(collection: string, data: any): Promise deleteDocument(collection: string, id: string): Promise