UsePostgres
Adds PostgreSQL database support for persisting train metadata, logs, manifests, and dead letters. Automatically migrates the database schema on startup.
Signatures
// Basic — uses default Npgsql data source settings
public static TraxEffectBuilderWithData UsePostgres(
this TraxEffectBuilder effectBuilder,
string connectionString
)
// With data source configuration — tune pool size, timeouts, multiplexing, etc.
public static TraxEffectBuilderWithData UsePostgres(
this TraxEffectBuilder effectBuilder,
string connectionString,
Action<NpgsqlDataSourceBuilder> configureDataSource
)Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
connectionString | string | Yes | PostgreSQL connection string (e.g., "Host=localhost;Database=trax;Username=postgres;Password=password") |
configureDataSource | Action<NpgsqlDataSourceBuilder> | No | Callback to configure the Npgsql data source builder. Invoked after Trax registers its enum mappings but before Build(). Use this to tune connection pool settings, enable multiplexing, or configure other NpgsqlDataSourceBuilder options. |
Returns
TraxEffectBuilderWithData — a subclass of TraxEffectBuilder that unlocks data-dependent methods like AddDataContextLogging. This provides compile-time safety: methods that require a data provider are only available on the returned type.
Examples
Basic usage with default settings:
services.AddTrax(trax => trax
.AddEffects(effects => effects
.UsePostgres("Host=localhost;Database=trax;Username=postgres;Password=password")
.AddDataContextLogging()
)
);Tuning the connection pool for high-throughput deployments:
services.AddTrax(trax => trax
.AddEffects(effects => effects
.UsePostgres(connectionString, dataSource =>
{
dataSource.ConnectionStringBuilder.MaxPoolSize = 50;
dataSource.ConnectionStringBuilder.MinPoolSize = 5;
dataSource.ConnectionStringBuilder.ConnectionIdleLifetime = 300;
})
)
);What It Registers
- Migrates the database schema to the latest version via
DatabaseMigrator(unless SkipMigrations was called) - Creates an
NpgsqlDataSourcewith enum mappings (TrainState,LogLevel,ScheduleType,DeadLetterStatus,WorkQueueStatus,MisfirePolicy) - Registers
IDbContextFactory<PostgresContext>for creating database contexts - Registers
IDataContext(scoped) for direct database access - Enables data context logging support (for AddDataContextLogging)
- Registers
PostgresContextProviderFactoryas a non-toggleable effect
Remarks
- Returns
TraxEffectBuilderWithData, which makesAddDataContextLogging()available at compile time. Methods that don't require a data provider (likeAddJson(),SaveTrainParameters()) use generic self-type preservation and work on bothTraxEffectBuilderandTraxEffectBuilderWithData. - The database migration runs synchronously on startup. Ensure the database server is accessible at application start time. To skip migration (e.g., in Lambda runners), call SkipMigrations before
UsePostgres(). - For testing/development without a database, use UseInMemory instead.
Package
dotnet add package Trax.Effect.Data.Postgres