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

typeorm-transactional-subscriber

v0.2.0

Published

Adds afterInsertCommitted, afterUpdateCommitted, and afterRemoveCommitted hooks to TypeORM subscribers, ensuring side effects only run after a successful transaction commit (never on rollback or savepoint release).

Readme

typeorm-transactional-subscriber

A base class for TypeORM subscribers that adds afterInsertCommitted, afterUpdateCommitted, afterRemoveCommitted, and afterSoftRemoveCommitted hooks, ensuring side effects only run after a successful transaction commit (never on rollback or savepoint release).

Why?

TypeORM's entity subscribers fire hooks (like afterInsert) even if the transaction is later rolled back. This package allows you to safely perform side effects (like sending emails or publishing to a message queue) only after a transaction is committed.

Installation

npm install typeorm-transactional-subscriber
# or
pnpm add typeorm-transactional-subscriber
# or
yarn add typeorm-transactional-subscriber

Usage

import { TransactionalEntitySubscriberBase } from "typeorm-transactional-subscriber";
import {
  EventSubscriber,
  InsertEvent,
  UpdateEvent,
  RemoveEvent,
  SoftRemoveEvent,
} from "typeorm";

@EventSubscriber()
export class UserSubscriber extends TransactionalEntitySubscriberBase<User> {
  listenTo() {
    return User;
  }

  async afterInsertCommitted(event: InsertEvent<User>) {
    // Called after transaction commit or immediately if not in a transaction
    await sendWelcomeEmail(event.entity.email);
  }

  async afterUpdateCommitted(event: UpdateEvent<User>) {
    // Called after transaction commit or immediately if not in a transaction
    await logUserUpdate(event.entity.id);
  }

  async afterRemoveCommitted(event: RemoveEvent<User>) {
    // Called after transaction commit or immediately if not in a transaction
    await cleanupUserData(event.entity.id);
  }

  async afterSoftRemoveCommitted(event: SoftRemoveEvent<User>) {
    // Called after transaction commit or immediately if not in a transaction
    await logSoftRemove(event.entity.id);
  }
}

How it works

  • During a transaction, afterInsert/afterUpdate/afterRemove/afterSoftRemove events are queued per transaction.
  • On commit, the queued events are replayed and your hooks are called.
  • On rollback, the queue is cleared and "*Committed" hooks are NOT called.

Nested Transactions (Savepoints) Support

This package supports nested transactions (savepoints) out of the box. If you use TypeORM's nested transactions (e.g., by calling manager.transaction() inside another transaction), the subscriber will maintain a transaction depth counter per QueryRunner. Post-commit hooks (such as afterInsertCommitted, afterUpdateCommitted, afterRemoveCommitted, afterSoftRemoveCommitted) are only called after the outermost transaction is committed. Inner (nested) commits or rollbacks do not trigger post-commit hooks; only the final, outer commit does.

Example:

await dataSource.manager.transaction(async (outerManager) => {
  // ...
  await outerManager.transaction(async (innerManager) => {
    // ...
  });
  // ...
});
// Only after the outermost commit will post-commit hooks run.

This ensures that side effects (like event publishing, logging, etc.) only occur if the entire transaction scope (including all nested savepoints) is successfully committed.

⚠️ Overriding afterInsert, afterUpdate and afterRemove

The afterInsert, afterUpdate, and afterRemove methods are called by TypeORM immediately after the corresponding database operation—during the transaction. By default, the base class implementation queues these events for post-commit processing. If you want to add custom logic that runs during the transaction (not after commit), you can override these methods. Be sure to call super.afterInsert(event) (or the corresponding method) to preserve the transactional queuing logic. You can place your custom logic before or after the super call, depending on when you want it to run:

async afterInsert(event: InsertEvent<User>) {
  await super.afterInsert(event); // Ensures transactional queuing and post-commit logic
  // Custom logic after transactional handling
}

Note:

  • If you do not call super.afterInsert(event), the transactional queuing and post-commit logic will be skipped for that event.
  • The same pattern applies to afterUpdate and afterRemove.

⚠️ Overriding afterTransactionStart

If you override the afterTransactionStart method in a subclass of TransactionalEntitySubscriberBase, you must call super.afterTransactionStart(event) inside your override. Failing to do so will break the transaction depth tracking, and nested transaction (savepoint) support will not work correctly. This may cause post-commit hooks to be triggered after inner/nested commits instead of only after the outermost commit.

Example:

public afterTransactionStart(event: { queryRunner: any }) {
  super.afterTransactionStart(event); // ensure depth tracking
  // custom logic here
}

Always call the base implementation to preserve correct transactional behavior.

License

MIT