Skip to content

Commit

Permalink
Allow configuring Postgres datasource builder. Fixes #323.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeyzimarev committed Jun 13, 2024
1 parent f3d8029 commit 2e85521
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,32 @@ public static class ServiceCollectionExtensions {
/// <param name="connectionString">Connection string</param>
/// <param name="schema">Schema name</param>
/// <param name="initializeDatabase">Set to true if you want the schema to be created on startup</param>
/// <param name="configureBuilder">Optional: function to configure the data source builder</param>
/// <param name="connectionLifetime">Optional: lifetime of the connection, default is transient</param>
/// <param name="dataSourceLifetime">Optional> lifetime of the data source, default is singleton</param>
/// <returns></returns>
/// <returns>Services collection</returns>
// ReSharper disable once UnusedMethodReturnValue.Global
public static IServiceCollection AddEventuousPostgres(
this IServiceCollection services,
string connectionString,
string schema,
bool initializeDatabase = false,
ServiceLifetime connectionLifetime = ServiceLifetime.Transient,
ServiceLifetime dataSourceLifetime = ServiceLifetime.Singleton
this IServiceCollection services,
string connectionString,
string schema,
bool initializeDatabase = false,
Action<IServiceProvider, NpgsqlDataSourceBuilder>? configureBuilder = null,
ServiceLifetime connectionLifetime = ServiceLifetime.Transient,
ServiceLifetime dataSourceLifetime = ServiceLifetime.Singleton
) {
var options = new PostgresStoreOptions {
Schema = schema,
ConnectionString = connectionString,
InitializeDatabase = initializeDatabase
};

var s = new Schema(schema);

services.AddNpgsqlDataSourceCore(
_ => connectionString,
(_, builder) => builder.MapComposite<NewPersistedEvent>(s.StreamMessage),
(sp, builder) => {
builder.MapComposite<NewPersistedEvent>(Schema.GetStreamMessageTypeName(schema));
configureBuilder?.Invoke(sp, builder);
},
connectionLifetime,
dataSourceLifetime
);
Expand All @@ -57,14 +61,17 @@ public static IServiceCollection AddEventuousPostgres(
/// </summary>
/// <param name="services">Service collection</param>
/// <param name="config">Configuration section for PostgreSQL options</param>
/// <param name="configureBuilder">Optional: function to configure the data source builder</param>
/// <param name="connectionLifetime">Optional: lifetime of the connection, default is transient</param>
/// <param name="dataSourceLifetime">Optional> lifetime of the data source, default is singleton</param>
/// <returns></returns>
/// <returns>Services collection</returns>
// ReSharper disable once UnusedMethodReturnValue.Global
public static IServiceCollection AddEventuousPostgres(
this IServiceCollection services,
IConfiguration config,
ServiceLifetime connectionLifetime = ServiceLifetime.Transient,
ServiceLifetime dataSourceLifetime = ServiceLifetime.Singleton
this IServiceCollection services,
IConfiguration config,
Action<IServiceProvider, NpgsqlDataSourceBuilder>? configureBuilder = null,
ServiceLifetime connectionLifetime = ServiceLifetime.Transient,
ServiceLifetime dataSourceLifetime = ServiceLifetime.Singleton
) {
services.Configure<PostgresStoreOptions>(config);
services.AddSingleton<PostgresStoreOptions>(sp => sp.GetRequiredService<IOptions<PostgresStoreOptions>>().Value);
Expand All @@ -73,8 +80,8 @@ public static IServiceCollection AddEventuousPostgres(
sp => Ensure.NotEmptyString(sp.GetRequiredService<PostgresStoreOptions>().ConnectionString),
(sp, builder) => {
var options = sp.GetRequiredService<PostgresStoreOptions>();
var schema = new Schema(options.Schema);
builder.MapComposite<NewPersistedEvent>(schema.StreamMessage);
builder.MapComposite<NewPersistedEvent>(Schema.GetStreamMessageTypeName(options.Schema));
configureBuilder?.Invoke(sp, builder);
},
connectionLifetime,
dataSourceLifetime
Expand Down
6 changes: 4 additions & 2 deletions src/Postgres/src/Eventuous.Postgresql/Schema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ namespace Eventuous.Postgresql;
public class Schema(string schema = Schema.DefaultSchema) {
public const string DefaultSchema = "eventuous";

public string StreamMessage => $"{schema}.stream_message";
public static string GetStreamMessageTypeName(string schema = Schema.DefaultSchema) => $"{schema}.stream_message";

public string StreamMessage => GetStreamMessageTypeName(schema);
public string AppendEvents => $"select * from {schema}.append_events(@_stream_name, @_expected_version, @_created, @_messages)";
public string ReadStreamForwards => $"select * from {schema}.read_stream_forwards(@_stream_name, @_from_position, @_count)";
public string ReadStreamBackwards => $"select * from {schema}.read_stream_backwards(@_stream_name, @_count)";
Expand Down Expand Up @@ -42,7 +44,7 @@ public async Task CreateSchema(NpgsqlDataSource dataSource, ILogger<Schema>? log
using var reader = new StreamReader(stream!);

#if NET7_0_OR_GREATER
var script = await reader.ReadToEndAsync(cancellationToken).NoContext();
var script = await reader.ReadToEndAsync(cancellationToken).NoContext();
#else
var script = await reader.ReadToEndAsync().NoContext();
#endif
Expand Down

0 comments on commit 2e85521

Please sign in to comment.