prisma client API, ์‚ฌ์šฉ ์˜ˆ์‹œ

21550 ๋‹จ์–ด prismaprisma

index

  • data
  • where
  • select
  • connect
  • include

data [create, update]

record๋ฅผ createํ•˜๊ฑฐ๋‚˜ update ํ•  ๋•Œ ๋“ค์–ด๊ฐˆ key์™€ value์˜ ๊ธฐ์ž…ํ•  ๋•Œ ์‚ฌ์šฉ

const result = await client.product.create({
  data: {
    name : "",
    price: 12, 
    description: "",
    imgae: "",
  },
});

where [read]

๊ฒ€์ƒ‰ํ•˜๊ณ ์ž ํ•˜๋Š” ์ปฌ๋Ÿผ์„ ์ง€์ •ํ•˜๊ณ ์ž ํ•  ๋•Œ ์‚ฌ์šฉ

// ํŠน์ • ์ปฌ๋Ÿผ์— ๊ตฌ๋ถ„์ง“์ง€ ์•Š๊ณ , ๋ชจ๋“  product์„ ์กฐํšŒํ•˜๊ฒ ๋‹ค.
// "where์ด ์—†์œผ๋ฉด ๋ชจ๋“  ๋Œ€์ƒ์—์„œ ์กฐํšŒํ•˜๊ฒ ๋‹ค" ๋ผ๋Š” ์˜๋ฏธ
const result = await client.product.findMany({}); 

// product์˜ ์ปฌ๋Ÿผ ์ค‘์— id๊ฐ€ 2์ธ ๋ชจ๋“  ๊ฒƒ์„ ์กฐํšŒํ•˜๊ฒ ๋‹ค.
const result = await client.product.findMany({
  where: {
    id: 2
  }
}); 

// product์˜ ์ปฌ๋Ÿผ ์ค‘์— name์ด '๊ฐค๋Ÿญ์‹œ'์ด๊ณ , price๊ฐ€ 123 ์ธ ๊ฒƒ์„ ๋ชจ๋‘ ์กฐํšŒํ•˜๊ฒ ๋‹ค.
const result = await client.product.findMany({
  where: {
    name: '๊ฐค๋Ÿญ์‹œ',
    price: 123,
  }
});

select [read]

where์—์„œ ์„ ํƒํ•œ ๋ฐ์ดํ„ฐ ์ค‘์—์„œ ํŠน์ • key๊ฐ’๋งŒ ์ถ”์ถœํ•˜๊ณ ์ž ํ•  ๋•Œ ์‚ฌ์šฉ

const result = await client.product.findMany({
  where: {
    id: 2,
  }, 
  select: {
    name: true,
    price: true,
  }
});

// { name: "", price: "" }

connect [create, update]

create ๋˜๋Š” update๋ฅผ ํ•  ๋•Œ๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋ฉฐ, ๋ชฉ์ ์€ ๋ ˆ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•  ๋•Œ @relation ๊ด€๊ณ„์— ์žˆ๋Š” table์˜ ์–ด๋– ํ•œ ๊ฐ’๊ณผ ์—ฐ๊ฒฐ์‹œํ‚ฌ ๊ฒƒ์ธ์ง€ ์ง€์ •ํ•˜๊ณ ์ž ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค. ์•„๋ž˜ ์ฝ”๋“œ์—์„œ๋Š” product์„ ์ถ”๊ฐ€ํ•  ๋•Œ, ํ•ด๋‹น product์˜ user, user ์ค‘์—์„œ id๊ฐ€ ์–ด๋–ค ๊ฒƒ์ธ์ง€ ์ง€์ •ํ•œ๋‹ค. ์ด๊ฒƒ์„ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ์–‘์ชฝ์˜ field๊ฐ€ @relation์œผ๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์–ด์•ผ ํ•œ๋‹ค.

const result = await client.product.create({
  data: {
    name : "",
    price: 12, 
    description: "",
    imgae: "",
    user: {
      connect: {
        id: 18,
      }
    }
  },
});

