mirror of
https://github.com/aljazceru/turso.git
synced 2025-12-17 16:44:19 +01:00
128 lines
3.5 KiB
HTML
128 lines
3.5 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
|
|
<head>
|
|
<meta charset="UTF-8" />
|
|
<title>Guestbook</title>
|
|
<style>
|
|
body {
|
|
font-family: sans-serif;
|
|
max-width: 600px;
|
|
margin: 2rem auto;
|
|
}
|
|
|
|
#comments {
|
|
border: 1px solid #ddd;
|
|
border-radius: 6px;
|
|
padding: 0;
|
|
list-style: none;
|
|
}
|
|
|
|
#comments li {
|
|
padding: 8px;
|
|
border-top: 1px solid #eee;
|
|
}
|
|
|
|
#comments li:first-child {
|
|
border-top: none;
|
|
}
|
|
|
|
.time {
|
|
font-size: 0.8em;
|
|
color: gray;
|
|
}
|
|
</style>
|
|
</head>
|
|
|
|
<body>
|
|
<h1>Guestbook</h1>
|
|
<form id="form">
|
|
<input id="comment" placeholder="Write a comment..." style="width:70%; padding:6px;" />
|
|
<button type="submit">Add</button>
|
|
</form>
|
|
<ul id="comments"></ul>
|
|
|
|
<script type="module">
|
|
import { connect } from "@tursodatabase/sync-wasm/vite";
|
|
|
|
console.info(import.meta.env.VITE_TURSO_AUTH_TOKEN);
|
|
console.info(import.meta.env.VITE_TURSO_DATABASE_URL);
|
|
let db = await connect({
|
|
path: "sync-guestbook.db", // local path to store database
|
|
authToken: import.meta.env.VITE_TURSO_AUTH_TOKEN, // Turso Cloud auth token, WARNING: Bundled into browser code, so it is NOT secret. Use carefully.
|
|
url: import.meta.env.VITE_TURSO_DATABASE_URL, // Turso Cloud database url
|
|
longPollTimeoutMs: 5000, // optional long-polling interval for pull operation; useful in case when pull called in a loop
|
|
});
|
|
// execute multiple SQL statements with exec(...)
|
|
await db.exec(`
|
|
CREATE TABLE IF NOT EXISTS guestbook ( comment TEXT, created_at DEFAULT (unixepoch()) );
|
|
CREATE INDEX IF NOT EXISTS guestbook_idx ON guestbook (created_at);
|
|
`);
|
|
await refresh();
|
|
|
|
async function pull() {
|
|
try {
|
|
// pull new data from remote
|
|
await db.pull();
|
|
await refresh();
|
|
} catch (e) {
|
|
console.error('pull error', e);
|
|
} finally {
|
|
setTimeout(pull, 0);
|
|
}
|
|
}
|
|
async function push() {
|
|
try {
|
|
// if there is something new for push
|
|
if ((await db.stats()).operations > 0) {
|
|
// push new data to remote
|
|
await db.push();
|
|
}
|
|
} catch (e) {
|
|
console.error('push error', e);
|
|
} finally {
|
|
setTimeout(push, 10);
|
|
}
|
|
}
|
|
|
|
pull();
|
|
push();
|
|
|
|
async function addComment() {
|
|
// use prepared statements and bind args to placeholders later
|
|
const insert = db.prepare(`INSERT INTO guestbook(comment) VALUES (?)`);
|
|
|
|
const input = document.getElementById("comment");
|
|
const text = input.value.trim();
|
|
input.value = "";
|
|
|
|
// use run(...) method if query only need to be executed till completion
|
|
await insert.run([text]);
|
|
|
|
await refresh();
|
|
}
|
|
document.getElementById("form").addEventListener("submit", (e) => {
|
|
e.preventDefault();
|
|
addComment();
|
|
});
|
|
|
|
async function refresh() {
|
|
const select = db.prepare(`SELECT comment, created_at FROM guestbook ORDER BY created_at DESC LIMIT ?`);
|
|
// use all(...) or get(...) methods to get all or one row from the query
|
|
const rows = await select.all([5]);
|
|
|
|
const ul = document.getElementById("comments");
|
|
ul.innerHTML = "";
|
|
rows.forEach((row) => {
|
|
const li = document.createElement("li");
|
|
li.innerHTML = `
|
|
<div>${row.comment}</div>
|
|
<div class="time">${new Date(row.created_at * 1000).toLocaleString()}</div>
|
|
`;
|
|
ul.appendChild(li);
|
|
});
|
|
}
|
|
</script>
|
|
</body>
|
|
|
|
</html> |