[Prisma] Prisma client API

jw·2022년 12월 30일
0

Prisma

목록 보기
3/3
post-thumbnail

include

: 현재 조회한 쿼리에 관계를 이루고 있는 필드를 포함시킬 때 사용한다.

const oldPhotoHashtags = await client.photo.findFirst({
	where: { id },
	include: { user: true },
});

조회결과 : 현재 조회한 Photo와 관계를 이루고 있는 User 필드가 포함되었다

{
  id: 6,
  userId: 2,
  file: 'test1',
  caption: '#bye #avocado',
  createdAt: 2022-12-30T11:38:55.975Z,
  updatedAt: 2022-12-30T12:05:42.924Z,
  user: {
    id: 2,
    firstName: 'DD',
    lastName: 'K',
    username: 'dy',
    email: 'kdy.com',
    password: '$2b$10$iOvh9kPy6UrUuAaOOvv43OoI6NA/dvcPee2OP/DVowNVp73PeWKyK',
    bio: 'im supper super',
    avatar: 'http://localhost:4000/static/2-1671703926176-snow.jpeg',
    createdAt: 2022-11-28T07:34:38.389Z,
    updatedAt: 2022-12-22T10:12:06.178Z
  }
}

select

: where에서 선택한 데이터 중에서 특정 key값만 추출하고 싶을 때 사용한다.

const oldPhotoHashtags = await client.photo.findFirst({
	where: { id },
	select: { hashtags: true },
});

출력 결과

{
  hashtags: [ { id: 2, hashtag: '#bye' }, { id: 3, hashtag: '#avocado' } ]
}

+select와 비슷하게 구현하는 방법

const oldPhotoHashtags = await client.photo.findFirst({
	where: { id },
}).hashtags();

출력결과

[ { id: 2, hashtag: '#bye' }, { id: 3, hashtag: '#avocado' } ]

++여기서 특정 key값만 추출하는 방법

const oldPhotoHashtags = await client.photo.findFirst({
	where: { id },
	}).hashtags({
		select: {hashtag: true,},
});

출력결과

[ { hashtag: '#bye' }, { hashtag: '#avocado' } ]

connect [create, update]

create 또는 update를 할 때만 사용 가능하며, 목적은 레코드를 추가할 때 @relation 관계에 있는 table의 어떠한 값과 연결시킬 것인지 지정하고자 할 때 사용한다.

profile
다시태어나고싶어요

0개의 댓글