A mythical god who brings reliability to your system 🫒 by implementing the Outbox pattern, so you don't have to think of it too much.
You don't have to pray for the gifts of nature. Just run npm and type:
npm i @chassisjs/hermes @chassisjs/hermes-mysql
mysql2 connection, Kysely, Drizzle, Prisma or any library that runs SQLAUTO_INCREMENT idimport { createOutboxConsumer } from '@chassisjs/hermes-mysql'
const outbox = createOutboxConsumer<DomainEvent>({
getOptions: () => ({ host: 'localhost', port: 3306, user: 'app', password: 'secret', database: 'app' }),
publish: async (envelope) => {
await broker.publish(envelope.messageType, envelope.message)
},
consumerName: 'app',
})
const stop = await outbox.start()
const connection = await pool.getConnection()
await connection.beginTransaction()
await connection.query('INSERT INTO patient (id) VALUES (?)', [patientId])
await outbox.queue(
{ messageId: patientId, messageType: 'PatientRegistered', message: { patientId } },
{ tx: connection, partitionKey: tenantId },
)
await connection.commit()
Delivery is at-least-once, so make the handlers of the messages idempotent 🥰
See the documentation.