see on GitHub

Changes API: How to Subscribe to Document Changes

Following methods allow you to subscribe to document changes:

forDocument

Single document changes can be observed using forDocument() method.

Syntax

store.changes().forDocument(docId);

Parameters
docId string ID of a document for which notifications will be processed.
Return Value
IChangesObservable<DocumentChange> Observable that allows to add listeners for events for given document.

Example

store.changes().forDocument("employees/1")
    .on("error", err => {
        //handle error 
    })
    .on("data", change => {
        switch (change.type) {
            case "Put":
                // do something
                break;
            case "Delete":
                // do something
                break;
        }
    });

forDocumentsInCollection

To observe all document changes in particular collection use forDocumentInCollection() method. This method filters documents by @collection metadata property value.

Syntax

store.changes().forDocumentsInCollection(collectionName);
store.changes().forDocumentsInCollection(clazz);

Parameters
collectionName string Name of document collection for which notifications will be processed.
Return Value
IChangesObservable<DocumentChange> Observable that allows to add subscriptions to notifications for given document collection name.

Information

Overload with entity type uses conventions.getCollectionNameForType() to get collection name.

Example

store.changes().forDocumentsInCollection(Employee)
    .on("data", change => {
        console.log(change.type + " on document " + change.id);
    });

or

const collectionName = store.conventions.getCollectionNameForType(Employee);
store.changes()
    .forDocumentsInCollection(collectionName)
    .on("data", change => {
        console.log(change.type + " on document " + change.id);
    });

forDocumentsStartingWith

To observe all document changes for documents with ID that contains given prefix use forDocumentsStartingWith() method.

Syntax

store.changes().forDocumentsStartingWith(docIdPrefix);

Parameters
docIdPrefix string Document ID prefix for which notifications will be processed.
Return Value
IChangesObservable<DocumentChange> Observable that allows to add subscriptions to notifications for given document ID prefix.

Example

store.changes()
    .forDocumentsStartingWith("employees/1") // employees/1, employees/10, employees/11, etc.
    .on("data", change => {
        console.log(change.type + " on document " + change.id);
    });

forAllDocuments

To observe all document changes use forAllDocuments() method.

Syntax

store.changes().forAllDocuments();

Return Value
IChangesObservable<DocumentChange> Observable that allows to add subscriptions to notifications for all documents.

Example

store.changes().forAllDocuments()
    .on("data", change => {
        console.log(change.type + " on document " + change.id);
    });

DocumentChange

Name Type Description
type DocumentChangeTypes Document change type enum
id string Document identifier
collectionName string Document's collection name
typeName string Type name
changeVector string Document's ChangeVector

DocumentChangeTypes

Name
None
Put
Delete
BulkInsertStarted
BulkInsertEnded
BulkInsertError
DeleteOnTombstoneReplication
Conflict
Common