chore: playground example

This commit is contained in:
arashsheyda
2023-06-05 21:27:49 +03:00
parent 6ca94dc574
commit 3356ffd052
6 changed files with 58 additions and 0 deletions

View File

@ -0,0 +1,8 @@
export default defineEventHandler(async (event) => {
try {
return await UserSchema.findOneAndDelete({ _id: event.context.params?._id })
}
catch (error) {
return error
}
})

View File

@ -0,0 +1,8 @@
export default defineEventHandler(async (event) => {
try {
return await UserSchema.findOne({ _id: event.context.params?._id })
}
catch (error) {
return error
}
})

View File

@ -0,0 +1,9 @@
export default defineEventHandler(async (event) => {
const body = await readBody(event)
try {
return await UserSchema.findOneAndUpdate({ _id: event.context.params?._id }, body, { new: true })
}
catch (error) {
return error
}
})

View File

@ -0,0 +1,9 @@
export default defineEventHandler(async (event) => {
const body = await readBody(event)
try {
return await new UserSchema(body).save()
}
catch (error) {
return error
}
})

View File

@ -0,0 +1,8 @@
export default defineEventHandler(async () => {
try {
return await UserSchema.find({})
}
catch (error) {
return error
}
})

View File

@ -0,0 +1,16 @@
import { defineMongooseModel } from '#nuxt/mongoose'
export const UserSchema = defineMongooseModel({
name: 'User',
schema: {
name: {
type: 'string',
required: true,
},
slug: {
type: 'string',
required: true,
unique: true,
},
},
})