diff --git a/client/components/DatabaseDetail.vue b/client/components/DatabaseDetail.vue
index bf99cf9..e95cf43 100644
--- a/client/components/DatabaseDetail.vue
+++ b/client/components/DatabaseDetail.vue
@@ -78,19 +78,24 @@ function editDocument(document: any) {
selectedDocument.value = { ...document }
}
-async function saveDocument(document: any) {
- await rpc.createDocument(props.collection, document)
- editing.value = false
- selectedDocument.value = undefined
- documents.value = await rpc.listDocuments(props.collection, pagination)
-}
+async function saveDocument(document: any, create = true) {
+ const method = create ? rpc.createDocument : rpc.updateDocument
+ const newDocument = await method(props.collection, document)
+ if (newDocument?.error)
+ return alert(newDocument.error.message)
-async function updateDocument() {
- // TODO: validate & show errors
- await rpc.updateDocument(props.collection, selectedDocument.value)
- editing.value = false
- selectedDocument.value = undefined
- documents.value = await rpc.listDocuments(props.collection, pagination)
+ if (create) {
+ if (!documents.value.length) {
+ documents.value = await rpc.listDocuments(props.collection, pagination)
+ return discardEditing()
+ }
+ documents.value.push({ _id: newDocument.insertedId, ...document })
+ }
+ else {
+ const index = documents.value.findIndex((doc: any) => doc._id === newDocument.value._id)
+ documents.value[index] = document
+ }
+ discardEditing()
}
function discardEditing() {
@@ -99,8 +104,11 @@ function discardEditing() {
}
async function deleteDocument(document: any) {
- rpc.deleteDocument(props.collection, document._id)
- documents.value = await rpc.listDocuments(props.collection, pagination)
+ const newDocument = await rpc.deleteDocument(props.collection, document._id)
+ if (newDocument.deletedCount === 0)
+ return alert('Failed to delete document')
+
+ documents.value = documents.value.filter((doc: any) => doc._id !== document._id)
}
const copy = useCopy()
@@ -163,7 +171,7 @@ const copy = useCopy()
-
+
diff --git a/src/server-rpc/database.ts b/src/server-rpc/database.ts
index 8c415eb..177015a 100644
--- a/src/server-rpc/database.ts
+++ b/src/server-rpc/database.ts
@@ -1,4 +1,3 @@
-import { logger } from '@nuxt/kit'
import mongoose from 'mongoose'
import type { NuxtDevtoolsServerContext, ServerFunctions } from '../types'
@@ -10,24 +9,54 @@ export function setupDatabaseRPC({ options }: NuxtDevtoolsServerContext): any {
return mongoose.connection.readyState
},
async createCollection(name: string) {
- return await mongoose.connection.db.createCollection(name)
+ try {
+ return await mongoose.connection.db.createCollection(name)
+ }
+ catch (error) {
+ return ErrorIT(error)
+ }
},
async listCollections() {
- return await mongoose.connection.db.listCollections().toArray()
+ try {
+ return await mongoose.connection.db.listCollections().toArray()
+ }
+ catch (error) {
+ return ErrorIT(error)
+ }
},
async getCollection(name: string) {
- return mongoose.connection.db.collection(name)
+ try {
+ return await mongoose.connection.db.collection(name).findOne()
+ }
+ catch (error) {
+ return ErrorIT(error)
+ }
},
async dropCollection(name: string) {
- return await mongoose.connection.db.collection(name).drop()
+ try {
+ return await mongoose.connection.db.dropCollection(name)
+ }
+ catch (error) {
+ return ErrorIT(error)
+ }
},
async createDocument(collection: string, data: any) {
const { _id, ...rest } = data
- return await mongoose.connection.db.collection(collection).insertOne(rest)
+ try {
+ return await mongoose.connection.db.collection(collection).insertOne(rest)
+ }
+ catch (error: any) {
+ return ErrorIT(error)
+ }
},
async countDocuments(collection: string) {
- return await mongoose.connection.db.collection(collection).countDocuments()
+ try {
+ return await mongoose.connection.db.collection(collection).countDocuments()
+ }
+ catch (error) {
+ return ErrorIT(error)
+ }
},
async listDocuments(collection: string, options: { page: number; limit: number } = { page: 1, limit: 10 }) {
const skip = (options.page - 1) * options.limit
@@ -36,8 +65,13 @@ export function setupDatabaseRPC({ options }: NuxtDevtoolsServerContext): any {
cursor.limit(options.limit)
return await cursor.toArray()
},
- async getDocument(collection: string, document: {}) {
- return await mongoose.connection.db.collection(collection).findOne({ document })
+ async getDocument(collection: string, document: any) {
+ try {
+ return await mongoose.connection.db.collection(collection).findOne({ document })
+ }
+ catch (error) {
+ return ErrorIT(error)
+ }
},
async updateDocument(collection: string, data: any) {
const { _id, ...rest } = data
@@ -45,12 +79,25 @@ export function setupDatabaseRPC({ options }: NuxtDevtoolsServerContext): any {
return await mongoose.connection.db.collection(collection).findOneAndUpdate({ _id: new mongoose.Types.ObjectId(_id) }, { $set: rest })
}
catch (error) {
- logger.log(error)
- return error
+ return ErrorIT(error)
}
},
async deleteDocument(collection: string, id: string) {
- return await mongoose.connection.db.collection(collection).deleteOne({ _id: new mongoose.Types.ObjectId(id) })
+ try {
+ return await mongoose.connection.db.collection(collection).deleteOne({ _id: new mongoose.Types.ObjectId(id) })
+ }
+ catch (error) {
+ return ErrorIT(error)
+ }
},
} satisfies Partial
}
+
+function ErrorIT(error: any) {
+ return {
+ error: {
+ message: error?.message,
+ code: error?.code,
+ },
+ }
+}
diff --git a/src/server-rpc/resource.ts b/src/server-rpc/resource.ts
index cc53b04..a525674 100644
--- a/src/server-rpc/resource.ts
+++ b/src/server-rpc/resource.ts
@@ -1,11 +1,10 @@
import fs from 'fs-extra'
import { resolve } from 'pathe'
-import mongoose from 'mongoose'
import type { Collection, NuxtDevtoolsServerContext, Resource, ServerFunctions } from '../types'
import { generateApiRoute, generateSchemaFile } from '../utils/schematics'
import { capitalize, pluralize, singularize } from '../utils/formatting'
-export function setupResourceRPC({ nuxt }: NuxtDevtoolsServerContext): any {
+export function setupResourceRPC({ nuxt, rpc }: NuxtDevtoolsServerContext): any {
return {
// TODO: maybe separate functions
async generateResource(collection: Collection, resources: Resource[]) {
@@ -45,8 +44,9 @@ export function setupResourceRPC({ nuxt }: NuxtDevtoolsServerContext): any {
}
// create collection if not exists
- if (!mongoose.connection.modelNames().includes(dbName))
- await mongoose.connection.db.createCollection(plural)
+ const collections = await rpc.functions.listCollections()
+ if (!collections.find((c: any) => c.name === plural))
+ await rpc.functions.createCollection(plural)
},
async resourceSchema(collection: string) {
// TODO: use magicast
|