Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow configuring Postgres datasource builder #341

Merged
merged 1 commit into from
Jun 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading