nextjs+typescript 기반 CMS 개발 프로젝트
[prisma+nextjs] api 서비스 개발하기
개발자 고포고
2022. 3. 12. 19:52
반응형
[prisma+nextjs] api 서비스 개발하기
nextjs와 prisma가 설치된 환경에서 api 서비스 개발하는 방법을 알아본다.
만약에 nextjs 설치 및 prisma 설치부터 풀로 진행과정이 궁금하시면 댓글로 달아주시면 블로깅을 진행하겠습니다.
#prisma client 설치
-설치 시 현재 시점에선 collection is not iterable 버그가 존재하기때문에 최신버전이 아닌 3.9.2 버전을 설치하여준다.
$ npm i @prisma/client@3.9.2
#client 객체 생성 및 관리를 위한 client.ts 작성
import { PrismaClient } from "@prisma/client";
export default new PrismaClient();
#scheme.prisma에 데이터베이스 스키마와 동기화 할 객체 생성
model User{
id Int @id @default(autoincrement())
phone Int? @unique
email String? @unique
name String
createAt DateTime @default(now())
updateAt DateTime @updatedAt
}
#client-t.tsx api 폴더 생성 후 api 등록
import { NextApiRequest, NextApiResponse } from "next";
import client from "../../libs/client";
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
await client.user.create({
data: {
name: "helloworld",
},
});
res.json({
ok: true,
test:"wow"
});
}
#결과 확인
손쉽게 nextjs에서 서버 환경의 api를 개발 할 수 있다.
#nextjs #api #web service #rest #graphql #prisma #client #prismaclient #typescript
반응형