Created
January 21, 2018 14:30
-
-
Save ulve/8af4fc3d5400b9c8c013961d23240aea to your computer and use it in GitHub Desktop.
Postgres from f#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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