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

@lepresk/after-commit

v0.1.0

Published

Run side effects only after your database transaction commits, using AsyncLocalStorage. Register after-commit hooks anywhere in the call stack without threading callbacks through every service. Framework and ORM agnostic.

Downloads

31

Readme

@lepresk/after-commit

CI npm version node license: MIT

Run side effects only after your database transaction commits, using AsyncLocalStorage.

Sending an email, enqueuing a job, or publishing an event from inside a transaction is a bug: the transaction can still roll back after the side effect has already left the building. The usual fix is to thread an onCommit callback array through every service and repository. This library removes that plumbing. Open a context around the transaction, and any code running inside it can register an after-commit hook, no matter how deep in the call stack.

Framework and ORM agnostic. Zero dependencies.

Install

pnpm add @lepresk/after-commit

Usage

Open a context around the unit of work. Hooks registered inside it run, in order, only after the callback resolves:

import { runWithAfterCommitContext, registerAfterCommitHook } from '@lepresk/after-commit';

await runWithAfterCommitContext(async () => {
  await db.transaction(async (tx) => {
    const order = await createOrder(tx, input);

    // Registered here, deep in the domain layer, but does not fire yet.
    registerAfterCommitHook(() => sendOrderConfirmationEmail(order));

    await chargePayment(tx, order);
  });
});
// The transaction has committed. Only now does the email hook run.

If the callback throws, the transaction rolls back and the hooks are discarded:

await runWithAfterCommitContext(async () => {
  registerAfterCommitHook(() => sendEmail()); // never runs
  throw new Error('validation failed');
});

Registering a hook with no active context runs it immediately, so non-transactional call sites keep working without special casing:

// No surrounding runWithAfterCommitContext: the hook runs now.
registerAfterCommitHook(() => publishEvent());

Why AsyncLocalStorage

The context is stored in an AsyncLocalStorage, so registerAfterCommitHook finds it automatically across await boundaries and nested function calls. Your domain services never receive, hold, or pass an onCommit array. That keeps the transactional concern at the boundary where it belongs, instead of leaking into every signature.

API

runWithAfterCommitContext<T>(callback: () => Promise<T>): Promise<T>

Runs callback inside a fresh context and returns its result. On success, hooks run in registration order. If callback rejects, hooks are discarded and the error is rethrown. If a hook rejects, the remaining hooks do not run and the rejection propagates. Contexts nest: an inner context's hooks are isolated from the outer one.

registerAfterCommitHook(hook: () => void | Promise<void>): void

Defers hook until the active context succeeds. With no active context, the hook runs immediately and a rejection is logged rather than left unhandled.

hasActiveAfterCommitContext(): boolean

Returns whether a context is currently active. Useful to assert that a code path is running inside a transaction.

setAfterCommitLogger(logger: { error(error: unknown, message: string): void }): void

Overrides the logger used when a no-context hook rejects. Defaults to console.error. In NestJS, pass a Logger instance to route these into your logging pipeline:

import { Logger } from '@nestjs/common';
import { setAfterCommitLogger } from '@lepresk/after-commit';

const logger = new Logger('AfterCommit');
setAfterCommitLogger({ error: (err, message) => logger.error(message, err) });

Requirements

  • Node.js >=18.18 (AsyncLocalStorage)

Development

pnpm install
pnpm typecheck
pnpm lint
pnpm test:coverage
pnpm build

License

MIT