demetrixbio / FSharp.Data.Npgsql

F# type providers to support statically typed access to input parameters and result set of sql statement in idiomatic F# way. Data modifications via statically typed tables.
Apache License 2.0
127 stars 16 forks source link

Support different connection string for runtime vs type generation, and offline types (maybe with a path to a file where they can be stored/cached) #98

Closed jkone27 closed 3 years ago

jkone27 commented 3 years ago

Other SQL type provider give the ability to pass another connection string , e.g. using one for schema generation , and one for production usage.

From the docs this wasn's clear on how to use it. Maybe I missed it

e.g. https://fsprojects.github.io/FSharp.Data.SqlClient/

use cmd = new SqlCommandProvider<"
        SELECT TOP(@topN) FirstName, LastName, SalesYTD 
        FROM Sales.vSalesPerson
        WHERE CountryRegionName = @regionName AND SalesYTD > @salesMoreThan 
        ORDER BY SalesYTD
        " , connectionString>(connectionString) //<<<<<<< this can be different for each environt!

or https://fsprojects.github.io/SQLProvider/core/general.html

type sql = SqlDataProvider<Common.DatabaseProviderTypes.SQLITE,
                           connectionString, /// the one used for the schema
                           ResolutionPath = resolutionPath,
                           CaseSensitivityChange = Common.CaseSensitivityChange.ORIGINAL

     \\\\then one for running queries, maybe read from secrets
let ctx = sql.GetDataContext prodConnectionString

a very good reason is e.g. CI doesn't have access to prod sql connection.

a good idea could be to store also statically in a file the schema informations, like other providers do, to allow the type provider to work OFFLINE after establishing a first successful connection/generation.

var connString = "Host=myserver;Username=mylogin;Password=mypass;Database=mydatabase";

await using var conn = new NpgsqlConnection(connString); /////// when opening and passing a connection to a type, must be a new one and an external parameter i feel like
await conn.OpenAsync();

// Insert some data
await using (var cmd = new NpgsqlCommand("INSERT INTO data (some_field) VALUES (@p)", conn))
{
    cmd.Parameters.AddWithValue("p", "Hello world");
    await cmd.ExecuteNonQueryAsync();
}
kerams commented 3 years ago

Sorry, what is the problem?

type Db = FSharp.Data.Npgsql.NpgsqlConnection<"Host=localhost;Username=postgres;Password=postgres;Database=postgres">

let deleteRememberMe cs (id: RememberMeTokenId) = async {
    use cmd = Db.CreateCommand<"delete from remember_me where id = @id">(cs)
    do! cmd.AsyncExecute %id }
kerams commented 3 years ago

a good idea could be to store also statically in a file the schema informations, like other providers do, to allow the type provider to work OFFLINE after establishing a first successful connection/generation.

That could be done easily, but it's Postgres that parses and type checks your query. Have fun recreating a full parser in .NET.

jkone27 commented 3 years ago

Hello, yes i was just re-watching 🥇 LOL and was about to remove my first part of comment :D :D :D . Thanks a lot!

jkone27 commented 3 years ago

Thanks @kerams my fault , will start using it right now!👍 Seems Awesome