-
-
Notifications
You must be signed in to change notification settings - Fork 0
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
UI on SQL query page for saving a query #7
Comments
Currently the plugin works by registering its own I'm going to switch to having dedicated interfaces instead. I want this page to grow a new "Save this query" button: https://latest.datasette.io/fixtures?sql=select+pk1%2C+pk2%2C+content+from+compound_primary_key+order+by+pk1%2C+pk2+limit+101 I'll likely use JavaScript to add this. I'll start by using a Then any time you view a query like on https://latest.datasette.io/fixtures/neighborhood_search that was previously saved there will be a "delete query" button. I'm not sure how to handle edit yet - at first I think I'll let people delete the query and then save a new version of it. You can get to "Edit SQL" from the canned query page in one click so that should hopefully be an OK flow. Can reconsider that once I have the new UI working. |
Prototype - paste this in browser devtools on a query results page: var button = document.createElement("button");
button.style.marginLeft = "2em";
button.setAttribute("id", "save-query");
button.setAttribute("type", "button");
button.appendChild(document.createTextNode("Save query"));
button.addEventListener("click", (ev) => {
ev.preventDefault();
var sql = document.getElementById("sql-editor").value;
var name = prompt("Name to use for saved query");
if (name) {
var csrftoken = document.cookie.split("; ").find(row => row.startsWith("ds_csrftoken=")).split("=")[1];
// Check if query already exists
fetch(`/data/${name}.json`).then((response) => {
if (response.status == 404) {
// POST to /data/save_query with JSON name= and sql=
fetch("/data/save_query.json", {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
credentials: "include",
// Send body as form-encoded name=/sql=/csrf_token=
body: new URLSearchParams({
name: name,
sql: sql,
csrftoken: csrftoken,
})
}).then((response) => {
if (response.status == 200) {
alert("Query saved");
}
});
} else {
alert("A query with that name already exists");
}
});
}
});
document.getElementById("sql-format").parentNode.appendChild(button); |
Simpler version - this one tries to run the query directly and spots if an integrity error is returned: var button = document.createElement("button");
button.style.marginLeft = "2em";
button.setAttribute("id", "save-query");
button.setAttribute("type", "button");
button.appendChild(document.createTextNode("Save query"));
button.addEventListener("click", (ev) => {
ev.preventDefault();
var sql = document.getElementById("sql-editor").value;
var name = prompt("Name to use for saved query");
if (name) {
var csrftoken = document.cookie.split("; ").find(row => row.startsWith("ds_csrftoken=")).split("=")[1];
fetch("/data/save_query.json", {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"Accept": "application/json"
},
credentials: "include",
body: new URLSearchParams({
name: name,
sql: sql,
csrftoken: csrftoken,
})
}).then((response) => response.json()).then((data) => {
if (data.ok) {
alert("Query saved");
} else {
alert(data.message);
}
});
}
});
document.getElementById("sql-format").parentNode.appendChild(button); Things I need to fix:
The tilde-encoding thing makes me think I'd still be better off implementing a custom route for this rather than trying to use that canned query. |
Maybe with this: |
Wahoo, great!
Should it be possible to put these data in the HTML. The
Wouldn't adding these data in the HTML code of each page allow more usages for plugins? |
Thinking about the usages, I think there are several types of queries. 1. Stable and popular/generic queries.
2. Stable and specific queries. 3. Evolving queries
"saved query" might not be a good name for them because it could leads to confusion with the "saved queries" that are never changing. Maybe these different queries could be stored in different places with different names, eg:
But maybe I'm complicating things... |
OK I really like the idea of revision tracking for queries. That could be super-useful. I actually have that as a very basic feature of |
Saved queries are a great idea. But it's hard to use, because we have to make several operations (clicks, copy/pasting, etc.) to make it work. It could be great to ease the use of it:
The text was updated successfully, but these errors were encountered: