Skip to main content

Sync options & status

stack.sync(options)StackSyncHandle

OptionTypeDefaultMeaning
remoteRemoteResolverrequiredA URL, a PouchDB.Database, or (stack) => url or database, sync or async. Called again on every restart().
direction"push", "pull", "both""both"
livebooleantrueKeep following changes.
retrybooleantrueReconnect with PouchDB's backoff after a transient failure.
batchSizenumberPouchDB'sDocuments per batch (batch_size).
batchesLimitnumberPouchDB'sConcurrent batches (batches_limit).
heartbeatnumber or falsePouchDB'sChanges-feed heartbeat, ms.
timeoutnumber or falsePouchDB'sChanges-feed timeout, ms.
classesClassFilterOptionsall classesWhich classes replicate (below).
filter(doc) => booleannoneAn extra pure predicate, ANDed with everything else. Identified by its source text for checkpointing.
internalDocsInternalDocFilterOptions or falseDocStack's defaultsWhich of the stack's own documents stay local. false replicates everything.
checkSchemaVersionbooleantrueRefuse a remote last written by a newer schema.

ClassFilterOptions

FieldDefaultMeaning
includeallReplicate only these classes. Matched against ~class and against a relation's sourceClass/targetClass.
excludenoneNever replicate documents of these classes. Applied after include. The class models themselves still replicate; add a model's id to internalDocs.extraDocIds to keep it local too.
includeDataModeltrueKeep DATA_MODEL_CLASSES (class, ~self, domain, ~User, ~Group, ~AuthModule, ~Job and the rest) when include is set.

InternalDocFilterOptions

FieldDefaultMeaning
replicateSessionsfalseReplicate ~UserSession documents.
replicateSystemDocumentsfalseReplicate the documents the system patches seed.
replicatePatchLedgerfalseReplicate the local patch ledger.
ephemeralClassesfrom the stack's class modelsClasses whose documents never travel. Filled in by stack.sync().
extraSeededDocIdsfrom the configured patchesIds the application's patches seed. Filled in by stack.sync().
extraDocIdsnoneAdditional exact ids to keep local.
extraIdPrefixesnoneAdditional id prefixes to keep local.
extraClassesnoneAdditional ~class values to keep local.

The lists these extend are in What stays on the device.

StackSyncHandle

MemberMeaning
getStatus()The current SyncStatus.
getRemote()The resolved remote database, or null before start().
start()Resolve remote and begin replicating. Called by stack.sync().
cancel()Stop.
restart()cancel() then start(), resolving remote again. Counters and lastConvergedAt survive.
waitForConvergence(timeoutMs = 30000)Resolves with the status when a cycle finishes with nothing left to send; rejects on timeout.

Events dispatched on the handle (CustomEvent, payload in detail): status (the status object), change ({ direction, change }), idle (the status), error (the last error), denied (the last error), complete (PouchDB's completion info, one-shot replication). The stack itself dispatches sync-status with the status object, which is what useSyncStatus subscribes to.

Related stack methods: stack.getSyncHandle(), stack.getSyncStatus(), stack.cancelSync(), stack.getConsumerSchemaVersion().

SyncStatus

FieldTypeMeaning
stackstringThe stack name.
state"stopped", "starting", "active", "idle", "error", "denied"error is usually transient; denied means the remote refused a write, typically an expired or under-scoped credential.
direction"push", "pull", "both"
liveboolean
lastConvergedAtnumber or nullWhen a cycle last completed with nothing left to send. The value to render as "last synced".
lastActiveAtnumber or nullWhen documents last moved, either direction.
lastError{ name?, message, status? } or nullThe last failure, kept after recovery.
pushed, pullednumberDocuments written to the remote, and locally, since this handle started.

docstack.sync(options)DocStackSyncHandle

Every stack.sync option except remote, plus:

OptionMeaning
remoteA RemoteResolver; the function form is called once per stack.
stacksNames of the stacks to bind. Omit to bind every open stack and every stack added later (addStack).
tenantsA tenant entitlement compiled into per-stack class rules; stacks outside the scope are not synced at all. Cannot be combined with classes.

DocStackSyncHandle: handles (a Map of stack name to StackSyncHandle), names, getStatus() (a map of statuses), getLastConvergedAt() (the oldest convergence across stacks, or null), cancel(), restart(), remove(name). It re-dispatches each handle's status event.

Related: docstack.getSyncHandle(), docstack.getSyncCoverage(){ bound, unbound }, docstack.cancelSync().

SyncSchemaMismatchError

Thrown by stack.sync() before anything replicates when the remote was last written by a newer schema.

FieldMeaning
stackThe stack that refused.
scope"system" (DocStack's own patches; update the application) or "consumer" (the application's patches; apply them, or unlock so deferred ones replay).
localVersionWhat this device has applied, or undefined.
remoteVersionWhat the remote was last written with.

The remote's record is the _local/docstack-sync document (SYNC_META_DOC_ID), shared by every device that opens that remote and never replicated. readRemoteSchemaVersion, readRemoteConsumerSchemaVersion and publishSchemaVersion are exported for tooling.

StackWriteGuardError

Thrown by stack.db when application code tries to write around the authoring path: bulkDocs or put with new_edits: false or force: true, or any of the _-prefixed adapter methods (_bulkDocs, _put, _remove, _bulkGet). method names what was called. The message points at stack.sync().

Filter identity helpers

createReplicationFilter(options), createClassFilter(options), withFilterIdentity(filter, identity) and describeFilter(filter) are exported for applications that build replication filters by hand. A filter's identity is derived from its configuration, so PouchDB checkpoints resume across restarts when the configuration is unchanged and re-scan from the beginning when it changes.