Home Reference Source Repository
public class | source

Replicator

Extends:

events~EventEmitter → Replicator

Handles replication between a local and remote PouchDB database. Keeps track of state, uses an ExponentialBackoff to prevent connection saturation, serving as a wrapper over PouchDB replication.

The source database is set once in the constructor and remains unchanged and unchangeable during operation.

Constructor Summary

Public Constructor
public

constructor(logger: Logger, sourceDB: PouchDB)

Member Summary

Private Members
private

ExponentialBackoff instance.

private

_log: Bunyan

Logger instance.

private

_replication: PouchDBReplicationID

PouchDB replication instance/ID.

private

_sourceDB: PouchDB

Source database.

private

Replicator status.

private

_targetDB: PouchDB

Target database.

Method Summary

Public Methods
public

async cleanup(): Promise

Stops current replication and attempts last-minute replication.

public

replicate(dbName: String, username: String, password: String): Promise

Starts replication to a remote database.

public

stop()

Stops the replication process.

Private Methods
private
private

_createReplicator(opts: Object): PouchDBReplicationID

Creates a new PouchDB replicator.

private

Restarts the replication process when unintentionally stopped.

private

_updateState(state: String, moreLogData: Object)

Updates the Replicator's Replicator#_state.

Public Constructors

public constructor(logger: Logger, sourceDB: PouchDB) source

Params:

NameTypeAttributeDescription
logger Logger

Bunyan logger instance.

sourceDB PouchDB

Database to be replicated.

Private Members

private _backoff: ExponentialBackoff source

ExponentialBackoff instance. Keeps track of the replication backoff.

private _log: Bunyan source

Logger instance.

private _replication: PouchDBReplicationID source

PouchDB replication instance/ID.

private _sourceDB: PouchDB source

Source database.

private _state: String source

Replicator status.

private _targetDB: PouchDB source

Target database.

Public Methods

public async cleanup(): Promise source

Stops current replication and attempts last-minute replication. Last-minute replication is done in "one-shot" mode: replicates whatever data it has and immediately stops without listening for changes in the local database.

Return:

Promise

Emit:

*

state('cleanup')

public replicate(dbName: String, username: String, password: String): Promise source

Starts replication to a remote database.

Params:

NameTypeAttributeDescription
dbName String

Name/URL of the remote database.

username String
  • optional

Database username, if applicable.

password String
  • optional

Database password, if applicable.

Return:

Promise

resolved when the replication connection succeeds for the first time.

Emit:

*

state(state): the state of the replicator.

public stop() source

Stops the replication process.

Emit:

*

state('inactive')

Private Methods

private _cancelCurrentReplicator() source

private _createReplicator(opts: Object): PouchDBReplicationID source

Creates a new PouchDB replicator. Remember that this class is simply a wrapper.

Params:

NameTypeAttributeDescription
opts Object
  • optional

PouchDB replication options.

opts.live Boolean
  • optional
  • default: true

Keep the replication running, pushing changes as they happen.

opts.retry Boolean
  • optional
  • default: false

Enable PouchDB's built-in backoff algorithm.

Return:

PouchDBReplicationID

private _ensureReplication(retry: Number) source

Restarts the replication process when unintentionally stopped.

Params:

NameTypeAttributeDescription
retry Number

Retry counter

private _updateState(state: String, moreLogData: Object) source

Updates the Replicator's Replicator#_state.

Params:

NameTypeAttributeDescription
state String

New state.

moreLogData Object
  • optional

Extra data to be included in the log.

Emit:

*

state(state)