Database
Change Feeds
Change feeds provide real-time notifications when documents are inserted, updated, or deleted. Both Stream<T> and SingleSelection<T> expose a changes() method that returns change events.
The Changes Interface
interface Changes<T> {
changeType: "added" | "removed" | "modified";
oldValue?: T;
newValue?: T;
}
| Property | Description |
|---|---|
changeType | The type of change: "added", "removed", or "modified". |
oldValue | The document value before the change. Undefined for new documents. |
newValue | The document value after the change. Undefined for deletions. |
Create a Change Feed
Call changes() on any stream or single selection to subscribe to change events:
import { Schema } from "@antelopejs/interface-database";
const schema = Schema.get("myapp")!;
const users = schema.instance().table("users");
// Monitor all changes to a table
const allChanges = await users.changes();
// Monitor changes to a specific document
const userChanges = await users.get("user-123").changes();
Process Change Events
Each change event includes a changeType field that indicates the nature of the change:
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 from:", change.oldValue);
console.log("Updated to:", change.newValue);
break;
case "removed":
console.log("Deleted document:", change.oldValue);
break;
}
}
Use with Async Iteration
Since changes() returns a Query, you can use the cursor for streaming results:
const feed = users.changes();
for await (const batch of feed) {
for (const change of batch) {
console.log(`${change.changeType}:`, change.newValue ?? change.oldValue);
}
}
Use Cases
Change feeds are well suited for:
- Real-time UI updates - Push changes to connected clients as they happen.
- Event-driven architectures - React to data changes with downstream processing.
- Audit logging - Record every modification to a collection.
- Cache invalidation - Clear or update caches when the underlying data changes.
Stream
The Stream<T> class represents a sequence of documents that can be transformed, filtered, joined, grouped, and aggregated. It extends Query<T[]> and serves as the foundation for collection-based operations in AQL.
Query
The Query<T> class is the base class for all query types in the AQL system. It implements PromiseLike<T>, so queries can be awaited directly, and provides cursor-based async iteration for streaming results.