M2: replace shape-specific backend with generic KV doc store (firebase mirror)
Backend was endpoint-mapper (POST /api/campaigns ignores path id, etc). Adapter translation layer brittle, untested, lost doc identity. Generic contract (Layer 2) test caught 15 bugs immediately. Rewrite to firebase-mirror KV model: - server/db.js: docs table (path PK, parent, data JSON). getDoc/setDoc/updateDoc/ deleteDoc/getCollection/batchWrite. parent = path prefix for collection queries. - server/index.js: generic REST (GET/PUT/PATCH/DELETE /api/doc, GET /api/collection, POST /api/collection addDoc, POST /api/batch). WS subscribe by path (doc|collection), broadcast to doc subs at changed path + collection subs at parent path. - src/storage/ws.js: thin passthrough adapter. norm() strips firebase prefix. initial value via REST (independent of WS connect), subsequent changes via WS. - shared/turn.js kept (M4 use). server/handlers.js + server.test.js removed (logic now in App, backend is dumb KV). - src/storage/contract.js: flush() bumped to 50ms (WS roundtrip > setTimeout(0)). Layer 2 test (server/ws-contract.test.js): spins fresh backend per test, runs same storage contract spec against createWsStorage. Catches adapter translation bugs that firebase-mock Layer 1 tests cannot. nanoid v5 ESM breaks jest CJS -> replaced with crypto.randomUUID (Node builtin). Tests: 114 green (39 shared + 19 ws-contract + 56 frontend).
This commit is contained in:
+83
-99
@@ -1,15 +1,16 @@
|
||||
// server/db.js — SQLite persistence layer.
|
||||
// Owns the DB file. Only writer. Synchronous via better-sqlite3.
|
||||
// server/db.js — generic KV document store on SQLite.
|
||||
// Mirrors Firestore doc-tree model: every doc lives at a string path.
|
||||
// Collections are implicit = all docs whose parent path equals the collection path.
|
||||
//
|
||||
// Schema mirrors the Firestore doc tree used by src/App.js:
|
||||
// artifacts/{APP_ID}/public/data/
|
||||
// campaigns/{id} -> name, bg, ownerId, createdAt, players[]
|
||||
// campaigns/{id}/encounters/{eid} -> name, participants[], round, currentTurnParticipantId, isStarted, isPaused, turnOrderIds[]
|
||||
// activeDisplay/status -> activeCampaignId, activeEncounterId, hidePlayerHp
|
||||
// logs/{id} -> timestamp, message, encounterName, undo, undone
|
||||
// Path examples (canonical, prefix already stripped by adapter):
|
||||
// campaigns/{id} doc
|
||||
// campaigns/{cid}/encounters/{eid} doc
|
||||
// campaigns/{cid}/encounters collection (parent of encounter docs)
|
||||
// activeDisplay/status doc
|
||||
// logs/{id} doc
|
||||
//
|
||||
// Collections (campaigns, encounters, logs) -> rows with JSON blobs for fields.
|
||||
// Single-row "status" docs (activeDisplay) -> their own tables.
|
||||
// No shape-specific tables. Data is opaque JSON. This is the firebase mirror:
|
||||
// the adapter (src/storage/ws.js) is a thin passthrough, app logic unchanged.
|
||||
|
||||
'use strict';
|
||||
|
||||
@@ -18,48 +19,14 @@ const path = require('path');
|
||||
const fs = require('fs');
|
||||
|
||||
const SCHEMA = `
|
||||
CREATE TABLE IF NOT EXISTS campaigns (
|
||||
id TEXT PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
playerDisplayBackgroundUrl TEXT NOT NULL DEFAULT '',
|
||||
ownerId TEXT,
|
||||
createdAt TEXT NOT NULL,
|
||||
players TEXT NOT NULL DEFAULT '[]'
|
||||
CREATE TABLE IF NOT EXISTS docs (
|
||||
path TEXT PRIMARY KEY,
|
||||
parent TEXT,
|
||||
data TEXT NOT NULL,
|
||||
updated_at INTEGER NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS encounters (
|
||||
id TEXT NOT NULL,
|
||||
campaignId TEXT NOT NULL,
|
||||
name TEXT NOT NULL,
|
||||
participants TEXT NOT NULL DEFAULT '[]',
|
||||
round INTEGER NOT NULL DEFAULT 0,
|
||||
currentTurnParticipantId TEXT,
|
||||
isStarted INTEGER NOT NULL DEFAULT 0,
|
||||
isPaused INTEGER NOT NULL DEFAULT 0,
|
||||
turnOrderIds TEXT NOT NULL DEFAULT '[]',
|
||||
createdAt TEXT NOT NULL,
|
||||
PRIMARY KEY (campaignId, id),
|
||||
FOREIGN KEY (campaignId) REFERENCES campaigns(id) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS active_display (
|
||||
id INTEGER PRIMARY KEY CHECK (id = 1),
|
||||
activeCampaignId TEXT,
|
||||
activeEncounterId TEXT,
|
||||
hidePlayerHp INTEGER NOT NULL DEFAULT 1
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS logs (
|
||||
id TEXT PRIMARY KEY,
|
||||
timestamp INTEGER NOT NULL,
|
||||
message TEXT NOT NULL,
|
||||
encounterName TEXT,
|
||||
undo TEXT,
|
||||
undone INTEGER NOT NULL DEFAULT 0
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_logs_timestamp ON logs(timestamp DESC);
|
||||
CREATE INDEX IF NOT EXISTS idx_encounters_campaign ON encounters(campaignId);
|
||||
CREATE INDEX IF NOT EXISTS idx_docs_parent ON docs(parent);
|
||||
`;
|
||||
|
||||
function openDb(dbPath) {
|
||||
@@ -67,60 +34,77 @@ function openDb(dbPath) {
|
||||
if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
|
||||
const db = new Database(dbPath);
|
||||
db.pragma('journal_mode = WAL');
|
||||
db.pragma('foreign_keys = ON');
|
||||
db.exec(SCHEMA);
|
||||
// Ensure the single active_display row exists.
|
||||
db.prepare('INSERT OR IGNORE INTO active_display (id, hidePlayerHp) VALUES (1, 1)').run();
|
||||
return db;
|
||||
}
|
||||
|
||||
// --- JSON helpers ---
|
||||
const parseArr = (s) => (s ? JSON.parse(s) : []);
|
||||
const parseObj = (s, fallback = null) => (s ? JSON.parse(s) : fallback);
|
||||
|
||||
// --- Campaign shape (matches Firestore doc) ---
|
||||
function rowToCampaign(row) {
|
||||
if (!row) return null;
|
||||
return {
|
||||
id: row.id,
|
||||
name: row.name,
|
||||
playerDisplayBackgroundUrl: row.playerDisplayBackgroundUrl,
|
||||
ownerId: row.ownerId,
|
||||
createdAt: row.createdAt,
|
||||
players: parseArr(row.players),
|
||||
};
|
||||
// parentOf('campaigns/abc/encounters/xyz') => 'campaigns/abc/encounters'
|
||||
// parentOf('campaigns') => null (root-level doc, no parent collection tracked)
|
||||
function parentOf(p) {
|
||||
const i = p.lastIndexOf('/');
|
||||
return i === -1 ? null : p.slice(0, i);
|
||||
}
|
||||
|
||||
// --- Encounter shape (matches Firestore doc) ---
|
||||
function rowToEncounter(row) {
|
||||
if (!row) return null;
|
||||
return {
|
||||
id: row.id,
|
||||
campaignId: row.campaignId,
|
||||
name: row.name,
|
||||
participants: parseArr(row.participants),
|
||||
round: row.round,
|
||||
currentTurnParticipantId: row.currentTurnParticipantId,
|
||||
isStarted: !!row.isStarted,
|
||||
isPaused: !!row.isPaused,
|
||||
turnOrderIds: parseArr(row.turnOrderIds),
|
||||
createdAt: row.createdAt,
|
||||
};
|
||||
function makeStore(db, broadcast) {
|
||||
const stmtGet = db.prepare('SELECT data FROM docs WHERE path = ?');
|
||||
const stmtUpsert = db.prepare(`
|
||||
INSERT INTO docs (path, parent, data, updated_at) VALUES (@path, @parent, @data, @ts)
|
||||
ON CONFLICT(path) DO UPDATE SET data = @data, updated_at = @ts
|
||||
`);
|
||||
const stmtDelete = db.prepare('DELETE FROM docs WHERE path = ?');
|
||||
const stmtColl = db.prepare('SELECT path, data FROM docs WHERE parent = ? ORDER BY path ASC');
|
||||
|
||||
function getDoc(p) {
|
||||
const row = stmtGet.get(p);
|
||||
return row ? JSON.parse(row.data) : null;
|
||||
}
|
||||
|
||||
function setDoc(p, data) {
|
||||
const ts = Date.now();
|
||||
stmtUpsert.run({ path: p, parent: parentOf(p), data: JSON.stringify(data), ts });
|
||||
if (broadcast) broadcast({ path: p, parent: parentOf(p) });
|
||||
return data;
|
||||
}
|
||||
|
||||
// shallow merge; if doc missing, patch becomes the doc (matches firebase updateDoc create-on-miss)
|
||||
function updateDoc(p, patch) {
|
||||
const existing = getDoc(p) || {};
|
||||
const merged = { ...existing, ...patch };
|
||||
setDoc(p, merged);
|
||||
return merged;
|
||||
}
|
||||
|
||||
function deleteDoc(p) {
|
||||
stmtDelete.run(p);
|
||||
if (broadcast) broadcast({ path: p, parent: parentOf(p), deleted: true });
|
||||
}
|
||||
|
||||
function getCollection(collPath) {
|
||||
return stmtColl.all(collPath).map(row => ({ id: row.path.split('/').pop(), path: row.path, ...JSON.parse(row.data) }));
|
||||
}
|
||||
|
||||
function batchWrite(ops) {
|
||||
const run = db.transaction((items) => {
|
||||
const changed = [];
|
||||
for (const op of items) {
|
||||
if (op.type === 'set') {
|
||||
setDoc(op.path, op.data);
|
||||
changed.push({ path: op.path, parent: parentOf(op.path) });
|
||||
} else if (op.type === 'delete') {
|
||||
deleteDoc(op.path);
|
||||
changed.push({ path: op.path, parent: parentOf(op.path), deleted: true });
|
||||
} else if (op.type === 'update') {
|
||||
updateDoc(op.path, op.data);
|
||||
changed.push({ path: op.path, parent: parentOf(op.path) });
|
||||
}
|
||||
}
|
||||
return changed;
|
||||
});
|
||||
const changed = run(ops);
|
||||
if (broadcast) changed.forEach(c => broadcast(c));
|
||||
}
|
||||
|
||||
return { getDoc, setDoc, updateDoc, deleteDoc, getCollection, batchWrite };
|
||||
}
|
||||
|
||||
// --- Active display shape ---
|
||||
function rowToActiveDisplay(row) {
|
||||
if (!row) return null;
|
||||
return {
|
||||
activeCampaignId: row.activeCampaignId,
|
||||
activeEncounterId: row.activeEncounterId,
|
||||
hidePlayerHp: !!row.hidePlayerHp,
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
openDb,
|
||||
rowToCampaign,
|
||||
rowToEncounter,
|
||||
rowToActiveDisplay,
|
||||
};
|
||||
module.exports = { openDb, parentOf, makeStore };
|
||||
|
||||
Reference in New Issue
Block a user