next crud
v4.0.1
next-crud是一個輔助庫,它基於Next.js的Prisma模型創建CRUD API路由。
從LIB的版本3開始,我們僅支持Prisma V5及以上。如果您使用的是Prisma V4或以上,請安裝LIB的版本2。
該文檔可在此處提供
yarn add @premieroctet/next-crud
鑑於以下Prisma模式:
model User {
id Int @id @default(autoincrement())
name String?
email String?
}創建file /pages/api/[...nextcrud].ts.和:
import NextCrud , { PrismaAdapter } from '@premieroctet/next-crud'
import { NextApiRequest , NextApiResponse } from 'next'
const handler = async ( req : NextApiRequest , res : NextApiResponse ) => {
const nextCrudHandler = await NextCrud ( {
adapter : new PrismaAdapter ( {
prismaClient : myPrismaClientInstance ,
} ) ,
} )
return nextCrudHandler ( req , res )
}
export default handler並獲取您的全部特色Crud路線!
| 端點 | 描述 | |
|---|---|---|
| 列表 | 獲取/api/users | 獲取所有用戶 |
| 得到 | 獲取/api/users/[id] | 獲取一個用戶 |
| 添加 | 發布/api/users | 創建一個用戶 |
| 編輯 | put /api/users/[id] | 更新一個用戶 |
| 部分編輯 | 補丁/api/users/[id] | 更新一個用戶(部分) |
| 刪除 | 刪除/api/users/[id] | 刪除一個用戶 |
您可以運行示例項目以測試您的修改。確保在根文件夾中運行yarn watch 。
該項目是由Premier Octet開發的,Premier Octet是一家專門從事React和React Native Developtments的網絡和移動機構。