npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@hodfords/nestjs-transaction

v11.3.0-rc.4

Published

Manages database transactions in NestJS applications

Readme

Installation 🤖

Install the nestjs-transaction package with:

npm install @hodfords/nestjs-transaction --save

Usage 🚀

First, you need to import the TransactionModule into your AppModule and

import { Module } from '@nestjs/common';
import { TransactionModule } from '@hodfords/nestjs-transaction';

@Module({
    imports: [
        TransactionModule.forRoot({
            autoUseMasterNodeForChangeRequest: true
        }),
        // other modules
    ],
    controllers: [],
    providers: [],
})
export class AppModule {}

Optional Configuration

You can configure the TransactionModule with the following options:

  • autoUseMasterNodeForChangeRequest: If set to true, the module will automatically use the master node for change requests (except GET). This is useful for ensuring that write operations are always directed to the correct database node.

How to use

Transactional

You can use the @Transactional decorator to mark a method as transactional. This means that all database operations within this method and child methods will be executed within a transaction context. If any operation fails, the entire transaction will be rolled back, ensuring data integrity.

In any service, you can use the @Transactional decorator to ensure that the method is executed within a transaction context:

Note: We suggest using the @Transactional decorator on Controller/Task/Cron jobs methods, as it will automatically handle the transaction lifecycle for you. However, you can also use it in services if needed.

@Transactional()
async myTransactionalMethod() {
    await this.myRepository.save(myEntity);
}

In controllers, you can also use the @Transactional decorator to ensure that the entire request is wrapped in a transaction:


import { Controller, Post } from '@nestjs/common';
import { Transactional } from '@hodfords/nestjs-transaction';
import { MyService } from './my.service';

@Controller('my')
export class MyController {
    constructor(private myService: MyService) {}
    
    @Post('create')
    @Transactional()
    async create() {
        return this.myService.create();
    }
}

You also use the @Transactional decorator with options to control the transaction behavior, such as isolation level. For example:

@Transactional({ isolationLevel: 'SERIALIZABLE'})
async myTransactionalMethod() {
    // This method will run in a transaction with SERIALIZABLE isolation level
    await this.myRepository.save(myEntity);
}

Replication

You can use the @UseMasterNode/@UseSlaveNode decorator to ensure that a method is executed on the master/slave node. This is useful for write operations that need to be directed to the master/slave database node.

import { Controller, Post } from '@nestjs/common';
import { UseMasterNode } from '@hodfords/nestjs-transaction';
import { MyService } from './my.service';

@Controller('my')
export class MyController {
    constructor(private myService: MyService) {}
    
    @Post('create')
    @UseMasterNode()
    async create() {
        return this.myService.create();
    }
}

Hooks

You can use the @RunAfterTransactionCommit decorator to run a method after a transaction has been successfully committed. This is useful for performing actions that should only occur if the transaction was successful.

import { Injectable } from '@nestjs/common';
import { RunAfterTransactionCommit } from '@hodfords/nestjs-transaction';
import { PostRepository } from './post.repository';

@Injectable()
export class MyService {

    constructor(private postRepo: PostRepository) {
    }

    @Transactional()
    async create() {
        // Perform some database operations
        await this.postRepo.createPost({ title: 'New Post', content: 'This is a new post.' });
        await this.emitEvent();
    }

    @RunAfterTransactionCommit()
    async emitEvent() {
        // This method will be called after the transaction is committed
        console.log('Transaction committed successfully!');
    }
}

You also use it with method runAfterTransactionCommit without the decorator:


this.postRepo.create(...);
runAfterTransactionCommit(() => {
    // This code will run after the transaction is committed
    console.log('Transaction committed successfully!');
});

License 📝

This project is licensed under the MIT License