// ── SQL, and nothing else ───────────────────────────────────────────────── // // Core's own backend is layered `router → controller → model → db`, with models // arriving in pairs: a `.db.js` holding the SQL and a `.model.js` holding the // logic that calls it. Your module is under no obligation to copy that — the // contract says nothing about how you organise yourself — but the split earns // its keep here for the same reason it does in core: the file with the queries // in it has no branching to test, and the file with the branching in it has no // database to stand up. // // Raw parameterised SQL through `core.query`, no ORM. Placeholders always; a // value interpolated into a query string is the one mistake in this file that // nothing downstream can catch. const core = require('../../core') const TABLE = 'examplegame_world_status' /** The singleton status row, or `null` if the schema replay has not run yet. */ async function getStatus() { const rows = await core.query( `SELECT online, players, world_name AS worldName, updated_at AS updatedAt FROM ${TABLE} WHERE id = 1`, ) return rows[0] || null } /** * Overwrite the singleton. Called by whatever ingests from your sidecar. * * **`updated_at` is set explicitly, and it has to be.** MariaDB's * `ON UPDATE CURRENT_TIMESTAMP` fires only when an UPDATE actually CHANGES a * value — an update that writes the same numbers back is a no-op and leaves the * timestamp where it was. A game sitting quietly at the same player count writes * exactly that update, so the column would freeze at the first write, the row * would cross the freshness window, and the page would report the world offline * while the game was up and reporting normally. * * That is invisible to every test — the model takes its timestamps as arguments, * and nothing in a suite runs an UPDATE twice against a real database. It shows * up as a page that was right when you looked at it and wrong an hour later. */ async function setStatus({ online, players, worldName }) { await core.query( `UPDATE ${TABLE} SET online = ?, players = ?, world_name = ?, updated_at = CURRENT_TIMESTAMP WHERE id = 1`, [online ? 1 : 0, players, worldName], ) } module.exports = { getStatus, setStatus, TABLE }