简体   繁体   English

NestJS 中 Prisma 交互式事务的回滚在抛出错误时不起作用

[英]Rollback of Prisma Interactive Transaction in NestJS not working when throwing an error

I am using the prisma ORM and NestJS and I got the following example code:我正在使用棱镜 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`);
  });
}

I would expect that the transaction is rolled back as I have thrown an error, but it is not, it creates a new database entry.我希望事务回滚,因为我抛出了一个错误,但事实并非如此,它会创建一个新的数据库条目。

Here is the example of the official documentation .这是官方文档的示例

Is this maybe related to the dependency injection of NestJS and that the injected prisma service is not correctly recognizing the error?这可能与 NestJS 的依赖注入以及注入的 prisma 服务没有正确识别错误有关吗? Or am I doing something wrong?还是我做错了什么?

As example shows you need to use prisma instance that is passed as an argument to your callback function, like that:如示例所示,您需要使用作为参数传递给回调 function 的 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`);
  });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM