Database
Change Events
The Changes<T> interface represents a change event emitted by change feeds. It includes the type of change and the document values before and after the change.
Interface Definition
interface Changes<T> {
changeType: "added" | "removed" | "modified";
oldValue?: T;
newValue?: T;
}
Properties
| Property | Type | Description |
|---|---|---|
changeType | "added" | "removed" | "modified" | The kind of change that occurred. |
oldValue | T | undefined | The document before the change. Undefined for inserts. |
newValue | T | undefined | The document after the change. Undefined for deletes. |
Determine the Change Type
The changeType field indicates what happened:
"added"- A new document was inserted.newValuecontains the document;oldValueis undefined."modified"- An existing document was updated. BotholdValueandnewValueare present."removed"- A document was deleted.oldValuecontains the deleted document;newValueis undefined.
Process Change Events
import { Schema } from "@antelopejs/interface-database";
const schema = Schema.get("myapp")!;
const users = schema.instance().table("users");
const changes = await users.changes();
for (const change of changes) {
switch (change.changeType) {
case "added":
console.log("New document:", change.newValue);
break;
case "modified":
console.log("Updated:", change.oldValue, "->", change.newValue);
break;
case "removed":
console.log("Deleted:", change.oldValue);
break;
}
}
Monitor a Single Document
const userChanges = await users.get("user-123").changes();
for (const change of userChanges) {
if (change.changeType === "modified") {
console.log("User updated:", change.newValue);
} else if (change.changeType === "removed") {
console.log("User deleted");
}
}
Use Cases
- Real-time notifications - Push updates to connected clients when data changes.
- Audit logging - Record every modification with before/after snapshots.
- Cache invalidation - Clear or refresh caches when underlying data is modified.
- Event-driven workflows - Trigger downstream processing based on data changes.