From ab9f2f577b41017dea79231eecba36e141f1a3d6 Mon Sep 17 00:00:00 2001 From: "Daniel J. Summers" Date: Fri, 28 Jul 2023 19:41:53 -0400 Subject: [PATCH] Use PG lib data source singleton --- src/MyWebLog.Data/PostgresData.fs | 12 +++++------- src/MyWebLog/Program.fs | 16 +++++++--------- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/src/MyWebLog.Data/PostgresData.fs b/src/MyWebLog.Data/PostgresData.fs index 0650379..58967d5 100644 --- a/src/MyWebLog.Data/PostgresData.fs +++ b/src/MyWebLog.Data/PostgresData.fs @@ -6,16 +6,14 @@ open Microsoft.Extensions.Logging open MyWebLog open MyWebLog.Data.Postgres open Newtonsoft.Json -open Npgsql open Npgsql.FSharp /// Data implementation for PostgreSQL -type PostgresData (source : NpgsqlDataSource, log : ILogger, ser : JsonSerializer) = +type PostgresData (log : ILogger, ser : JsonSerializer) = /// Create any needed tables let ensureTables () = backgroundTask { // Set up the PostgreSQL document store - Configuration.useDataSource source Configuration.useSerializer { new IDocumentSerializer with member _.Serialize<'T> (it : 'T) : string = Utils.serialize ser it @@ -23,9 +21,8 @@ type PostgresData (source : NpgsqlDataSource, log : ILogger, ser : } let! tables = - Sql.fromDataSource source - |> Sql.query "SELECT tablename FROM pg_tables WHERE schemaname = 'public'" - |> Sql.executeAsync (fun row -> row.string "tablename") + Custom.list "SELECT tablename FROM pg_tables WHERE schemaname = 'public'" [] + (fun row -> row.string "tablename") let needsTable table = not (List.contains table tables) // Create a document table let mutable isNew = false @@ -117,7 +114,8 @@ type PostgresData (source : NpgsqlDataSource, log : ILogger, ser : $"INSERT INTO {Table.DbVersion} VALUES ('{Utils.currentDbVersion}')" } - Sql.fromDataSource source + Configuration.dataSource () + |> Sql.fromDataSource |> Sql.executeTransactionAsync (sql |> Seq.map (fun s -> diff --git a/src/MyWebLog/Program.fs b/src/MyWebLog/Program.fs index 7b0c3ca..cd462bb 100644 --- a/src/MyWebLog/Program.fs +++ b/src/MyWebLog/Program.fs @@ -27,6 +27,7 @@ type WebLogMiddleware (next : RequestDelegate, log : ILogger) open System +open BitBadger.Npgsql.FSharp.Documents open Microsoft.Extensions.DependencyInjection open MyWebLog.Data open Newtonsoft.Json @@ -44,7 +45,7 @@ module DataImplementation = let builder = NpgsqlDataSourceBuilder (cfg.GetConnectionString "PostgreSQL") let _ = builder.UseNodaTime () // let _ = builder.UseLoggerFactory(LoggerFactory.Create(fun it -> it.AddConsole () |> ignore)) - builder.Build () + (builder.Build >> Configuration.useDataSource) () /// Get the configured data implementation let get (sp : IServiceProvider) : IData = @@ -68,11 +69,11 @@ module DataImplementation = let conn = await (rethinkCfg.CreateConnectionAsync log) RethinkDbData (conn, rethinkCfg, log) elif hasConnStr "PostgreSQL" then - let source = createNpgsqlDataSource config - use conn = source.CreateConnection () + createNpgsqlDataSource config + use conn = Configuration.dataSource().CreateConnection () let log = sp.GetRequiredService> () log.LogInformation $"Using PostgreSQL database {conn.Database}" - PostgresData (source, log, Json.configure (JsonSerializer.CreateDefault ())) + PostgresData (log, Json.configure (JsonSerializer.CreateDefault ())) else createSQLite "Data Source=./myweblog.db;Cache=Shared" @@ -99,7 +100,6 @@ let showHelp () = open System.IO -open System.Linq open BitBadger.AspNetCore.CanonicalDomains open Giraffe open Giraffe.EndpointRouting @@ -111,7 +111,7 @@ open NeoSmart.Caching.Sqlite open RethinkDB.DistributedCache [] -let rec main args = +let main args = let builder = WebApplication.CreateBuilder(args) let _ = builder.Services.Configure(fun (opts : ForwardedHeadersOptions) -> @@ -162,9 +162,7 @@ let rec main args = () | :? PostgresData as postgres -> // ADO.NET Data Sources are designed to work as singletons - let _ = - builder.Services.AddSingleton (fun sp -> - DataImplementation.createNpgsqlDataSource (sp.GetRequiredService ())) + let _ = builder.Services.AddSingleton (Configuration.dataSource ()) let _ = builder.Services.AddSingleton postgres let _ = builder.Services.AddSingleton (fun _ ->