@takeny1998/nestjs-prisma-transactional
TypeScript icon, indicating that this package has built-in type declarations

1.0.5 • Public • Published

nestjs-prisma-transactional

Manage PrismaORM transactions easily by adding decorator to your nest.js services.

npm npm MIT License

Installation

Install with npm

  npm install @takeny1998/nestjs-prisma-transactional

Or install via yarn

  yarn add @takeny1998/nestjs-prisma-transactional

Usage

Step 1: Import Transaction Module

Import the TransactionMoudle to AppModule of your nest.js project. Or just add it to the module you want to use.

import { TransactionModule } from '@takeny1998/nestjs-prisma-transactional';

@Module({
  imports: [
    ...
    TransactionModule, // just import it
  ],
  controllers: [...],
  providers: [...],
})
export class AppModule {}

Step 2: Extend PrismaClient

I provide the extension so that you can freely extend PrismaClient. My extension does not modify the type of the existing PrismaClient, so you can simply extend and inject your own Client.

import { TransactionModule } from '@takeny1998/nestjs-prisma-transactional';

@Module({
  imports: [
    ...
    TransactionModule,
  ],
  controllers: [...],
  providers: [
    {
      provide: 'PrismaService',
      useValue: new PrismaService().$extends(txExtension),
    },
  ],
  exports: ['PrismaService'],
})
export class AppModule {}

Otherwise, a good alternative is to use the CustomPrismaModule from the nestjs-prisma package. (If you have a great way to extend PrismaClient, please let me know it by opening an issue)

Step 3: Add @Transactional Decorator whereever you want

Add the @Transactional() decorator to any method you want to make transactional scope, and all repositories using the method's child PrismaClient will automatically be merged as one transaction.

@Transactional()
async writePost(dto: WritePostDto) {
  const createdPost = await this.postService.createPost(userUuid, { ...post, summary });

  const user = await this.userService.findUserByUniqueInput({ uuid: userUuid });

  if (!user) {
    throw new NotFoundException(`Cloud not found User with UUID: ${userUuid}`);
  }

  ...
}

Even if there are decorators from repositories or domain services under the method, they will be merged into the top-level transaction scope.

Contributing

Contributions are always welcome!

If you have any suggestions for improvements or encounter a bug while using this package, please feel free to open an issue.

Readme

Keywords

none

Package Sidebar

Install

npm i @takeny1998/nestjs-prisma-transactional

Weekly Downloads

0

Version

1.0.5

License

MIT

Unpacked Size

63.7 kB

Total Files

82

Last publish

Collaborators

  • takeny1998