fix(db): serialize complete JSON mutations and preserve concurrent edits

Adapted from MikaelKW/nodecast-tv-plus commit 55cb8de23a37eb5683459bffed9b64c6c5dd73ae. Queue reads, validations, and writes together; return write errors to callers; preserve existing IDs and metadata; route source/user edits through the atomic operations. Cover concurrent writes, stale edits, queue recovery, and administrator races.

Co-authored-by: Mikael <mikaelkwessel@gmail.com>
This commit is contained in:
Technomancer702
2026-10-12 16:01:58 -07:00
parent 8127d26f29
commit 12080ba0a6
5 changed files with 442 additions and 404 deletions

View File

@ -92,12 +92,15 @@ router.put('/:id', async (req, res) => {
}
const { name, url, username, password } = req.body;
const updated = await sources.update(req.params.id, {
name: name || existing.name,
url: url || existing.url,
username: username !== undefined ? username : existing.username,
password: password !== undefined ? password : existing.password
});
// Omitted fields must be merged against the latest record inside the
// database transaction, not the snapshot read before another edit.
const updates = {};
if (name) updates.name = name;
if (url) updates.url = url;
if (username !== undefined) updates.username = username;
if (password !== undefined) updates.password = password;
const updated = await sources.update(req.params.id, updates);
if (!updated) return res.status(404).json({ error: 'Source not found' });
// Trigger Sync (if critical fields changed? safely just trigger it)
syncService.syncSource(parseInt(req.params.id)).catch(console.error);
res.json(updated);