REST
REST API를 구축할 때 프리즈마 클라이언트를 경로 컨트롤러 내에서 사용하여 데이터베이스 질의를 보낼 수 있습니다.
지원되는 라이브러리
프리즈마 클라이언트는 데이터베이스에 질의를 보내는 책임만 가지고 있으므로 원하는 모든 HTTP 서버 라이브러리나 웹 프레임워크와 결합할 수 있습니다.
다음은 프리즈마와 함께 사용할 수 있는 라이브러리 및 프레임워크 목록입니다.
REST API 서버 예시
다음과 같은 프리즈마 스키마가 있다고 가정하겠습니다.
prisma
datasource db {provider = "sqlite"url = "file:./dev.db"}generator client {provider = "prisma-client-js"}model Post {id Int @id @default(autoincrement())title Stringcontent String?published Boolean @default(false)author User? @relation(fields: [authorId], references: [id])authorId Int?}model User {id Int @id @default(autoincrement())email String @uniquename String?posts Post[]}
prisma
datasource db {provider = "sqlite"url = "file:./dev.db"}generator client {provider = "prisma-client-js"}model Post {id Int @id @default(autoincrement())title Stringcontent String?published Boolean @default(false)author User? @relation(fields: [authorId], references: [id])authorId Int?}model User {id Int @id @default(autoincrement())email String @uniquename String?posts Post[]}
이제 생성된 프리즈마 클라이언트 API를 사용하여 HTTP 요청이 들어올 때 데이터베이스 작업을 수행하는 경로 컨트롤러(예: Express 사용)를 구현할 수 있습니다.
아래에는 몇 가지 샘플 코드 조각이 있습니다. 이 코드 조각을 실행하고 싶다면 REST API 예시를 사용할 수 있습니다.
GET
js
app.get('/feed', async (req, res) => {const posts = await prisma.post.findMany({where: { published: true },include: { author: true },});res.json(posts);});
js
app.get('/feed', async (req, res) => {const posts = await prisma.post.findMany({where: { published: true },include: { author: true },});res.json(posts);});
이 경우 feed
엔드포인트는 중첩된 JSON 응답, author
객체가 포함된 Post
객체를 반환합니다. 샘플 응답은 다음과 같습니다.
json
[{"id": "21","title": "Hello World","content": "null","published": "true","authorId": 42,"author": {"id": "42","name": "Alice","email": "alice@prisma.io"}}]
json
[{"id": "21","title": "Hello World","content": "null","published": "true","authorId": 42,"author": {"id": "42","name": "Alice","email": "alice@prisma.io"}}]
POST
js
app.post(`/post`, async (req, res) => {const { title, content, authorEmail } = req.body;const result = await prisma.post.create({data: {title,content,published: false,author: { connect: { email: authorEmail } },},});res.json(result);});
js
app.post(`/post`, async (req, res) => {const { title, content, authorEmail } = req.body;const result = await prisma.post.create({data: {title,content,published: false,author: { connect: { email: authorEmail } },},});res.json(result);});
PUT
js
app.put('/publish/:id', async (req, res) => {const { id } = req.params;const post = await prisma.post.update({where: { id: Number(id) },data: { published: true },});res.json(post);});
js
app.put('/publish/:id', async (req, res) => {const { id } = req.params;const post = await prisma.post.update({where: { id: Number(id) },data: { published: true },});res.json(post);});
DELETE
js
app.delete(`/post/:id`, async (req, res) => {const { id } = req.params;const post = await prisma.post.delete({where: {id: Number(id),},});res.json(post);});
js
app.delete(`/post/:id`, async (req, res) => {const { id } = req.params;const post = await prisma.post.delete({where: {id: Number(id),},});res.json(post);});