prisma + api routes 연동

Posted by : on

Category : nextJs


prisma + api routes 연동하기

Prisma를 사용해서 모든 데이터를 가져온 후, getServerSideProps에서 해당 API 호출

import type { NextApiRequest, NextApiResponse } from "next";
import { StoreType } from "@interface";
const PrismaClient = require("@prisma/client");

export default async function handler(
  req: NextApiRequest,
  res: NextApiResponse<StoreType[]>
) {
  const prisma = new PrismaClient();
  const stores = await prisma.store.findMany({});

  res.status(200).json(stores);
}
export async function getServerSideProps() {
  const stores = await fetch(
    `${process.env.NEXT_PUBLIC_API_URL}/api/stores`
  ).then((response) => response.json());

  return {
    props: { stores },
  };
}

About 유재석
유재석

개발자 유재석 입니다. Web Developer.

Email : jaeseok9405@gmail.com

Website : https://github.com/yoo94