티스토리 뷰
반응형
[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
반응형
'nextjs+typescript 기반 CMS 개발 프로젝트' 카테고리의 다른 글
[nextjs+ts]프로젝트 06. api request middleware 만들기(nextjs) (0) | 2022.03.13 |
---|---|
[nextjs+ts]프로젝트 05.로그인 페이지 react-hook-form을 활용한 리펙토링 (0) | 2022.03.13 |
[prisma+PlanetScale]스크립트 작성하여 DB(PlanetScale)에 동기화하기 (0) | 2022.03.12 |
[prisma+PlanetScale]database connect 데이터베이스 구성 및 접속하기 (2) | 2022.03.12 |
[nextjs+ts]( 번외) tailwindCSS peer와 group을 활용하여 다른 객체의 상태값 받아오기 / peer와 group의 차이 (0) | 2022.03.09 |
댓글
반응형