REST API
概觀
本升級指南說明如何遷移一個基於 Prisma 1 並使用 Prisma 1 client 來實作 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)
})