Skip to main content

Stores Overview

Stores persist saga state to databases.

Choosing a Store​

StoreBest ForFeatures
PostgreSQLGeneral purposeACID, JSON support
MySQLMySQL shopsWide compatibility
SQL ServerMicrosoft stackEnterprise features
MongoDBDocument-orientedFlexible schema
DynamoDBAWS serverlessAuto-scaling
RedisHigh performanceTTL support
SQLiteLocal developmentZero config
PrismaORM usersType-safe queries
In-MemoryTestingNo persistence

Store Interface​

All stores implement:

interface SagaStore<TState extends SagaState> {
getById(sagaName: string, sagaId: string): Promise<TState | null>;
getByCorrelationId(sagaName: string, correlationId: string): Promise<TState | null>;
insert(sagaName: string, correlationId: string, state: TState): Promise<void>;
update(sagaName: string, state: TState, expectedVersion: number): Promise<void>;
delete(sagaName: string, sagaId: string): Promise<void>;
}

Basic Usage​

import { PostgresSagaStore } from '@saga-bus/store-postgres';

const store = new PostgresSagaStore({
connectionString: process.env.DATABASE_URL,
});

const bus = createBus({
transport,
store,
sagas: [{ definition: orderSaga }],
});

Schema Management​

Most SQL stores provide schema helpers:

import { createSchema, getSchemaSql } from '@saga-bus/store-postgres';

// Create tables programmatically
await createSchema(pool);

// Or get SQL for manual migration
const sql = getSchemaSql();

Shared vs Per-Saga​

// Shared store (recommended)
const bus = createBus({
store: sharedStore,
sagas: [{ definition: saga1 }, { definition: saga2 }],
});

// Per-saga stores
const bus = createBus({
sagas: [
{ definition: saga1, store: store1 },
{ definition: saga2, store: store2 },
],
});