Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raw query response support #6

Merged
merged 3 commits into from
Oct 4, 2024
Merged

Raw query response support #6

merged 3 commits into from
Oct 4, 2024

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"
        }
    ]
}

src/operation.ts Outdated Show resolved Hide resolved
@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