剩余 API
概述
¥Overview
本升级指南介绍如何迁移基于 Prisma 1 并使用 Prisma 1 客户端 实现 REST API 的 Node.js 项目。
¥This upgrade guide describes how to migrate a Node.js project that's based on Prisma 1 and uses the Prisma 1 client to implement a REST API.
本指南假定你已经完成了 Prisma ORM 层升级指南。这意味着你已经:
¥The guide assumes that you already went through the guide for upgrading the Prisma ORM layer. This means you already:
-
安装 Prisma ORM 2 CLI
¥installed the Prisma ORM 2 CLI
-
创建了你的 Prisma ORM 2 架构
¥created your Prisma ORM 2 schema
-
内省你的数据库并解决潜在的架构不兼容性
¥introspected your database and resolved potential schema incompatibilities
-
安装并生成 Prisma 客户端
¥installed and generated Prisma Client
该指南进一步假设你有一个与此类似的文件设置:
¥The guide further assumes that you have a file setup that looks similar to this:
.
├── README.md
├── package-lock.json
├── package.json
├── prisma
│ ├── datamodel.prisma
│ ├── docker-compose-mysql.yml
│ ├── docker-compose.yml
│ ├── prisma.yml
│ └── seed.graphql
├── src
│ ├── generated
│ │ └── prisma-client
│ │ ├── index.ts
│ │ └── prisma-schema.ts
│ └── index.ts
└── tsconfig.json
重要的部分是:
¥The important parts are:
-
使用 Prisma ORM 2 架构以
prisma
调用的文件夹¥A folder called with
prisma
with your Prisma ORM 2 schema -
名为
src
的文件夹,其中包含你的应用代码¥A folder called
src
with your application code
如果这不是你的项目结构,你需要调整指南中的说明以匹配你自己的设置。
¥If this is not what your project structure looks like, you'll need to adjust the instructions in the guide to match your own setup.
1. 调整应用以使用 Prisma Client 2
¥ Adjust the application to use Prisma Client 2
出于本指南的目的,我们将使用 prisma1-examples
存储库中 express
示例中的示例 API 调用。
¥For the purpose of this guide, we'll use the sample API calls from the express
example in the prisma1-examples
repository.
我们示例中的应用代码位于单个文件中,如下所示:
¥The application code in our example is located in a single file and looks as follows:
import * as express from 'express'
import * as bodyParser from 'body-parser'
import { prisma } from './generated/prisma-client'
const app = express()
app.$use(bodyParser.json())
app.post(`/user`, async (req, res) => {
const result = await prisma.createUser({
...req.body,
})
res.json(result)
})
app.post(`/post`, async (req, res) => {
const { title, content, authorEmail } = req.body
const result = await prisma.createPost({
title: title,
content: content,
author: { connect: { email: authorEmail } },
})
res.json(result)
})
app.put('/publish/:id', async (req, res) => {
const { id } = req.params
const post = await prisma.updatePost({
where: { id },
data: { published: true },
})
res.json(post)
})
app.delete(`/post/:id`, async (req, res) => {
const { id } = req.params
const post = await prisma.deletePost({ id })
res.json(post)
})
app.get(`/post/:id`, async (req, res) => {
const { id } = req.params
const post = await prisma.post({ id })
res.json(post)
})
app.get('/feed', async (req, res) => {
const posts = await prisma.post({ where: { published: true } })
res.json(posts)
})
app.get('/filterPosts', async (req, res) => {
const { searchString } = req.query
const draftPosts = await prisma.post({
where: {
OR: [
{
title_contains: searchString,
},
{
content_contains: searchString,
},
],
},
})
res.json(draftPosts)
})
app.listen(3000, () =>
console.log('Server is running on http://localhost:3000')
)
考虑每次出现的 Prisma 客户端实例 prisma
并替换为 Prisma 客户端 2 的相应用法。你可以在 API 参考 中了解更多信息。
¥Consider each occurrence of the Prisma Client instance prisma
and replacing with the respective usage of Prisma Client 2. You can learn more in the API Reference.
1.1.调整导入
¥1.1. Adjusting the import
导入生成的 @prisma/client
节点模块,如图:
¥Import the generated @prisma/client
node module as shown:
import { PrismaClient } from '@prisma/client'
请注意,这仅导入 PrismaClient
构造函数,因此你还需要实例化 Prisma Client 2 实例:
¥Note that this only imports the PrismaClient
constructor, so you also need to instantiate a Prisma Client 2 instance:
const prisma = new PrismaClient()
1.2.调整 /user
路由(POST
)
¥1.2. Adjusting the /user
route (POST
)
使用 Prisma Client 2 API,POST
请求的 /user
路由必须更改为:
¥With the Prisma Client 2 API, the /user
route for POST
requests has to be changed to:
app.post(`/user`, async (req, res) => {
const result = await prisma.user.create({
data: {
...req.body,
},
})
res.json(result)
})
1.3.调整 /post
路由(POST
)
¥1.3. Adjusting the /post
route (POST
)
使用 Prisma Client 2 API,POST
请求的 /post
路由必须更改为:
¥With the Prisma Client 2 API, the /post
route for POST
requests has to be changed to:
app.post(`/post`, async (req, res) => {
const { title, content, authorEmail } = req.body
const result = await prisma.post.create({
data: {
title: title,
content: content,
author: { connect: { email: authorEmail } },
},
})
res.json(result)
})
1.4.调整 /publish/:id
路由(PUT
)
¥1.4. Adjusting the /publish/:id
route (PUT
)
使用 Prisma Client 2 API,PUT
请求的 /publish/:id
路由必须更改为:
¥With the Prisma Client 2 API, the /publish/:id
route for PUT
requests has to be changed to:
app.put('/publish/:id', async (req, res) => {
const { id } = req.params
const post = await prisma.post.update({
where: { id },
data: { published: true },
})
res.json(post)
})
1.5.调整 /post/:id
路由(DELETE
)
¥1.5. Adjusting the /post/:id
route (DELETE
)
使用 Prisma Client 2 API,DELETE
请求的 //post/:id
路由必须更改为:
¥With the Prisma Client 2 API, the //post/:id
route for DELETE
requests has to be changed to:
app.delete(`/post/:id`, async (req, res) => {
const { id } = req.params
const post = await prisma.post.delete({
where: { id },
})
res.json(post)
})
1.6.调整 /post/:id
路由(GET
)
¥1.6. Adjusting the /post/:id
route (GET
)
使用 Prisma Client 2 API,GET
请求的 /post/:id
路由必须更改为:
¥With the Prisma Client 2 API, the /post/:id
route for GET
requests has to be changed to:
app.get(`/post/:id`, async (req, res) => {
const { id } = req.params
const post = await prisma.post.findUnique({
where: { id },
})
res.json(post)
})
1.7.调整 /feed
路由(GET
)
¥1.7. Adjusting the /feed
route (GET
)
使用 Prisma Client 2 API,GET
请求的 /feed
路由必须更改为:
¥With the Prisma Client 2 API, the /feed
route for GET
requests has to be changed to:
app.get('/feed', async (req, res) => {
const posts = await prisma.post.findMany({ where: { published: true } })
res.json(posts)
})
1.8.调整 /filterPosts
路由(GET
)
¥1.8. Adjusting the /filterPosts
route (GET
)
使用 Prisma Client 2 API,POST
请求的 /user
路由必须更改为:
¥With the Prisma Client 2 API, the /user
route for POST
requests has to be changed to:
app.get('/filterPosts', async (req, res) => {
const { searchString } = req.query
const filteredPosts = await prisma.post.findMany({
where: {
OR: [
{
title: { contains: searchString },
},
{
content: { contains: searchString },
},
],
},
})
res.json(filteredPosts)
})