๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Side project

[ํ”Œ์ ๊ธฐ๋ก] Prisma Seeding ๐ŸŒฑ

์š”์ฆ˜ Prisma๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์‚ฌ์ด๋“œ ํ”„๋กœ์ ํŠธ๋ฅผ ์ง„ํ–‰์ค‘์ธ๋ฐ ์ฒ˜์Œ ์‚ฌ์šฉํ•˜๋‹ค๋ณด๋‹ˆ ์ƒˆ๋กœ ๋ฐฐ์šฐ๋Š” ๊ฒƒ๋“ค์ด ๋งŽ๋‹ค.
Seeding๋„ ๊ทธ ์ค‘ ํ•œ ๊ฐ€์ง€์ธ๋ฐ ํŠœํ† ๋ฆฌ์–ผ ๋™์˜์ƒ๊ณผ ๊ธ€๋“ค์„ ๋ณด๋‹ค๋ณด๋‹ˆ seeding, seed๋ผ๋Š” ๋‹จ์–ด๊ฐ€ ์ž์ฃผ ๋‚˜์™”๋‹ค. ๋‹น์žฅ ํ•„์š”ํ•ด๋ณด์ด์ง€๋Š” ์•Š์•„์„œ ๋‚˜์ค‘์— ํ•„์š”ํ•  ๋•Œ์— ๋ด์•ผ์ง€ ํ•˜๊ณ  ์ง€๋‚˜์ณค๋Š”๋ฐ ํ•„์š”ํ•œ ์ˆœ๊ฐ„์ด ์™”๋‹ค. (์œ„์—์„œ ์–ธ๊ธ‰ํ–ˆ๋‹ค์‹œํ”ผ Prisma๋ฅผ ์ฒ˜์Œ ์‚ฌ์šฉํ•˜๋‹ค๋ณด๋‹ˆ Prisma, Prisma seeding์— ๋Œ€ํ•œ ๋‚˜์˜ ์ดํ•ด๊ฐ€ ์ œํ•œ์ ์ผ ์ˆ˜ ์žˆ๊ณ  ์ด ๊ธ€์ด ๊ณต์‹ ๋ฌธ์„œ์— ๊ธฐ์ดˆํ•ด ์žˆ๊ธดํ•˜์ง€๋งŒ ๋” ์ •ํ™•ํ•œ ๋‚ด์šฉ์€ ๊ณต์‹ ๋ฌธ์„œ๋ฅผ ์ฐธ๊ณ ํ•˜๊ธฐ ๋ฐ”๋ž€๋‹ค.)

Prisma์˜ ๊ณต์‹ ๋ฌธ์„œ๋ฅผ ๋ณด๊ณ  ์‹ถ๋‹ค๋ฉด ์ด ๋งํฌ๋ฅผ ํ™•์ธํ•˜๋ผ.

 

Seeding your database

Learn how to seed your database using Prisma's integrated seeding functionality and Prisma Client

www.prisma.io

 

Seeding์€ ๋ฌด์—‡์ด๊ณ  ์–ธ์ œ ํ•„์š”ํ•œ๊ฐ€?

Seeding์ด ๋ฌด์—‡์ธ์ง€๋ถ€ํ„ฐ ๊ฐ„๋‹จํžˆ ์‚ดํŽด๋ณด๋ฉด ๊ณต์‹ ๋ฌธ์„œ์—๋Š” ์ด๋ ‡๊ฒŒ ํ•œ ๋ฌธ์žฅ์œผ๋กœ ์„ค๋ช…ํ•œ๋‹ค. 

Seeding allows you to consistently re-create the same data in your database
Seeding์€ ๋‹น์‹ ์˜ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ๊ฐ™์€ ๋ฐ์ดํ„ฐ๋ฅผ ์ผ๊ด€์„ฑ์žˆ๊ฒŒ ์žฌ์ƒ์„ฑํ•˜๋„๋ก ํ•ด์ค๋‹ˆ๋‹ค.

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ผ๋Š” ํ† ์–‘์— ๋ฐ์ดํ„ฐ๋ผ๋Š” ์”จ๋ฅผ ๋ฏธ๋ฆฌ ์‹ฌ์–ด๋†“๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•˜๋Š” ๊ฒƒ ๊ฐ™๊ธฐ๋„ ํ•˜๋‹ค. ์•„๋ฌดํŠผ ๊ณต์‹ ๋ฌธ์„œ์—์„œ๋Š” ๊ณ„์†ํ•ด์„œ seeding์ด ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ๋Š” ๋ถ€๋ถ„์— ๋Œ€ํ•ด ์ด๋ ‡๊ฒŒ ์„ค๋ช…ํ•œ๋‹ค. 

