Versioning & Time-Travel
Track changes and query historical data with automatic versioning
Enable Versioning
Add versioning to your model:
models/Document.tstypescript
import { defineModel, DataTypes } from "stabilize-orm";export const Document = defineModel({tableName: "documents",versioned: true, // Enable automatic versioningcolumns: {id: { type: DataTypes.Integer, primaryKey: true },title: { type: DataTypes.String, length: 255 },content: { type: DataTypes.Text },status: { type: DataTypes.String, length: 50 },},});
Track Changes
Every update creates a new version:
examples/track-changes.tstypescript
const docRepository = orm.getRepository(Document);// Create initial document (version 1)const doc = await docRepository.create({title: "My Document",content: "Initial content",status: "draft",});console.log("Created document, version:", doc.version); // 1// Update creates version 2await docRepository.update(doc.id, {content: "Updated content",});// Another update creates version 3await docRepository.update(doc.id, {status: "published",});console.log("Document now at version 3");
View History
Get all versions of a record:
examples/view-history.tstypescript
// Get full version historyconst history = await docRepository.history(doc.id);console.log(`Document has ${history.length} versions`);history.forEach(version => {console.log(`Version ${version.version}:`);console.log(` Title: ${version.title}`);console.log(` Status: ${version.status}`);console.log(` Changed at: ${version.modified_at}`);});
Time-Travel Queries
Query data as it existed at a specific point in time:
examples/time-travel.tstypescript
// Get document as it was on a specific dateconst pastDate = new Date("2025-01-01T00:00:00Z");const docAsOf = await docRepository.asOf(doc.id, pastDate);console.log("Document on Jan 1, 2025:");console.log(` Title: ${docAsOf.title}`);console.log(` Content: ${docAsOf.content}`);console.log(` Status: ${docAsOf.status}`);// Get a specific version (may require a helper or history lookup)const version2 = (await docRepository.history(doc.id)).find(v => v.version === 2);console.log("Version 2 content:", version2?.content);
Rollback Changes
Restore a previous version:
examples/rollback-version.tstypescript
// Rollback to version 2await docRepository.rollback(doc.id, 2);const currentDoc = await docRepository.findOne(doc.id);console.log("Rolled back to version 2");console.log("Current content:", currentDoc.content);console.log("Current version:", currentDoc.version); // Creates new version// Rollback creates a new version with old data// So if you were at version 5 and rollback to version 2,// you'll now be at version 6 with version 2's data
Audit Trail
Track who made changes and when:
examples/audit-trail.tstypescript
// Get audit trail with user informationconst auditTrail = await docRepository.history(doc.id);console.log("Change History:");auditTrail.forEach(version => {console.log(`Version ${version.version}:`);console.log(` Changed by: ${version.modified_by || "System"}`);console.log(` Changed at: ${version.modified_at}`);// You may need to diff fields manually for "changes"});
