6 ms·
I think plenty of ML languages have similar things. In F# SQL Type Providers have long been a mainstay (which makes using an ORM with F# almost meaningless IMO)
by aryonoco 7mo ago
I think plenty of ML languages have similar things. In F# SQL Type Providers have long been a mainstay (which makes using an ORM with F# almost meaningless IMO)
https://learn.microsoft.com/en-us/dotnet/fsharp/tutorials/type-providers/ https://learn.microsoft.com/en-us/dotnet/fsharp/tutorials/ty...
SQLProvider is probably the most well known one: https://fsprojects.github.io/SQLProvider/ https://fsprojects.github.io/SQLProvider/
It’s really beautiful. You get type safety with SQL. If your code compiles, you guaranteed to have valid executable both F# code and SQL. Also you get to create composable queries.
And there are other, thinner/leaner type providers as well. My favourite Postgres one is: https://github.com/Zaid-Ajaj/Npgsql.FSharp https://github.com/Zaid-Ajaj/Npgsql.FSharp
This simple sample executes query and read results as table then map the results
open Npgsql.FSharp
type User = {
Id: int
FirstName: string
LastName: string
}
let getAllUsers (connectionString: string) : User list =
connectionString
|> Sql.connect
|> Sql.query "SELECT * FROM users"
|> Sql.execute (fun read ->
{
Id = read.int "user_id"
FirstName = read.text "first_name"
LastName = read.text "last_name"
})