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

Add writable canned query demo to latest.datasette.io #2134

Closed
simonw opened this issue Aug 9, 2023 · 5 comments
Closed

Add writable canned query demo to latest.datasette.io #2134

simonw opened this issue Aug 9, 2023 · 5 comments

Comments

@simonw
Copy link
Owner

simonw commented Aug 9, 2023

This would be useful while working on:

@simonw
Copy link
Owner Author

simonw commented Aug 9, 2023

Here's a simple plugin that can do this:

from datasette import hookimpl

@hookimpl
def startup(datasette):
    db = datasette.add_memory_database("counters")

    async def inner():
        await db.execute_write("create table if not exists counters (name text primary key, value integer)")
        await db.execute_write("insert or ignore into counters (name, value) values ('counter', 0)")

    return inner


@hookimpl
def canned_queries(database):
    if database == "counters":
        return {
            "increment": {
                "sql": "update counters set value = value + 1 where name = 'counter'",
                "write": True,
            },
            "decrement": {
                "sql": "update counters set value = value - 1 where name = 'counter'",
                "write": True,
            },
        }

@simonw
Copy link
Owner Author

simonw commented Aug 9, 2023

Annoying thing about this plugin is that you don't see the new counter value when you submit the increment or decrement query.

Maybe canned queries should support SQL multiple statements? Could return the result of the last one.

@simonw
Copy link
Owner Author

simonw commented Aug 9, 2023

Alternatively, what about if there was a custom SQL function available during canned write queries for setting the output message? Then the fact that canned queries don't return a table view wouldn't be a problem. Bit weird though.

@simonw
Copy link
Owner Author

simonw commented Aug 9, 2023

Or... how about if the on_success_message option could define a SQL query to be executed to generate that message? Maybe on_success_message_sql.

@simonw
Copy link
Owner Author

simonw commented Aug 10, 2023

Now live at https://latest.datasette.io/counters

increment

@simonw simonw closed this as completed Aug 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant