【发布时间】:2022-12-11 11:53:45
【问题描述】:
我有一个用户表和一个帖子表。
没有至少一个帖子,用户就不可能存在。
我有下一个“UsersService”:
class CreateUserDto {
firtname: string;
lastname: string;
postIdToAssign: number;
}
class UsersService {
constructor(
private readonly postsService: PostsService,
private readonly prisma: PrismaService,
) {}
createUser(dto: CreateUserDto) {
if (this.postsService.find(postsIdToAssign) {
throw new PostAlreadyAssignedError();
}
return this.prisma.user.create({
data: dto,
})
}
}
为了检查帖子是否存在,我应该使用“postsService.find”方法(在上面的代码中),还是直接执行
if (this.prisma.post.findFirst({where: {id: postIdToAssign}}) // then throw Error
这里主要关心的是我是否应该使用 PostsService 的方法,因为如果使用 PostsService 方法——在某些情况下“PostsService”可能需要使用一些“UsersService”方法,我们将得到循环依赖。或者这是正常情况?
【问题讨论】:
标签: typescript rest nestjs prisma