-
-
Notifications
You must be signed in to change notification settings - Fork 703
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
/db/-/create
API for creating tables
#1882
Comments
API design for this:
Supported column types are:
This matches my design for |
Validating this JSON object is getting a tiny bit complex. I'm tempted to adopt https://pydantic-docs.helpmanual.io/ at this point. The from pydantic import create_model
d = {"strategy": {"name": "test_strat2", "periods": 10}}
Strategy = create_model("Strategy", **d["strategy"])
print(Strategy.schema_json(indent=2))
|
Mocked up a quick HTML+JavaScript form for creating that JSON structure using some iteration against Copilot prompts: <pre>
/* JSON format:
{
"table": {
"name": "my new table",
"columns": [
{
"name": "id",
"type": "integer"
},
{
"name": "title",
"type": "text"
}
]
"pk": "id"
}
}
HTML form with Javascript for creating this JSON:
*/</pre>
<form id="create-table-form">
<label for="table-name">Table name</label>
<input type="text" id="table-name" name="table-name" required><br>
<label for="table-pk">Primary key</label>
<input type="text" id="table-pk" name="table-pk" required><br>
<label for="column-name">Column name</label>
<input type="text" id="column-name" name="column-name" required>
<label for="column-type">Column type</label>
<input type="text" id="column-type" name="column-type" required>
<button type="button" id="add-column">Add column</button>
<p>Current columns:</p>
<ul id="columns"></ul>
<button type="button" id="create-table">Create table</button>
</form>
<script>
var form = document.getElementById('create-table-form');
var tableName = document.getElementById('table-name');
var tablePk = document.getElementById('table-pk');
var columnName = document.getElementById('column-name');
var columnType = document.getElementById('column-type');
var addColumn = document.getElementById('add-column');
var createTable = document.getElementById('create-table');
var columns = [];
addColumn.addEventListener('click', () => {
columns.push({
name: columnName.value,
type: columnType.value
});
var li = document.createElement('li');
li.textContent = columnName.value + ' (' + columnType.value + ')';
// Add a delete button to each column
var deleteButton = document.createElement('button');
deleteButton.textContent = 'Delete';
deleteButton.addEventListener('click', () => {
columns.splice(columns.indexOf(li), 1);
li.remove();
});
li.appendChild(deleteButton);
document.getElementById('columns').appendChild(li);
});
createTable.addEventListener('click', () => {
var table = {
name: tableName.value,
pk: tablePk.value,
columns: columns
};
console.log(JSON.stringify(table, null, 2));
});
</script> |
I made a decision here that this endpoint should also accept an optional |
On that basis I think the core API design should change to this:
This leaves room for a {
"table": {
"name": "my_new_table"
},
"rows": [
{"id": 1, "title": "Title"}
]
} Weird to have the table |
Thought of an edge-case: with
How could this new API support that? I thought about adding a This doesn't feel right to me - I want to keep those options here, on One idea I had was to implement it such that you can call But instead, I'm going to outsource this to the CLI tool I plan to write that feeds data into this API. I'm already planning to use that tool for CSV inserts (so the API doesn't need to accept CSV directly). I think it's a good place for other usability enhancements like "insert this, creating the table if it does not exist" as well. |
Need to validate the table name. SQLite supports almost any table name - but they can't contain a newline character and cannot start with |
What should this API return? I think the name of the table ( |
Tried out my prototype in the API explorer: The Problem is I really like |
I'm going to change it to |
Documentation:
Wrote a TIL about how I wrote some of those tests with Copilot: https://til.simonwillison.net/gpt3/writing-test-with-copilot |
Originally posted by @simonw in #1862 (comment)
The text was updated successfully, but these errors were encountered: