跳到主内容

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 http://localhost:3000')
)

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

1.1. 调整导入

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

import { PrismaClient } from '@prisma/client'

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

const prisma = new PrismaClient()

1.2. 调整 /user 路由(POST

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

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,/publish/:id 路由的 PUT 请求必须更改为

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,/post/:id 路由的 DELETE 请求必须更改为

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,/post/:id 路由的 GET 请求必须更改为

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,/feed 路由的 GET 请求必须更改为

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,/user 路由的 POST 请求必须更改为

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)
})
© . All rights reserved.