NestJS 中的 Prisma Interactive Transaction 回滚在抛出错误时不起作用

Fel*_*iel 6 rollback node.js nestjs prisma

我正在使用prisma ORMNestJS,我得到了以下示例代码:

async createMerchant(data: Prisma.MerchantCreateInput): Promise<Merchant> {
  return await this.prisma.$transaction(async (): Promise<Merchant> => {
    await this.prisma.merchant.create({
      data,
    });
    throw new Error(`Some error`);
  });
}
Run Code Online (Sandbox Code Playgroud)

我希望事务会回滚,因为我抛出了一个错误,但事实并非如此,它创建了一个新的数据库条目。

这是官方文档的示例

这是否可能与 NestJS 的依赖注入有关,并且注入的 prisma 服务无法正确识别错误?或者我做错了什么?

Dan*_*ila 14

如示例所示,您需要使用 prisma 实例作为参数传递给回调函数,如下所示:

async createMerchant(data: Prisma.MerchantCreateInput): Promise<Merchant> {
  return await this.prisma.$transaction(async (prisma): Promise<Merchant> => {
    // Not this.prisma, but prisma from argument
    await prisma.merchant.create({
      data,
    });
    throw new Error(`Some error`);
  });
}
Run Code Online (Sandbox Code Playgroud)