跳到主要内容

REST API

概述

本升级指南介绍了如何迁移基于 Prisma 1 并使用 Prisma 1 客户端 来实现 REST API 的 Node.js 项目。

本指南假设您已经完成了升级 Prisma ORM 层的指南。这意味着您已经

  • 安装了 Prisma ORM 2 CLI
  • 创建了您的 Prisma ORM 2 schema
  • 内省了您的数据库并解决了潜在的 schema 不兼容性
  • 安装并生成了 Prisma Client

本指南进一步假设您的文件设置与此类似

.
├── 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

重要的部分是

  • 一个名为 prisma 的文件夹,其中包含您的 Prisma ORM 2 schema
  • 一个名为 src 的文件夹,其中包含您的应用程序代码

如果您的项目结构不是这样,您需要调整指南中的说明以匹配您自己的设置。

1. 调整应用程序以使用 Prisma Client 2

为了本指南的目的,我们将使用 express 示例 API 调用,该示例位于 prisma1-examples 仓库中。

我们示例中的应用程序代码位于单个文件中,如下所示

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 https://127.0.0.1:3000')
)

考虑 Prisma Client 实例 prisma 的每次出现,并将其替换为 Prisma Client 2 的相应用法。您可以在 API 参考中了解更多信息。

1.1. 调整导入

导入生成的 @prisma/client node 模块,如下所示

import { PrismaClient } from '@prisma/client'

请注意,这仅导入了 PrismaClient 构造函数,因此您还需要实例化 Prisma Client 2 实例

const prisma = new PrismaClient()

1.2. 调整 /user 路由 (POST)

使用 Prisma Client 2 API,POST 请求的 /user 路由必须更改为

app.post(`/user`, async (req, res) => {
const result = await prisma.user.create({
data: {
...req.body,
},
})
res.json(result)
})

1.3. 调整 /post 路由 (POST)

使用 Prisma Client 2 API,POST 请求的 /post 路由必须更改为

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)

使用 Prisma Client 2 API,PUT 请求的 /publish/:id 路由必须更改为

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)

使用 Prisma Client 2 API,DELETE 请求的 //post/:id 路由必须更改为

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)

使用 Prisma Client 2 API,GET 请求的 /post/:id 路由必须更改为

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)

使用 Prisma Client 2 API,GET 请求的 /feed 路由必须更改为

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

1.8. 调整 /filterPosts 路由 (GET)

使用 Prisma Client 2 API,POST 请求的 /user 路由必须更改为

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)
})