[data-reveal]{opacity:1!important;transform:none!important}
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;
}
PropertyDescription
changeTypeThe type of change: "added", "removed", or "modified".
oldValueThe document value before the change. Undefined for new documents.
newValueThe 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.