nestjs/outbox

A transactional outbox module for Nest framework (node.js) 📤

★ 7Forks 1TypeScriptGitHub ↗Compare
nestjsnodejsoutbox-patterntransactional-outbox

README

Nest Logo

A progressive Node.js framework for building efficient and scalable server-side applications.

NPM Version Package License NPM Downloads Discord Backers on Open Collective Sponsors on Open Collective

Description

Transactional outbox module for Nest: messages written in the same database transaction as your business data, a relay that publishes them after commit with leases, retries and a dead-letter table, and an inbox that deduplicates redeliveries, with no third-party dependencies.

Installation

$ npm i --save @nestjs/outbox

Quick Start

Overview & Tutorial

PostgreSQL store

@nestjs/outbox/postgres ships PostgresOutboxStore, which keeps the messages, the dead letters and the consumers' inbox in a schema of its own (nest_outbox) and writes through the client your application already uses: fromPg(pool), fromDrizzle(db), fromTypeOrm(dataSource), fromPrisma(prisma) or fromKysely(db). Register it with a factory provider:

import { OutboxStorage } from '@nestjs/outbox';
import { fromDrizzle, PostgresOutboxStore } from '@nestjs/outbox/postgres';

@Module({
  imports: [DrizzleModule.forRoot({ drizzle, connection: process.env.DATABASE_URL! }), OutboxModule.forRoot()],
  providers: [
    {
      provide: PostgresOutboxStore,
      inject: [getDrizzleToken(), OutboxStorage],
      useFactory: (db: Database, storage: OutboxStorage) => new PostgresOutboxStore({ executor: fromDrizzle(db) }, storage),
    },
  ],
})
export class AppModule {}

outbox.add(tx, message) then takes your ORM's own transaction object (Drizzle's tx, a TypeORM EntityManager, a Prisma transaction client, a Kysely Transaction, a pg client after BEGIN). The store applies its migrations at startup, except when NODE_ENV is production; there, apply them on deploy with npx nest-outbox migrate --url <database url> (status checks, sql prints them for your own migration tool).

MySQL store

@nestjs/outbox/mysql ships MySqlOutboxStore (MySQL 8.4 LTS and 9.x), registered the same way: import it and the executor from @nestjs/outbox/mysql instead (fromMysql2(pool), fromDrizzle(db), fromTypeOrm(dataSource), fromPrisma(prisma) with @prisma/adapter-mariadb, or fromKysely(db)). Its tables live in your connection's database, named after the schema option (nest_outbox_messages, nest_outbox_dead_letters, nest_outbox_inbox); ids, topics, keys and consumer names are compared byte for byte and hold at most 255 characters. npx nest-outbox migrate --url mysql://... applies its migrations, and sql --dialect mysql prints them (MySqlOutboxStore.migrationStatements() lists them one per string, for TypeORM's queryRunner.query()).

Support

Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please read more here.

Stay in touch

License

Nest is MIT licensed.

Contributors

kamilmysliwiecAsadshah7950

Issues