This module is based on NeDB and Scuttlebutt modules.
NeDB is used as a datastore for collections. Scuttlebutt is utilized to replicate the collections and documents in the collections between peers.
Database
class has a collection map
which is a Scuttlebutt
model. Database creates the collections locally based on the
synchronization of this map. That process is setup by implementing
createStreams
and bindStreamHandlers
methods.
Database class creates a substream named map
in the createStreams
method which is directly piped to the Scuttlebutt model's
stream. bindStreamHandlers
method's purpose is to subscribe to
events announcing the substreams opened from the remote end. This
ensures that both ends of a connection does what is needed to
synchronize the collection map.
Collection
class utilizes the NeDB datastore for the underlying
database collection and a Scuttlebutt model to replicate the documents
between peers.
It uses a substream named model
to bind streams to the replication
model.
Replication model is used to store the latest version of a document by
its _id
field. All the implementation in the Collection
class is
basically mirroring the changes in the datastore to the replication model.