AutoCouch is a TypeScript framework to create object-oriented CRDTs that supports a simple way of distribution.
To use AutoCouch in your project add it by installing it via npm:
npm i autocouch
To define CRDTs the following steps are necessary:
- Define the user data in pure JSON that should be contained in the CRDT.
- Define the object-oriented interface of the CRDT.
- Extend
AutoCouchCRDT
. - Use the
change
function with the fitting callback to implement your interface and mutate your user data. - Use
CRDTFactory.registerType(...)
to pass the correct parameters to the constructor to create and load your object.
When an update on an object was caused by another node the local replications are notified.
To handle the update one can register handlers via the AutoCouchCRDT.on(...)
function.
If a handler is not needed anymore it can be removed via AutoCouchCRDT.off(...)
.
In some special cases like an object-oriented container CRDT manually using the ObjectRegistry
and Database
might be helpful.
Database
is a simplified wrapper of a PouchDB that allows getting and putting documents.
It can be used to save special documents of your application under a globally known ID.
ObjectRegistry
is a simple registry and cache that stores an object under its ID and loads it from the database if it is not found.
It can be used to reference objects from a CRDT container.
To choose which database holds the local replication set the environment variable AUTOCOUCH_LOCAL_DB
.
To set the address of the global database set the environment variable AUTOCOUCH_GLOBAL_DB
.
An example of how the framework is integrated in a React based web-app can be found at Polly.