fix: handle errors

This commit is contained in:
arashsheyda
2023-04-26 22:45:40 +03:00
parent 260eadd837
commit e9b764b72f
3 changed files with 86 additions and 31 deletions

View File

@ -78,19 +78,24 @@ function editDocument(document: any) {
selectedDocument.value = { ...document } selectedDocument.value = { ...document }
} }
async function saveDocument(document: any) { async function saveDocument(document: any, create = true) {
await rpc.createDocument(props.collection, document) const method = create ? rpc.createDocument : rpc.updateDocument
editing.value = false const newDocument = await method(props.collection, document)
selectedDocument.value = undefined if (newDocument?.error)
documents.value = await rpc.listDocuments(props.collection, pagination) return alert(newDocument.error.message)
}
async function updateDocument() { if (create) {
// TODO: validate & show errors if (!documents.value.length) {
await rpc.updateDocument(props.collection, selectedDocument.value) documents.value = await rpc.listDocuments(props.collection, pagination)
editing.value = false return discardEditing()
selectedDocument.value = undefined }
documents.value = await rpc.listDocuments(props.collection, pagination) 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() { function discardEditing() {
@ -99,8 +104,11 @@ function discardEditing() {
} }
async function deleteDocument(document: any) { async function deleteDocument(document: any) {
rpc.deleteDocument(props.collection, document._id) const newDocument = await rpc.deleteDocument(props.collection, document._id)
documents.value = await rpc.listDocuments(props.collection, pagination) if (newDocument.deletedCount === 0)
return alert('Failed to delete document')
documents.value = documents.value.filter((doc: any) => doc._id !== document._id)
} }
const copy = useCopy() const copy = useCopy()
@ -163,7 +171,7 @@ const copy = useCopy()
<td class="actions"> <td class="actions">
<div flex justify-center gap2 class="group"> <div flex justify-center gap2 class="group">
<template v-if="editing && selectedDocument._id === document._id"> <template v-if="editing && selectedDocument._id === document._id">
<NIconButton icon="carbon-save" @click="updateDocument" /> <NIconButton icon="carbon-save" @click="saveDocument(selectedDocument, false)" />
<NIconButton icon="carbon-close" @click="discardEditing" /> <NIconButton icon="carbon-close" @click="discardEditing" />
</template> </template>
<template v-else> <template v-else>

View File

@ -1,4 +1,3 @@
import { logger } from '@nuxt/kit'
import mongoose from 'mongoose' import mongoose from 'mongoose'
import type { NuxtDevtoolsServerContext, ServerFunctions } from '../types' import type { NuxtDevtoolsServerContext, ServerFunctions } from '../types'
@ -10,24 +9,54 @@ export function setupDatabaseRPC({ options }: NuxtDevtoolsServerContext): any {
return mongoose.connection.readyState return mongoose.connection.readyState
}, },
async createCollection(name: string) { 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() { 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) { 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) { 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) { async createDocument(collection: string, data: any) {
const { _id, ...rest } = data 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) { 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 }) { async listDocuments(collection: string, options: { page: number; limit: number } = { page: 1, limit: 10 }) {
const skip = (options.page - 1) * options.limit const skip = (options.page - 1) * options.limit
@ -36,8 +65,13 @@ export function setupDatabaseRPC({ options }: NuxtDevtoolsServerContext): any {
cursor.limit(options.limit) cursor.limit(options.limit)
return await cursor.toArray() return await cursor.toArray()
}, },
async getDocument(collection: string, document: {}) { async getDocument(collection: string, document: any) {
return await mongoose.connection.db.collection(collection).findOne({ document }) try {
return await mongoose.connection.db.collection(collection).findOne({ document })
}
catch (error) {
return ErrorIT(error)
}
}, },
async updateDocument(collection: string, data: any) { async updateDocument(collection: string, data: any) {
const { _id, ...rest } = data 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 }) return await mongoose.connection.db.collection(collection).findOneAndUpdate({ _id: new mongoose.Types.ObjectId(_id) }, { $set: rest })
} }
catch (error) { catch (error) {
logger.log(error) return ErrorIT(error)
return error
} }
}, },
async deleteDocument(collection: string, id: string) { 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<ServerFunctions> } satisfies Partial<ServerFunctions>
} }
function ErrorIT(error: any) {
return {
error: {
message: error?.message,
code: error?.code,
},
}
}

View File

@ -1,11 +1,10 @@
import fs from 'fs-extra' import fs from 'fs-extra'
import { resolve } from 'pathe' import { resolve } from 'pathe'
import mongoose from 'mongoose'
import type { Collection, NuxtDevtoolsServerContext, Resource, ServerFunctions } from '../types' import type { Collection, NuxtDevtoolsServerContext, Resource, ServerFunctions } from '../types'
import { generateApiRoute, generateSchemaFile } from '../utils/schematics' import { generateApiRoute, generateSchemaFile } from '../utils/schematics'
import { capitalize, pluralize, singularize } from '../utils/formatting' import { capitalize, pluralize, singularize } from '../utils/formatting'
export function setupResourceRPC({ nuxt }: NuxtDevtoolsServerContext): any { export function setupResourceRPC({ nuxt, rpc }: NuxtDevtoolsServerContext): any {
return { return {
// TODO: maybe separate functions // TODO: maybe separate functions
async generateResource(collection: Collection, resources: Resource[]) { async generateResource(collection: Collection, resources: Resource[]) {
@ -45,8 +44,9 @@ export function setupResourceRPC({ nuxt }: NuxtDevtoolsServerContext): any {
} }
// create collection if not exists // create collection if not exists
if (!mongoose.connection.modelNames().includes(dbName)) const collections = await rpc.functions.listCollections()
await mongoose.connection.db.createCollection(plural) if (!collections.find((c: any) => c.name === plural))
await rpc.functions.createCollection(plural)
}, },
async resourceSchema(collection: string) { async resourceSchema(collection: string) {
// TODO: use magicast // TODO: use magicast