using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Npgsql;
using StellaOps.Infrastructure.Postgres.Connections;
using StellaOps.Infrastructure.Postgres.Options;
using StellaOps.Notify.Persistence.Postgres.Models;
namespace StellaOps.Notify.Persistence.Postgres;
///
/// PostgreSQL data source for the Notify module.
/// Manages connections with tenant context for notifications and delivery tracking.
///
public sealed class NotifyDataSource : DataSourceBase
{
///
/// Default schema name for Notify tables.
///
public const string DefaultSchemaName = "notify";
///
/// Creates a new Notify data source.
///
public NotifyDataSource(IOptions options, ILogger logger)
: base(CreateOptions(options.Value), logger)
{
}
///
protected override string ModuleName => "Notify";
///
protected override void ConfigureDataSourceBuilder(NpgsqlDataSourceBuilder builder)
{
// Register PostgreSQL enum type mappings so Npgsql sends enum values natively
// instead of text, matching the notify.channel_type and notify.delivery_status DB types.
builder.MapEnum(DefaultSchemaName + ".channel_type");
builder.MapEnum(DefaultSchemaName + ".delivery_status");
}
private static PostgresOptions CreateOptions(PostgresOptions baseOptions)
{
if (string.IsNullOrWhiteSpace(baseOptions.SchemaName))
{
baseOptions.SchemaName = DefaultSchemaName;
}
return baseOptions;
}
}