- Populate your database with data that is required for your application to start - for example, a default language or a default currency.
- Provide basic data for validating and using your application in a development environment. This is particularly useful if you are using Prisma Migrate, which sometimes requires resetting your development database.

- ๋‹น์‹ ์˜ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์‹œ์ž‘ํ•˜๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹น์‹ ์˜ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ถ”๊ฐ€ํ•ด์ค€๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด ๊ธฐ๋ณธ ์–ธ์–ด๋‚˜ ๊ธฐ๋ณธ ํ†ตํ™”. 
- ๊ฐœ๋ฐœ ํ™˜๊ฒฝ์—์„œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๊ฒ€์ฆํ•˜๊ณ  ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ๊ธฐ๋ณธ ๋ฐ์ดํ„ฐ๋ฅผ ์ œ๊ณตํ•œ๋‹ค. ์ด๊ฒƒ์€ ๋‹น์‹ ์˜ ๊ฐœ๋ฐœ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์žฌ์„ค์ •ํ•ด์•ผ ํ•˜๋Š” Prisma Migrate๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ํŠนํžˆ ์œ ์šฉํ•˜๋‹ค.

Prisma Migrate์— ๋Œ€ํ•ด์„œ๋Š” ๋‹ค๋ฃจ์ง€ ์•Š๋Š”๋‹ค. ์ „ ๊ธ€์— PlanetScale์—์„œ ๋ธŒ๋žœ์น˜๋ฅผ ์ƒ์„ฑํ•˜๊ณ  deploy requestํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์˜ฌ๋ ธ์—ˆ๋Š”๋ฐ ๊ฐœ๋ฐœํ•˜๋Š” ๊ณผ์ •์— ์—ฌ๋Ÿฌ ๋ธŒ๋žœ์น˜๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ฒฝ์šฐ seeding ๊ธฐ๋Šฅ์„ ํ™œ์šฉํ•˜๋ฉด ํŽธ๋ฆฌํ•˜๊ณ  ๋น ๋ฅด๊ฒŒ ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€ํ•ด์ค„ ์ˆ˜ ์žˆ๊ฒ ๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ค์—ˆ๋‹ค. PlanetScale์—์„œ ๋ธŒ๋žœ์น˜๋ฅผ ์ƒˆ๋กœ ์ƒ์„ฑํ•˜๋ฉด schema๋งŒ ๋ณต์‚ฌ๊ฐ€ ๋˜๊ณ  ๋ฐ์ดํ„ฐ๋Š” ๊ฐ™์ด ๋ณต์‚ฌ๋˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ƒˆ๋กœ์šด ๋ธŒ๋žœ์น˜๋ฅผ ์ƒ์„ฑํ•  ๋•Œ๋งˆ๋‹ค ์ƒˆ๋กœ์šด ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ์–ด์•ผ ํ•  ๊ฒƒ์ด๋‹ค. ์ถ”๊ฐ€ํ•ด์ฃผ์–ด์•ผ ํ•˜๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ์ ๋‹ค๋ฉด Seed ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ž‘์„ฑํ•˜๋Š” ์‹œ๊ฐ„์ด ๋” ์˜ค๋ž˜ ๊ฑธ๋ฆด์ง€ ๋ชจ๋ฅด์ง€๋งŒ ๋ฐ์ดํ„ฐ๊ฐ€ ์ ๋”๋ผ๋„ ๊ณ„์† ๋ฐ˜๋ณตํ•˜์—ฌ ์ถ”๊ฐ€ํ•ด ์ฃผ์–ด์•ผ ํ•˜๋Š” ๊ฒƒ์ด๋ผ๋ฉด ์‹œ๊ฐ„์„ ์กฐ๊ธˆ ๋” ๋“ค์—ฌ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด ๋” ํšจ์œจ์ ์ผ์ง€๋„ ๋ชจ๋ฅธ๋‹ค. 

 

Seeding์€ ์–ด๋–ป๊ฒŒ ํ•˜๋Š” ๊ฒƒ์ธ๊ฐ€? (JavaScript ๊ธฐ์ค€)

1. seed.js ํŒŒ์ผ ์ถ”๊ฐ€ํ•˜๊ธฐ

ํ”„๋กœ์ ํŠธ์—์„œ prisma๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋‹ค๋ฉด /prisma๋ผ๋Š” ๋””๋ ‰ํ† ๋ฆฌ๊ฐ€ ์žˆ์„ ๊ฒƒ์ด๋‹ค. ํ•ด๋‹น ๋””๋ ‰ํ† ๋ฆฌ ์•ˆ์— seed.js ํŒŒ์ผ์„ ์ƒ์„ฑํ•ด์ค€๋‹ค. 

 

2. seed.js ์— ์Šคํฌ๋ฆฝํŠธ ์ž‘์„ฑํ•˜๊ธฐ 

์•„๋ž˜์˜ ์ฝ”๋“œ๋Š” ๊ณต์‹ ๋ฌธ์„œ์— ๋‚˜์™€์žˆ๋Š” ์ƒ˜ํ”Œ ์Šคํฌ๋ฆฝํŠธ์ด๋‹ค.
alice, bob์ด๋ผ๋Š” user๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  ์žˆ๊ณ  ๋ชจ๋‘ upsert๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š”๋ฐ where์— ๋Œ€ํ•ด ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ๋Š” update๋ฅผ ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ์ƒˆ๋กœ ์ƒ์„ฑํ•˜๋Š” ์ฟผ๋ฆฌ๋กœ ์ž์„ธํ•œ ์‚ฌ์šฉ๋ฒ•์€ ์—ฌ๊ธฐ์„œ ํ™•์ธํ•ด๋ณด์ž(๋งํฌ). ์ด ์Šคํฌ๋ฆฝํŠธ๋Š” ์ž์‹ ์˜ ์Šคํ‚ค๋งˆ์™€ ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ์— ๋งž์ถฐ์„œ ์ž‘์„ฑํ•ด์ฃผ๋ฉด ๋œ๋‹ค. 

const { PrismaClient } = require('@prisma/client')
const prisma = new PrismaClient()

async function main() {
  const alice = await prisma.user.upsert({
    where: { email: 'alice@prisma.io' },
    update: {},
    create: {
      email: 'alice@prisma.io',
      name: 'Alice',
      posts: {
        create: {
          title: 'Check out Prisma with Next.js',
          content: 'https://www.prisma.io/nextjs',
          published: true,
        },
      },
    },
  })

  const bob = await prisma.user.upsert({
    where: { email: 'bob@prisma.io' },
    update: {},
    create: {
      email: 'bob@prisma.io',
      name: 'Bob',
      posts: {
        create: [
          {
            title: 'Follow Prisma on Twitter',
            content: 'https://twitter.com/prisma',
            published: true,
          },
          {
            title: 'Follow Nexus on Twitter',
            content: 'https://twitter.com/nexusgql',
            published: true,
          },
        ],
      },
    },
  })
  console.log({ alice, bob })
}

main()
  .catch((e) => {
    console.error(e)
    process.exit(1)
  })
  .finally(async () => {
    await prisma.$disconnect()
  })

์•„๋ž˜๋Š” ๋‚ด๊ฐ€ ์ž‘์„ฑํ•œ seed.js์˜ ์ผ๋ถ€์ด๋‹ค. 

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

async function main() {
  const category = await prisma.category.upsert({
    where: { id: 1 },
    update: {},
    create: {
      name: "Side project",
    },
  });

  const test = await prisma.user.upsert({
    where: { id: 1 },
    update: {},
    create: {
      email: "test@test.com",
      name: "test",
      Article: {
        create: {
          categoryId: 1,
          title: "[ํ”Œ์ ๊ณ„ํš] ์ƒˆ๋กœ์šด ์‚ฌ์ด๋“œ ํ”„๋กœ์ ํŠธ๋ฅผ ์‹œ์ž‘ํ•ด๋ณด์ž",
          content:
            '์ƒˆ๋กœ์šด ์‚ฌ์ด๋“œ ํ”„๋กœ์ ํŠธ๋ฅผ ์‹œ์ž‘ํ•  ์‹œ๊ฐ„์ด๋‹ค. ๋ฌด์—‡์ด๋“  ์ง์ ‘ํ•ด๋ด์•ผ ๋Š๋ผ๋Š” ๊ฒƒ๋„ ๋งŽ๊ณ  ๋ฐฐ์šฐ๋Š” ๊ฒƒ๋„ ๋งŽ์€ ๊ฒƒ ๊ฐ™๋‹ค...',
          ... // ์ƒ๋žต
        },
      },
    },
  });

  console.log({ category, test });
}

main()
  .catch((e) => {
    console.error(e);
    process.exit(1);
  })
  .finally(async () => {
    await prisma.$disconnect();
  });

 

3. seed.js ํŒŒ์ผ์— ๋Œ€ํ•ด package.json์— ์ถ”๊ฐ€ํ•˜๊ธฐ

 

4. Seeding ํ•˜๊ธฐ

์œ„์˜ ์ปค๋งจ๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด seeding์ด ์ง„ํ–‰๋˜๊ณ  ์„ฑ๊ณต์‹œ ์•„๋ž˜์™€ ๊ฐ™์ด ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.