Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Readme

Simple SQLite dashboard made by townie in this video: https://x.com/stevekrouse/status/1833577807093371325

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// This val creates a SQLite dashboard admin panel with a sidebar for table names
// It uses React for the frontend and the Val Town SQLite API for database operations
// Now includes functionality to edit rows, using rowid or all columns as identifiers
// and the ability to add new rows to tables
// Column types are displayed next to column names in the UI
/** @jsxImportSource https://esm.sh/react */
import React, { useEffect, useState } from "https://esm.sh/react";
import { createRoot } from "https://esm.sh/react-dom/client";
import { vtTokenSessionAuth } from "https://esm.town/v/stevekrouse/vtTokenSessionAuthSafe";
function App() {
const [tables, setTables] = useState([]);
const [selectedTable, setSelectedTable] = useState(null);
const [tableData, setTableData] = useState([]);
const [tableSchema, setTableSchema] = useState([]);
const [error, setError] = useState(null);
const [editingRow, setEditingRow] = useState(null);
const [newRow, setNewRow] = useState({});
useEffect(() => {
fetchTables();
}, []);
const fetchTables = async () => {
try {
const response = await fetch("/tables");
const data = await response.json();
setTables(data);
} catch (err) {
setError("Failed to fetch tables");
}
};
const fetchTableData = async (tableName) => {
try {
const dataResponse = await fetch(`/table/${tableName}`);
const data = await dataResponse.json();
setTableData(data);
const schemaResponse = await fetch(`/schema/${tableName}`);
const schema = await schemaResponse.json();
setTableSchema(schema);
setSelectedTable(tableName);
// Initialize newRow with empty values for each column
const emptyRow = schema.reduce((acc, col) => {
acc[col.name] = "";
return acc;
}, {});
setNewRow(emptyRow);
} catch (err) {
setError(`Failed to fetch data for table ${tableName}`);
}
};
const handleEdit = (index) => {
setEditingRow(index);
};
const handleSave = async (index) => {
try {
const response = await fetch(`/update/${selectedTable}`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(tableData[index]),
});
if (!response.ok) {
throw new Error("Failed to update row");
}
setEditingRow(null);
await fetchTableData(selectedTable);
} catch (err) {
setError(`Failed to update row: ${err.message}`);
}
};
const handleInputChange = (index, key, value) => {
const newData = [...tableData];
newData[index][key] = value;
setTableData(newData);
};
const handleNewRowInputChange = (key, value) => {
setNewRow(prev => ({ ...prev, [key]: value }));
};
const handleAddRow = async () => {
try {
const response = await fetch(`/insert/${selectedTable}`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(newRow),
});
if (!response.ok) {
throw new Error("Failed to add new row");
stevekrouse-sqliteadmindashboard.web.val.run
September 11, 2024