feat(storage): move project data to SQLite via node:sqlite
- server/db.mjs: schema (site + projects), transactional replace, WAL, VACUUM INTO backups (keep 10), JSON export, first-run seed from data/projects.json - publish.mjs: read/write through the database, export mirror JSON for git/static/offline, validate duplicate project_id - ui/data.js: read live data from /api/projects first, fall back to data/projects.json - Dockerfile/compose/.env.example: DB_PATH, drop experimental sqlite warning flag - docs: AGENTS.md + README describe the database layer and traps
This commit is contained in:
1 parent
830dba58ec
commit
5fb095b872
11 files changed
+364
-61
No files matched your search
+24
-5
@@ -1,4 +1,5 @@
|
||||
/* data.js — ชั้นข้อมูล: อ่าน data/projects.json, ฉบับร่าง, เผยแพร่ผ่าน API ของเซิร์ฟเวอร์ */
|
||||
/* data.js — ชั้นข้อมูล: อ่านข้อมูลสดจาก API (SQLite) แล้ว fallback ไปสำเนา data/projects.json,
|
||||
ฉบับร่าง, เผยแพร่ผ่าน API ของเซิร์ฟเวอร์ */
|
||||
|
||||
export const DATA_URL = "data/projects.json";
|
||||
export const API_URL = "api/projects";
|
||||
@@ -76,12 +77,30 @@ export function createProject(index = 0) {
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------- loading */
|
||||
|
||||
/**
|
||||
* อ่านข้อมูลล่าสุด
|
||||
* 1) `api/projects` — ข้อมูลสดจากฐานข้อมูล SQLite ของเซิร์ฟเวอร์หลังบ้าน
|
||||
* 2) `data/projects.json` — สำเนาที่เซิร์ฟเวอร์ export ไว้ (ใช้ตอนเปิดแบบไฟล์นิ่ง/ออฟไลน์)
|
||||
*/
|
||||
async function fetchRemote() {
|
||||
try {
|
||||
const res = await fetch(API_URL, { cache: "no-store" });
|
||||
if (res.ok) {
|
||||
const body = await res.json();
|
||||
if (body && typeof body === "object" && body.data) return body.data;
|
||||
}
|
||||
} catch {
|
||||
/* ไม่มีเซิร์ฟเวอร์ — ลองไฟล์สำเนาต่อ */
|
||||
}
|
||||
const res = await fetch(DATA_URL, { cache: "no-store" });
|
||||
if (!res.ok) throw new Error(`HTTP ${res.status}`);
|
||||
return res.json();
|
||||
}
|
||||
|
||||
export async function loadProjects() {
|
||||
try {
|
||||
const res = await fetch(DATA_URL, { cache: "no-store" });
|
||||
if (!res.ok) throw new Error(`HTTP ${res.status}`);
|
||||
const json = await res.json();
|
||||
const data = normalizeData(json);
|
||||
const data = normalizeData(await fetchRemote());
|
||||
try {
|
||||
localStorage.setItem(CACHE_KEY, JSON.stringify(data));
|
||||
} catch {
|
||||
|
||||
+2
-2
@@ -512,10 +512,10 @@ export function adminView(ctx) {
|
||||
statusEl.dataset.state = "ok";
|
||||
textEl.textContent = "พร้อมเผยแพร่ผ่านเซิร์ฟเวอร์";
|
||||
extraEl.textContent = apiState.writable
|
||||
? "บันทึกลง data/projects.json ได้ทันที"
|
||||
? "บันทึกลงฐานข้อมูล SQLite ได้ทันที"
|
||||
: "อ่านได้ แต่เขียนไม่ได้ (ตรวจรหัสเผยแพร่)";
|
||||
hintEl.innerHTML =
|
||||
"กด “เผยแพร่” แล้วไฟล์ <code>data/projects.json</code> จะถูกเขียนทับและ commit ขึ้น Git ให้อัตโนมัติ";
|
||||
"กด “เผยแพร่” แล้วข้อมูลจะถูกบันทึกลงฐานข้อมูล SQLite (<code>data/hobbypj.sqlite</code>) พร้อม export <code>data/projects.json</code> และ commit ขึ้น Git ให้อัตโนมัติ";
|
||||
} else if (ctx.source === "cache") {
|
||||
statusEl.dataset.state = "warn";
|
||||
textEl.textContent = "เปิดแบบออฟไลน์ — เห็นข้อมูลที่แคชไว้";
|
||||
|
||||
Reference in new issue
Block a user