-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { PrismaClient } from '@prisma/client' | ||
const prisma = new PrismaClient() | ||
|
||
export default defineEventHandler(async event => { | ||
if (event.node.req.method == 'GET') { | ||
|
||
if (event.node.req.query.id) { | ||
const ${name} = await prisma.${name}.findUnique({ where: { id: event.node.req.query.id } }) | ||
return ${name} | ||
} | ||
|
||
const ${name} = await prisma.${name}.findMany() | ||
return ${name} | ||
} | ||
|
||
if (event.node.req.method == 'POST') { | ||
const data = await readBody(event) | ||
const ${name} = await prisma.${name}.create({ data }) | ||
if (!${name}) { | ||
throw createError({ status: 401, message: '${name} creation error' }) | ||
} | ||
return ${name} | ||
} | ||
|
||
if (event.node.req.method == 'PATCH') { | ||
const data = await readBody(event) | ||
const ${name} = await prisma.${name}.update({ where:{ id:d ata.id }, data }) | ||
if (!${name}) { | ||
throw createError({ status: 401, message: '${name} patch error' }) | ||
} | ||
return ${name} | ||
} | ||
|
||
if (event.node.req.method == 'DELETE') { | ||
const ${name} = await prisma.${name}.delete({ where:{ id:d ata.id }}) | ||
if (!${name}) { | ||
throw createError({ status: 401, message: '${name} delete error' }) | ||
} | ||
return ${name} | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters