โš’ Backend/Node.js

[Node.js] Prisma CRUD ๊ตฌํ˜„ํ•ด ๋ณด๊ธฐ (feat Prisma Studio)

Fomagran ๐Ÿ’ป 2022. 8. 2. 07:03
728x90
๋ฐ˜์‘ํ˜•

์•ˆ๋…•ํ•˜์„ธ์š”. Foma ์ž…๋‹ˆ๋‹ค.

 

์ €๋ฒˆ ๊ธ€์— Prisma์— ๊ด€ํ•œ ๋‚ด์šฉ๊ณผ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š” ์ด์œ ์— ๋Œ€ํ•ด์„œ ์ •๋ฆฌํ–ˆ๋Š”๋ฐ์š”.

 

(ํ˜น์‹œ ์•ˆ๋ณด์‹  ๋ถ„๋“ค์€ ์—ฌ๊ธฐ ์—์„œ ๋ณด์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค!)

 

์˜ค๋Š˜์€ Prisma๋ฅผ ์ด์šฉํ•˜์—ฌ CRUD๋ฅผ ๊ตฌํ˜„ํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

๋ฐ”๋กœ ์‹œ์ž‘ํ• ๊ฒŒ์š”~


Init Prisma

 

๋นˆ ํด๋”๋ฅผ ๋งŒ๋“ค๊ณ  npm์„ ์ดˆ๊ธฐํ™” ํ•ด์ค๋‹ˆ๋‹ค.

 

npm init -y

 

 

๊ทธ ๋‹ค์Œ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ prisma์™€ ts-node, @types/node๋ฅผ ์„ค์น˜ํ•ด ์ค๋‹ˆ๋‹ค.

 

npm install prisma @prisma/client typescript ts-node @types/node --save-dev

 

 

๊ทธ ๋‹ค์Œ prisma๋ฅผ ์•„๋ž˜ ๋ช…๋ น์–ด๋กœ ์ดˆ๊ธฐํ™” ํ•ด์ค๋‹ˆ๋‹ค.

 

npx prisma init

 

์•„๋ž˜์™€ ๊ฐ™์ด ์ž๋™์œผ๋กœ prisma ํด๋”์™€ env ํŒŒ์ผ์ด ์ƒ์„ฑ๋  ๊ฑฐ์—์š”.

 

 


Set a environment

 

.env ํŒŒ์ผ๋กœ ์ด๋™ํ•˜์…”์„œ ์•„๋ž˜์™€ ๊ฐ™์ด DB ์ •๋ณด๋ฅผ ์ž…๋ ฅํ•ด ์ค๋‹ˆ๋‹ค.

 

DATABASE_URL="๋””๋น„์ข…๋ฅ˜://์œ ์ €์ด๋ฆ„:ํŒจ์Šค์›Œ๋“œ@localhost:ํฌํŠธ๋ฒˆํ˜ธ/๋””๋น„์ด๋ฆ„"

Set a schema.prisma

 

schema.prisma ํŒŒ์ผ๋กœ ์ด๋™ํ•ด์„œ client๋ฅผ ์ƒ์„ฑํ•ด ์ค๋‹ˆ๋‹ค.

 

generator client {
  provider = "prisma-client-js"
}

 

db ๋ฐ์ดํ„ฐ ์†Œ์Šค๋ฅผ .env ํŒŒ์ผ์— ์ž‘์„ฑํ•œ ๋””๋น„ ์ข…๋ฅ˜์™€ DATABASE_URL์„ ์ž…๋ ฅํ•ด ์ค๋‹ˆ๋‹ค.

datasource db {
  provider = "๋””๋น„์ข…๋ฅ˜"
  url      = env("DATABASE_URL")
}

 

๊ทธ ๋‹ค์Œ ํ…Œ์ด๋ธ”์— ์›ํ•˜๋Š” ๋ฐ์ดํ„ฐ ๋ชจ๋ธ์„ ์ •์˜ํ•ด ์ค๋‹ˆ๋‹ค.

 

model User {
  id        Int      @id @default(autoincrement())
  name      String   @unique @db.VarChar(255)
  age       Int      @default(0)
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}

Init User table

 

ํ…Œ์ด๋ธ”์„ ๋งˆ์ด๊ทธ๋ ˆ์ดํŠธ๋ฅผ ์ง„ํ–‰ํ•ด ์ค๋‹ˆ๋‹ค.

 

npx prisma migrate dev --name ํ…Œ์ด๋ธ”์ด๋ฆ„_table_init

 

์•„๋ž˜์™€ ๊ฐ™์ด migrations ํด๋”๊ฐ€ ์ƒ๊ธฐ๊ณ  ํ•ด๋‹น schema ํŒŒ์ผ์—์„œ ์ •์˜ํ•œ ๋ชจ๋ธ์„ ๋งŒ๋“œ๋Š” sql๋ฌธ์ด ์ž‘์„ฑ๋˜์–ด ์žˆ๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

MySQL ์›Œํฌ๋ฒค์น˜๋กœ ๊ฐ€์„œ ํ™•์ธํ•˜๋ฉด ํ•ด๋‹น ๋””๋น„์— ํ…Œ์ด๋ธ”์ด ์ƒ์„ฑ๋˜๊ณ  ์„ค์ •ํ•œ ์ปฌ๋Ÿผ๋“ค์ด ์žˆ๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

 

์•„๋ž˜ ๋ช…๋ น์–ด๋ฅผ ํ„ฐ๋ฏธ๋„์— ์‹คํ–‰ํ•˜๋ฉด migration ํŒŒ์ผ์ด ์ƒ์„ฑ๋˜์ง€ ์•Š๊ณ  DB๊ฐ€ ์ž‘์„ฑํ•ด์ค€ ๋ชจ๋ธ๋Œ€๋กœ ์—…๋ฐ์ดํŠธ ๋ฉ๋‹ˆ๋‹ค.

 

npx prisma db push

Create

 

์—ฌ๋Ÿฌ ๋ฐ์ดํ„ฐ ํ•œ ๋ฒˆ์— ์ƒ์„ฑํ•˜๊ธฐ 

 

prisma.๋ชจ๋ธ์ด๋ฆ„.createMany ๋ฉ”์„œ๋“œ์— ์˜ค๋ธŒ์ ํŠธ ํ˜•์‹์œผ๋กœ ๋œ ์—ฌ๋Ÿฌ data๋ฅผ ๋„ฃ์–ด์ฃผ๋ฉด ํ•œ ๋ฒˆ์— ์—ฌ๋Ÿฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

skipDuplicates๋ฅผ true๋กœ ํ•ด์ฃผ๋ฉด ์ค‘๋ณต๋œ ๋ฐ์ดํ„ฐ๊ฐ€ ์ƒ์„ฑ๋˜๋Š” ๊ฒƒ์„ ๋ง‰์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

import { PrismaClient } from "@prisma/client";
const prisma = new PrismaClient();

async function main() {
  try {
    const data = [
      { name: "Kalid", age: 27 },
      { name: "fomagran", age: 28 },
      { name: "Young", age: 26 },
      { name: "Kalid", age: 27 },
    ];

    const res = await prisma.user.createMany({
      data,
      skipDuplicates:true
    });

    console.log("Created a user sucessfully", res);
  } catch (err) {
    console.log("Create a user Error:", err);
  } finally {
    async () => {
      await prisma.$disconnect();
    };
  }
}

main();

 

๊ทธ ๋‹ค์Œ ts-node๋กœ ํ•ด๋‹น ํŒŒ์ผ์ด ์žˆ๋Š” ๊ฒฝ๋กœ๋ฅผ ์ž…๋ ฅํ•ด์ฃผ๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์ฝ”๋“œ๊ฐ€ ์‹คํ–‰ ๋ฉ๋‹ˆ๋‹ค.

 

 

1๊ฐœ ๋ฐ์ดํ„ฐ๋งŒ ์ถ”๊ฐ€ํ•˜๊ธฐ

 

prisma.๋ชจ๋ธ์ด๋ฆ„.createMany ๋ถ€๋ถ„์˜ data ํ”„๋กœํผํ‹ฐ์— ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ์–ด์ค๋‹ˆ๋‹ค.

 

    const user = { name: "Kendrick", age: 26 };

    const res = await prisma.user.createMany({
      data:user,
    });
    
    ...

 

Read

 

๋ชจ๋“  ๋ฐ์ดํ„ฐ ์ฝ๊ธฐ

 

prisma.๋ชจ๋ธ์ด๋ฆ„.findMany()๋ฅผ ์ด์šฉํ•˜๋ฉด ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

import { PrismaClient } from "@prisma/client";
const prisma = new PrismaClient();

async function main() {
  try {
    const res = await prisma.user.findMany();
    console.log("All users: ", res);
  } catch (err) {
    console.log("Read a user Error:", err);
  } finally {
    async () => {
      await prisma.$disconnect();
    };
  }
}

main();

 

์œ„์—์„œ ์ถ”๊ฐ€ํ•œ ๋ฐ์ดํ„ฐ๋“ค์ด ์žˆ๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

 

์กฐ๊ฑด์„ ๊ฑธ์–ด ํŠน์ • ๋ฐ์ดํ„ฐ๋งŒ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ

 

age๊ฐ€ 27๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์€ ์œ ์ €๋ฅผ ๋ถˆ๋Ÿฌ ์˜ค๊ฒ ์Šต๋‹ˆ๋‹ค. (gte => greater than or equal)

 

import { PrismaClient } from "@prisma/client";
const prisma = new PrismaClient();

async function main() {
  try {
    const res = await prisma.user.findMany({
      where: {
        age: {
          gte: 27,
        },
      },
    });
    console.log("The age is greater than or equal 27 users : ", res);
  } catch (err) {
    console.log("Read a user Error:", err);
  } finally {
    async () => {
      await prisma.$disconnect();
    };
  }
}

main();

 

์•„๋ž˜์™€ ๊ฐ™์ด age๊ฐ€ 27๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์€ ์œ ์ €๊ฐ€ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค.

 

 

Update

 

prisma.๋ชจ๋ธ์ด๋ฆ„.updateMany๋ฅผ ์ด์šฉํ•˜์—ฌ age๊ฐ€ 28์ธ ์‚ฌ๋žŒ์˜ ์ด๋ฆ„์„ oldman์œผ๋กœ ๋ฐ”๊ฟ” ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

import { PrismaClient } from "@prisma/client";
const prisma = new PrismaClient();

async function main() {
  try {
    const res = await prisma.user.updateMany({
      where: {
        age: {
          equals: 28,
        },
      },
      data: {
        name: "oldman",
      },
    });

    console.log("Updated a user sucessfully", res);
  } catch (err) {
    console.log("Update a user Error:", err);
  } finally {
    async () => {
      await prisma.$disconnect();
    };
  }
}

main();

 

 

์•„๋ž˜์™€ ๊ฐ™์ด ๋‚˜์ด๊ฐ€ 28์ธ ์œ ์ €์˜ ์ด๋ฆ„์ด oldman์œผ๋กœ ๋ฐ”๊ปด ์žˆ๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

Delete

 

prisma.๋ชจ๋ธ์ด๋ฆ„.deleteMany๋กœ ๋‚˜์ด๊ฐ€ 28์ธ ์‚ฌ๋žŒ์„ ์‚ญ์ œํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

import { PrismaClient } from "@prisma/client";
const prisma = new PrismaClient();

async function main() {
  try {
    const res = await prisma.user.deleteMany({
      where: {
        age: {
          equals: 28,
        },
      },
    });

    console.log("Deleted a user sucessfully", res);
  } catch (err) {
    console.log("Delete a user Error:", err);
  } finally {
    async () => {
      await prisma.$disconnect();
    };
  }
}

