Skip to main content

REST

构建 REST API 时,Prisma 客户端可以在路由控制器内使用来发送数据库查询。

¥When building REST APIs, Prisma Client can be used inside your route controllers to send databases queries.

REST APIs with Prisma Client

支持的库

¥Supported libraries

由于 Prisma 客户端是 "only" 负责向你的数据库发送查询,因此它可以与你选择的任何 HTTP 服务器库或 Web 框架结合使用。

¥As Prisma Client is "only" responsible for sending queries to your database, it can be combined with any HTTP server library or web framework of your choice.

以下是可与 Prisma ORM 一起使用的库和框架的非详尽列表:

¥Here's a non-exhaustive list of libraries and frameworks you can use with Prisma ORM:

REST API 服务器示例

¥REST API server example

假设你有一个与此类似的 Prisma 架构:

¥Assume you have a Prisma schema that looks similar to this:

datasource db {
provider = "sqlite"
url = "file:./dev.db"
}

generator client {
provider = "prisma-client-js"
}

model Post {
id Int @id @default(autoincrement())
title String
content String?
published Boolean @default(false)
author User? @relation(fields: [authorId], references: [id])
authorId Int?
}

model User {
id Int @id @default(autoincrement())
email String @unique
name String?
posts Post[]
}

你现在可以实现路由控制器(例如使用 Express),当传入的 HTTP 请求到达时,它使用生成的 Prisma 客户端 API 来执行数据库操作。此页面仅显示一些示例代码片段;如果你想运行这些代码片段,你可以使用 REST API 示例

¥You can now implement route controller (e.g. using Express) that use the generated Prisma Client API to perform a database operation when an incoming HTTP request arrives. This page only shows few sample code snippets; if you want to run these code snippets, you can use a REST API example.

GET

app.get('/feed', async (req, res) => {
const posts = await prisma.post.findMany({
where: { published: true },
include: { author: true },
})
res.json(posts)
})

请注意,本例中的 feed 端点返回包含 author 对象的 Post 对象的嵌套 JSON 响应。这是一个示例响应:

¥Note that the feed endpoint in this case returns a nested JSON response of Post objects that include an author object. Here's a sample response:

[
{
"id": "21",
"title": "Hello World",
"content": "null",
"published": "true",
"authorId": 42,
"author": {
"id": "42",
"name": "Alice",
"email": "alice@prisma.io"
}
}
]

POST

app.post(`/post`, async (req, res) => {
const { title, content, authorEmail } = req.body
const result = await prisma.post.create({
data: {
title,
content,
published: false,
author: { connect: { email: authorEmail } },
},
})
res.json(result)
})

PUT

app.put('/publish/:id', async (req, res) => {
const { id } = req.params
const post = await prisma.post.update({
where: { id: Number(id) },
data: { published: true },
})
res.json(post)
})

DELETE

app.delete(`/post/:id`, async (req, res) => {
const { id } = req.params
const post = await prisma.post.delete({
where: {
id: Number(id),
},
})
res.json(post)
})

准备运行的示例项目

¥Ready-to-run example projects

你可以在 prisma-examples 存储库中找到几个可立即运行的示例,这些示例展示了如何使用 Prisma Client 实现 REST API 以及构建完整的应用。

¥You can find several ready-to-run examples that show how to implement a REST API with Prisma Client, as well as build full applications, in the prisma-examples repository.

示例描述
express仅后端REST API 与 Express for TypeScript
fastify仅后端使用 Fastify 和 Prisma 客户端的 REST API。
hapi仅后端使用 hapi 和 Prisma 客户端的 REST API
nestjs仅后端带有 REST API 的 Nest.js 应用 (Express)
nextjs全栈带有 REST API 的 Next.js 应用 (React)