Skip to content

Instantly share code, notes, and snippets.

@ulve
Created January 21, 2018 14:30
Show Gist options
  • Select an option

  • Save ulve/8af4fc3d5400b9c8c013961d23240aea to your computer and use it in GitHub Desktop.

Select an option

Save ulve/8af4fc3d5400b9c8c013961d23240aea to your computer and use it in GitHub Desktop.
Postgres from f#
// Using the super easy to use https://github.com/Zaid-Ajaj/Npgsql.FSharp
open System
open Npgsql.FSharp
type User = {
UserId : int
UserName: string
}
[<EntryPoint>]
let main _ =
let connectionString : string =
Sql.host "localhost"
|> Sql.port 5432
|> Sql.username "postgres"
|> Sql.password "password"
|> Sql.database "postgres"
|> Sql.str
let mapUserRowToUser row =
match row with
| [ "id", Int id
"username", String uname ] ->
let user =
{ UserId = id;
UserName = uname; }
Some user
| _ -> None
let getAllUsers =
connectionString
|> Sql.connect
|> Sql.query "SELECT id, username FROM users"
|> Sql.executeTable
|> Sql.mapEachRow mapUserRowToUser
getAllUsers |> Seq.iter (printfn "%A")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment