メインコンテンツにスキップ

REST API

概要

このアップグレードガイドでは、Prisma 1をベースとし、Prisma 1 clientを使用して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を使用するように調整する

このガイドでは、prisma1-examplesリポジトリのexpress例のサンプルAPIコールを使用します。

この例のアプリケーションコードは単一のファイルにあり、以下のようになっています。

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 Clientインスタンスprismaの各出現箇所を、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では、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)
})
© . All rights reserved.