description: "PostgreSQL + Prisma ORM 패턴" paths:
prisma/schema.prisma 에서 단일 관리npx prisma migrate dev --name <설명> 으로 마이그레이션 생성npx prisma migrate deployPascalCase 단수형 (Streamer, LiveSnapshot, StreamerGroup)camelCase (channelId, createdAt, followerCount)@@map("snake_case")@@index 명시Service 에서 PrismaService 를 주입받아 사용한다. 별도 Repository 레이어를 두지 않는다.
@Injectable()
export class CompanyService {
constructor(private readonly prisma: PrismaService) {}
async findById(id: number) {
return this.prisma.company.findUnique({ where: { id } });
}
}
include / selectinclude 또는 select_count 집계로 카운트만 필요한 경우 join 없이 처리const company = await this.prisma.company.findUnique({
where: { id: companyId },
include: {
_count: { select: { groups: true, streamers: true } },
groups: {
include: { members: { select: { streamer: true } } },
},
},
});
여러 테이블에 걸친 작업은 $transaction 사용
await this.prisma.$transaction([
this.prisma.liveRanking.deleteMany({ where: { snapshotId } }),
this.prisma.liveSnapshot.delete({ where: { id: snapshotId } }),
]);
"있으면 업데이트, 없으면 생성" 패턴은 upsert 사용
await this.prisma.streamer.upsert({
where: { channelId },
create: { channelId, channelName, followerCount },
update: { channelName, followerCount },
});
_count 값을 단순 합산하면 중복 카운트 위험 — 유니크 ID 기반으로 계산companyId?) 는 onDelete: SetNull 로 안전하게 처리@@index 추가select 로 필요한 컬럼만 가져오기include 로 한 번에 로드하거나, 서비스에서 배치 처리아직 피드백이 없어요. 첫 번째로 의견을 남겨보세요!