Manage PrismaORM transactions easily by adding decorator to your nest.js services.
Install with npm
npm install @takeny1998/nestjs-prisma-transactionalOr install via yarn
yarn add @takeny1998/nestjs-prisma-transactionalImport 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 {}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)
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.
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.