72 lines
1.7 KiB
TypeScript
72 lines
1.7 KiB
TypeScript
/* eslint-disable prettier/prettier */
|
|
import { Injectable } from '@nestjs/common'
|
|
import { Prisma, User } from '@prisma/client'
|
|
import { PrismaService } from 'src/prisma.service'
|
|
|
|
@Injectable()
|
|
export class UserService {
|
|
constructor(private prisma: PrismaService) {
|
|
prisma.$on<any>('query', (event: Prisma.QueryEvent) => {
|
|
console.log('Query: ' + event.query)
|
|
console.log('Duration: ' + event.duration + 'ms')
|
|
})
|
|
}
|
|
|
|
//전체조회
|
|
async count(params: {
|
|
cursor?: Prisma.UserWhereUniqueInput
|
|
where?: Prisma.UserWhereInput
|
|
}): Promise<number> {
|
|
const { cursor, where } = params
|
|
return this.prisma.user.count({
|
|
cursor,
|
|
where
|
|
})
|
|
}
|
|
|
|
//전체조회
|
|
async fetchAll(params: {
|
|
skip?: number
|
|
take?: number
|
|
cursor?: Prisma.UserWhereUniqueInput
|
|
where?: Prisma.UserWhereInput
|
|
orderBy?: Prisma.UserOrderByWithRelationInput
|
|
}): Promise<User[]> {
|
|
const { skip, take, cursor, where, orderBy } = params
|
|
return this.prisma.user.findMany({
|
|
skip,
|
|
take,
|
|
cursor,
|
|
where,
|
|
orderBy
|
|
})
|
|
}
|
|
|
|
//단일 조회
|
|
async fetchOne(where: Prisma.UserWhereInput): Promise<User | null> {
|
|
return this.prisma.user.findFirstOrThrow({ where: where })
|
|
}
|
|
|
|
//단일 추가
|
|
async add(data: Prisma.UserCreateInput): Promise<User> {
|
|
return this.prisma.user.create({ data })
|
|
}
|
|
|
|
//단일 수정
|
|
async update(params: {
|
|
where: Prisma.UserWhereUniqueInput
|
|
data: Prisma.UserUpdateInput
|
|
}): Promise<User | null> {
|
|
const { where, data } = params
|
|
return this.prisma.user.update({
|
|
data,
|
|
where
|
|
})
|
|
}
|
|
|
|
//단일 삭제
|
|
async remove(where: Prisma.UserWhereUniqueInput): Promise<User | null> {
|
|
return this.prisma.user.delete({ where })
|
|
}
|
|
}
|