[data-reveal]{opacity:1!important;transform:none!important}
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

PropertyTypeDescription
changeType"added" | "removed" | "modified"The kind of change that occurred.
oldValueT | undefinedThe document before the change. Undefined for inserts.
newValueT | undefinedThe document after the change. Undefined for deletes.

Determine the Change Type

The changeType field indicates what happened:

  • "added" - A new document was inserted. newValue contains the document; oldValue is undefined.
  • "modified" - An existing document was updated. Both oldValue and newValue are present.
  • "removed" - A document was deleted. oldValue contains the deleted document; newValue is 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.