Memento Database Tutorial Hot |top| | 2024-2026 |
const restoreStateAtTime = async (docId, targetTimestamp) => // Find the snapshot that was active JUST BEFORE the target timestamp const query = ` SELECT snapshot, version, created_at FROM document_history WHERE document_id = $1 AND created_at <= $2 ORDER BY created_at DESC LIMIT 1 `; const result = await pool.query(query, [docId, targetTimestamp]);
Problem: SELECT * FROM history ORDER BY created_at DESC is slow at 10 million rows. Hot Fix: Use Range Partitioning by created_at month. Also, use BRIN indexes on the timestamp column for massive speedups. memento database tutorial hot
let state = {}; for (const row of deltas.rows) state = patch(state, row.snapshot_delta); let state = {}; for (const row of deltas
};
// Usage: "Show me the document as it was 3 hours ago" const threeHoursAgo = new Date(Date.now() - 3 * 60 * 60 * 1000); const oldState = await restoreStateAtTime("doc-123", threeHoursAgo); console.log( Title back then was: $oldState.title ); Saving a full JSONB snapshot every second is expensive. The hottest trend right now is Differential Mementos (similar to Git). Only store the difference (the patch). Install a tiny library jsondiffpatch .
-- Index for fast time-travel CREATE INDEX idx_history_doc_time ON document_history(document_id, created_at DESC);
Install a tiny library jsondiffpatch .
