Skip to content

Raw query response support#6

Merged
Brayden merged 3 commits intomainfrom
bwilmoth/raw-query
Oct 4, 2024
Merged

Raw query response support#6
Brayden merged 3 commits intomainfrom
bwilmoth/raw-query

Conversation

@Brayden
Copy link
Member

@Brayden Brayden commented Oct 4, 2024

Users expect query results from a database to come back in an array of objects, where the objects key is the column name and the value is the database row value. It works great for API responses.

Database GUI's expect results differently, in the case 0 rows are returned but still needs to render column names for example. This pull request aims to include a new route /query/raw that returns the data in a format for GUI's, an example is shown below.

Raw Query

curl --location --request POST 'https://starbasedb.brayden-b8b.workers.dev/query/raw' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer ABC123' \
--data-raw '{
    "sql": "SELECT * FROM artist;",
    "params": []
}'

Response:

{
    "result": {
        "columns": [
            "artistid",
            "artistname"
        ],
        "rows": [
            [
                123,
                "Alice"
            ],
            [
                456,
                "Bob"
            ],
            [
                789,
                "Charlie"
            ]
        ],
        "meta": {
            "rows_read": 3,
            "rows_written": 0
        }
    }
}

Normal Query

curl --location --request POST 'https://starbasedb.brayden-b8b.workers.dev/query' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer ABC123' \
--data-raw '{
    "sql": "SELECT * FROM artist;",
    "params": []
}'

Response:

{
    "result": [
        {
            "artistid": 123,
            "artistname": "Alice"
        },
        {
            "artistid": 456,
            "artistname": "Bob"
        },
        {
            "artistid": 789,
            "artistname": "Charlie"
        }
    ]
}

@Brayden Brayden merged commit 5f5255c into main Oct 4, 2024
@Brayden Brayden deleted the bwilmoth/raw-query branch October 4, 2024 18:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants