Is SqlHydra a wrapper around Npgsql? (or: SqlHydra vs. Npgsql) #72
-
Just getting my feet wet with F# for a new project where I also need to connect to a PostgreSQL database.
Thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Both SqlHydra and Npgsql.FSharp use the same underlying Npgsql provider library. Here are a couple things that may help you decide: SqlHydra will generate record types for your tables which you can use to create strongly typed queries using the query syntax. I personally like to have strongly typed entities to query against as it makes it harder to make mistakes. (Note that Npgsql.FSharp also has a separate query analyzer that will check your query.) I suppose a side benefit is that you could still use SqlHydra if you changed database providers, but I've rarely ever needed to do that in all my years of software development. It really just depends on which API you prefer. Npgsql.FSharp example with manually crafted SQL query: let getActiveUsers (connectionString: string) =
connectionString
|> Sql.connect
|> Sql.query "SELECT * FROM users WHERE is_active = @active"
|> Sql.parameters [ "active", Sql.bit true ]
|> Sql.executeAsync (fun read ->
{
Id = read.int "user_id"
FirstName = read.text "first_name"
LastName = read.textOrNone "last_name"
}) SqlHydra query using generated types: let getActiveUsers () =
selectAsync HydraReader.Read (Create openContext) {
for p in people.users do
where p.is_active
} |
Beta Was this translation helpful? Give feedback.
Both SqlHydra and Npgsql.FSharp use the same underlying Npgsql provider library.
Here are a couple things that may help you decide:
SqlHydra will generate record types for your tables which you can use to create strongly typed queries using the query syntax. I personally like to have strongly typed entities to query against as it makes it harder to make mistakes. (Note that Npgsql.FSharp also has a separate query analyzer that will check your query.)
I suppose a side benefit is that you could still use SqlHydra if you changed database providers, but I've rarely ever needed to do that in all my years of software development.
OTOH, if you prefer to write your SQL queries manually, then Np…