์ƒ์„ฑ๋œ ๊ฒƒ์„ ์กฐํšŒํ•˜๋ฉด, connect์—์„œ ์—ฐ๊ฒฐํ–ˆ๋˜ userId: 18 ์ด ์ •๋ณด๋กœ ํฌํ•จ๋˜์–ด ์žˆ๋Š” ๊ฒƒ์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹จ์ˆœํžˆ userId 18 ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ, user์˜ ์ „์ฒด ์ •๋ณด๋ฅผ ์กฐํšŒ ํ•  ์ˆ˜ ์žˆ๋‹ค.

[
  {
    id: 7,
    createdAt: 2022-03-20T14:24:13.100Z,
    updatedAt: 2022-03-20T14:24:13.101Z,
    userId: 18,
    image: 'xxx',
    name: '',
    price: 12,
    description: '12',
    user: {
      id: 18,
      phone: null,
      email: 'rem9238@kakao.com',
      name: 'Anonymous',
      avatar: null,
      createdAt: 2022-03-01T07:04:26.393Z,
      updatedAt: 2022-03-01T07:04:26.395Z
    }
  }
]

include [read]

๊ด€๊ณ„๋ฅผ ์ด๋ฃจ๊ณ  ์žˆ๋Š” ํ•„๋“œ๋ฅผ ํ˜„์žฌ ์กฐํšŒํ•œ ์ฟผ๋ฆฌ์— ํฌํ•จ์‹œํ‚ฌ ๋•Œ ์‚ฌ์šฉ

const result = await client.product.findMany({
  where: {
    id: 2, 
  },
  include: {
    user: true,
  }
});

์กฐํšŒ ๊ฒฐ๊ณผ : user์™€ product์€ schema์—์„œ @relation ๋˜์–ด ์žˆ์Œ

[
  {
    id: 2,
    createdAt: 2022-03-13T10:46:04.141Z,
    updatedAt: 2022-03-20T12:48:06.522Z,
    userId: 19,
    image: 'xxx',
    name: '๊ฐค๋Ÿญ์‹œ',
    price: 123,
    description: '์ด๊ฒƒ์€ ์ฒซ๋ฒˆ์งธ',
    user: {
      id: 19,
      phone: '01099025911',
      email: null,
      name: 'Anonymous',
      avatar: null,
      createdAt: 2022-03-02T15:06:12.018Z,
      updatedAt: 2022-03-02T15:06:12.019Z
    }
  }
]

+select์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๊ธฐ

const result = await client.product.findMany({
  where: {
    id: 2, 
  }, 
  include: {
    user: {
      select: {
        name: true,
        id: true,
      }
    }
  }
});

์กฐํšŒ ๊ฒฐ๊ณผ

[
  {
    id: 2,
    createdAt: 2022-03-13T10:46:04.141Z,
    updatedAt: 2022-03-20T12:48:06.522Z,
    userId: 19,
    image: 'xxx',
    name: '๊ฐค๋Ÿญ์‹œ',
    price: 123,
    description: '์ด๊ฒƒ์€ ์ฒซ๋ฒˆ์งธ',
    user: { id: 19, name: 'Anonymous' }
  }
]

connectOrCreate

๊ณ ์œ  id ์— ์˜ํ•ด ๊ธฐ์กด ๋ ˆ์ฝ”๋“œ๋ฅผ ์—ฐ๊ฒฐํ•˜๊ฑฐ๋‚˜ ๋˜๋Š” ๋ ˆ์ฝ”๋“œ๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด, ๋ ˆ์ฝ”๋“œ๋ฅผ ์ƒˆ๋กœ ์ƒ์„ฑํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

// user : { phone: phone } ๋˜๋Š” { email: email } ==> phone๊ณผ email ๋ชจ๋‘ `@unique`

const token = await client.token.create({
    data: {
      payload, // token number
      // token์˜ user ์ •๋ณด๋Š” where ์ •๋ณด๋ฅผ ์กฐํšŒ ํ–ˆ์„ ๋•Œ 
      // ์ด๋ฏธ ์žˆ์œผ๋ฉด, connect ํ•˜๊ณ 
      // ์—†์œผ๋ฉด, { name: '', ...user } ๋กœ user๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.
      // token ์ƒ์„ฑ ์ฟผ๋ฆฌ์—์„œ user๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜๋„ ์žˆ๋‹ค.
      user: {
        connectOrCreate: {
          where: {
            ...user,
          },
          create: {
            name: "Anonymous",
            ...user,
          },
        },
      },
    },
  });

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