main();

 

 

์•„๋ž˜์™€ ๊ฐ™์ด ๋‚˜์ด๊ฐ€ 28์ธ ์‚ฌ๋žŒ์ด ์‚ญ์ œ๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 


Prisma Studio

 

์œ„ ๊ณผ์ •์„ ๋”์šฑ ์‰ฝ๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋Š”๋ฐ์š”.

 

๋ฐ”๋กœ Prisma Studio๋ฅผ ์ด์šฉํ•˜๋Š” ๊ฒƒ ์ž…๋‹ˆ๋‹ค.

 

์•„๋ž˜์™€ ๊ฐ™์ด ํ”„๋ฆฌ์ฆˆ๋งˆ ์ŠคํŠœ๋””์˜ค๋ฅผ ์‹คํ–‰์‹œ์ผœ ์ค๋‹ˆ๋‹ค.

 

npx prisma studio

 

๊ทธ๋Ÿฌ๋ฉด http:/localhost:5555 ์ฐฝ์ด ์ž๋™์œผ๋กœ ๋œจ๊ฒŒ ๋˜๋Š”๋ฐ์š”.

 

 

๋ฐ”๋กœ ์•„๋ž˜์™€ ๊ฐ™์ด ์œ„์—์„œ ์ƒ์„ฑํ•œ ํ…Œ์ด๋ธ”๋“ค์ด ๋ณด์ด๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

 

 

ํ•ด๋‹น ํ…Œ์ด๋ธ”์„ ํด๋ฆญํ•˜๋ฉด ์ƒ์„ฑํ–ˆ๋˜ ๋ฐ์ดํ„ฐ๋“ค์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

๋กœ์šฐ๋ฅผ ์„ ํƒํ•˜์—ฌ ์‚ญ์ œ ๋˜๋Š” ์ƒ์„ฑํ•  ์ˆ˜๋„ ์žˆ์œผ๋ฉฐ,

 

 

๋˜ํ•œ ์›ํ•˜๋Š” ์ปฌ๋Ÿผ์„ ๋”๋ธ” ํด๋ฆญํ•˜์—ฌ ์ˆ˜์ •ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

์œ„ ์ŠคํŠœ๋””์˜ค์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ”๊พธ๊ณ  ๋‚˜์„œ MySQL ์›Œํฌ๋ฒค์น˜๋กœ ์ด๋™ํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ํ™•์ธํ•ด ๋ณด๋ฉด ์‹ค์‹œ๊ฐ„์œผ๋กœ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ฐ”๋€Œ์–ด ์žˆ๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 


๋Š๋‚€ ์ 

 

์ด์ „ Sequelize, TypeORM์— ๋น„ํ•ด ํ›จ์”ฌ ์ง๊ด€์ ์ด๊ณ  ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ๋” ๊ฐ„ํŽธํ–ˆ๋‹ค.

 

์ด์ „ ๊ธ€์—์„œ Prisma๊ฐ€ ๊ฐœ๋ฐœ์ž์˜ ์ƒ์‚ฐ์„ฑ์„ ์šฐ์„  ๋ชฉํ‘œ๋กœ ํ•œ๋‹ค๊ณ  ํ•˜๋Š” ๊ฒƒ์ด ์–ด๋–ค ์˜๋ฏธ์ธ์ง€ ์•Œ ๊ฒƒ ๊ฐ™๋‹ค.

 

ํŠนํžˆ Prisma Studio๋ฅผ ํ†ตํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐ์ž‘ํ•˜๋Š” ๊ฒƒ์€ ์ •๋ง ํŽธ๋ฆฌํ•˜๊ณ  ์ƒ์‚ฐ์ ์ธ ์ธก๋ฉด์—์„œ ์—„์ฒญ ํšจ์œจ์ ์ธ ๊ฒƒ ๊ฐ™๋‹ค.

728x90
๋ฐ˜์‘ํ˜•