question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Automated conversion to a record

See original GitHub issue

I’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:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
Zaid-Ajajcommented, Sep 30, 2018

I am the one who should be thanking you, you did all the awesome work! Goodnight 😄

1reaction
bratfizykcommented, Sep 30, 2018

Yay ! I feel so smart now 😉. Thanks - now I can go to bed with a smile 😉.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found