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.

Beginner's question about getting query lists

See original GitHub issue

Hi, I am using Npgsql.FSharp to create an API using SAFE.

I was able to connect to the database and generate records randomly. However, I fail when I try to select all records from my table.

Consideing my “table” as this type:

type Market = {
    Id : int
    Pair : string
    Quantity : int
    Price : float
}

Then, I created another type to manage my database, as well as members. Similar to the example with LiteDB/default from SAFE. A fragment from code is as follow:

type DbNpgsqlStorage() =
    let connectionString : string =
        Sql.host "localhost"
        |> Sql.database "postgres"
        |> Sql.username "postgres"
        |> Sql.password "postgres"
        |> Sql.port 5432
        |> Sql.formatConnectionString

    member __.GetMarkets () =
        connectionString
        |> Sql.connect
        |> Sql.query "SELECT Id, Pair, Price, Quantity FROM market"
        |> Sql.executeAsync (fun read ->
        {
            Id = read.int "Id"
            Pair = read.text "Pair"
            Price = read.double "Price"
            Quantity = read.int "Quantity"
        })
        |> ignore

    member __.GetCountMarkets() =
        connectionString
        |> Sql.connect
        |> Sql.query "SELECT COUNT(*) as count_markets FROM market"
        |> Sql.executeRow(fun read -> read.int "count_markets")
        |> function
            | Ok count -> count
            | Error e -> 0

The question is: considering GetMarkets(), how can I get the list from the query? When I try to use ignore the result is a “unit”. I already used match but without success.

Similar problem I had on GetCountMarkets(), then, I used a bad workaround.

Sorry, I am learning F# and SAFE this week. I have never seen them before. Consequently, I do not know how to deal with F# Railway properly.

Every help is welcome 😄

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
Zaid-Ajajcommented, Mar 9, 2021

No problem, I am glad you were able to fix the problem. I will be adding better docs soon to help with these kinds of issues 😄

0reactions
jacksonpradolimacommented, Mar 9, 2021

I do not know why but it is working now! I guess I was trying before change the column names…sorry

Thanks a lot @Zaid-Ajaj !

Read more comments on GitHub >

github_iconTop Results From Across the Web

Top 30 SQL Query Interview Questions and Answers in 2023
This article is a guide on the Top 30 SQL Query Interview Questions in 2023 which will help you ace interviews for the...
Read more >
50 SQL Query Interview Questions and Practice Exercises
Practice SQL queries and enhance your SQL skills with the 50 most frequently asked SQL query interview questions and answers.
Read more >
Top 40 SQL Query Interview Questions and Answers for ...
Here is a list of some of the most frequently asked SQL query interview questions for experienced professionals. These questions cover SQL queries...
Read more >
The Most Important SQL Queries for Beginners
This guide discusses the 30 most important SQL queries for beginners. Let's take a look.
Read more >
41 Essential SQL Interview Questions and Answers [2023]
Comprehensive, community-driven list of essential SQL interview questions. ... Write a query to to get the list of users who took the a...
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