Next.js 프로젝트에 Prisma DB 연동하기

BellBoy·2024년 1월 12일
0
post-custom-banner
model Inquiry {
  id         Int      @id @default(autoincrement())
  name       String
  email      String
  content    String
  created_at DateTime @default(now()) @db.Timestamptz(6)
  is_mailing Boolean?
}
import { PrismaClient } from "@prisma/client";

export const db =  new PrismaClient();
"use client"

export default function Inquiry() {

  const name = "이일삼";
  const email = "twooneth@naver.com";
  const content = "안녕하세요";


  const handleCreate = async () => {
    const inquiry = await createInquiry({ name, email, content });
    
    console.log(inquiry);
  };


  return (
    <DefaultLayout>
      <div className="container grid gap-12 py-10">
        <button onClick={handleCreate}> test button</button>
      </div>
    </DefaultLayout>
  );

}
"use server"

import { inquiryService } from "@/services/inquiry.service";

interface ICreateInquiryDto {
  name: string;
  email: string;
  content: string;
}

export const createInquiry = async ({ name, email, content }: ICreateInquiryDto) => {

  console.log("action",name, email, content)

  // 메일 보내야하는곳

  const createdInquiry = await inquiryService.createInquiry({
    name,
    email,
    content,
  });

  return createdInquiry;

};

import { Prisma } from "@prisma/client";
import { db } from "@/utils/db";

class InquiryService {

  async createInquiry(dto: Prisma.InquiryCreateInput) {

    console.log("service", dto);
    try {
      const inquiry = await db.inquiry.create({
        data: dto,
      });

      return inquiry;

    }catch (e){
      console.log(e);
    }



  }
}


export const inquiryService = new InquiryService();
profile
리액트러버
post-custom-banner

0개의 댓글