Automated conversion to a record
See original GitHub issueI’ve just found the library and it looks pretty decent. However, I found it annoying to be forced to write a method that converts an SqlTable to a record that represents my data.
So that, instead of writting:
let getAllUsers() : User list =
defaultConnection
|> Sql.connect
|> Sql.query "SELECT * FROM \"users\""
|> Sql.executeTable // SqlTable
|> Sql.mapEachRow (function
| [ "user_id", Int id
"first_name", String fname
"last_name", String lname ] ->
let user =
{ UserId = id;
FirstName = fname;
LastName = lname }
Some user
| _ -> None)
You can write:
let getAllUsers() : User list =
defaultConnection
|> Sql.connect
|> Sql.query "SELECT * FROM \"users\""
|> Sql.executeTable // SqlTable
|> Sql.mapEachRow<User>
The solution is pretty basic now and it’s missing some error handling, but I find it a decent starting point.
[<AutoOpen>]
module SqlExts =
type Sql with
static member toObj = function
| Short s -> box s
| Int i -> box i
| Long l -> box l
| String s -> box s
| Date dt -> box dt
| Bool b -> box b
| Number d -> box d
| Decimal d -> box d
| Bytea b -> box b
| HStore hs -> box hs
| Uuid g -> box g
| Null -> null
| Other o -> o
static member mapRow<'a> (row : SqlRow) =
if FSharpType.IsRecord typeof<'a>
then
let args =
FSharpType.GetRecordFields typeof<'a>
|> Array.map (fun pi -> row |> List.find (fun (name, _) -> pi.Name = name) |> snd |> Sql.toObj)
Some <| (FSharpValue.MakeRecord(typeof<'a>, args) :?> 'a)
else None
[<CompilationRepresentation (CompilationRepresentationFlags.ModuleSuffix)>]
module Sql =
let mapEachRow<'a> =
Sql.mapEachRow Sql.mapRow<'a>
I’m going to work in order to improve it. Are you open to pull requests ? 😃
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:8 (8 by maintainers)
Top Results From Across the Web
Automated Conversion Reduces Cost, Time for Legacy ...
The assessment is a complete research and analysis project that outlines all legacy application and database conversion candidates. This step is vital to...
Read more >What is EMR Conversion & How to automate it?
EMR conversion is converting paper patient medical records into electronic health records. EMR conversion involves patient data migration from ...
Read more >What is Automated Document Conversion & How Can It ...
Automated document conversion is a critical component of an organization's digital transformation strategy. Automating the conversion ...
Read more >Converting Records Using Workflow Rule - CRM
Converting leads, quotes or sales orders can be automated using workflow rules. When a record meets the criteria defined in the workflow rule, ......
Read more >Introduction to automated forms conversion service
Generate Document of Record during conversion; Group commonly occurring fields into reusable form fragments; Enables Adobe Analytics during conversion. It is ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

I am the one who should be thanking you, you did all the awesome work! Goodnight 😄
Yay ! I feel so smart now 😉. Thanks - now I can go to bed with a smile 😉.