Search code examples
.netf#m

How to get a the ID of a field in a type F#


How do I create a function to search on a database and return just the ID of a type?

Actually, I have a database, where I can search for a pair. The pair is a Type, in F#:

module Types = 

    [<CLIMutable>]
    type Pair = {
        Id :int64
        PairName : string
    }

My objective is to create a function, that return just the value of the ID (int) given a PairName. I Already have a function that returns a Pair, given this name.

      module Query = 

        let GetSingleByName name = querySingleAsync<Types.Bird> {
            script "SELECT * FROM Pair WHERE PairName = @Name LIMIT 1"
            parameters (dict ["Name", box name])
        }

How can I return just the integer? Not all the Type.Pair?


Solution

  • let getIdByName name = querySingleAsync<int64> {
        script "SELECT Id FROM Pair WHERE PairName = @Name LIMIT 1"
        parameters (dict ["Name", box name])
    }
    

    if this does not work, you'd have to provide the definition of querySingleAsync.

    Alternatively:

    let getIdByName name = async {
        let! single = getSingleByName name
        return single |> Option.map (fun pair -> pair.Id)
    }