跳到主內容

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://: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)
})
© . This site is unofficial and not affiliated with Prisma Data, Inc.