REST API
概要
このアップグレードガイドでは、Prisma 1 に基づいており、Prisma 1 クライアント を使用して REST API を実装する Node.js プロジェクトを移行する方法について説明します。
このガイドは、Prisma ORM レイヤーをアップグレードするためのガイド をすでに読んだことを前提としています。これは、すでに以下のことを行っていることを意味します。
- Prisma ORM 2 CLI をインストール済み
- Prisma ORM 2 スキーマを作成済み
- データベースをイントロスペクションし、潜在的なスキーマの非互換性を解決済み
- 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 ORM 2 スキーマを含む
prisma
という名前のフォルダ - アプリケーションコードを含む
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 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)
})