Reputation: 129
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?
Upvotes: 0
Views: 96
Reputation: 8551
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)
}
Upvotes: 1
Reputation: 129
One of solutions that I find out was:
let GetSingleByName name = querySingleAsync<Types.Pair> {
script "SELECT * FROM Pair WHERE PairName = @Name LIMIT 1"
parameters (dict ["Name", box name])
}
let GetIdByName (name:string) = match GetSingleByName name |> Async.RunSynchronously with
| Some(pair)-> pair.Id
| None -> 0 |> int64
Upvotes: 0