Redux PouchDB Plus
About
Redux PouchDB Plus synchronizes a Redux store with a PouchDB database.
This code is heavily inspired (and some code reused) by Vicente de Alencar's redux-pouchdb. So all Kudos to him. The rewrite was necessary to allow the following extras:
- Have different Pouch databases for different reducers.
- Allow to switch databases dynamically.
- Support for Immutable states beside pure Javascript types.
- Provide several callbacks (when initialization and database access happens).
- Allow custom name for PouchDB document used by reducer. (great for multi-user applications)
The code is quite well tested using tape.
Usage
General setup
The reducers you wish to persist should be enhanced with this higher order reducer (persistentReducer
).
;const counter = {};const finalReducer = ;
Compose a store enhancer (persistentStore
) with other enhancers in order to initialize the persistence.
;const db = 'dbname';//optionalconst applyMiddlewares =;const createStoreWithMiddleware =createStore;const store = ;
You may also provide a specific database for this reducer (it is prioritized over the provided database to the store).
const db2 = 'another_dbname';const finalReducer = ;
A custom name (_id) can also be given to the document created/used for the reducer.
const finalReducer = ;
Switching databases during runtime
You may also provide a function that return a database connector instead of the connector itself. This makes it possible to switch databases dynamically during runtime.
; let dbChoice = 1;const db = { if dbChoice === 1 return 'dbname1'; else return 'dbname2';} // uses 'dbname1' databaseconst finalReducer = ; // switch to 'dbname2' databasedbChoice = 2;// reinitialize reducer counterstore;// reinitialize all reducersstore;
Check if database is in sync
With the inSync
method you can check if all state changes of persistent
reducers are saved to the database or if there is some saving in progress.
; if // do something if the reducer states and the database are in sync
Use Immutable js states
You can use Immutable.js states in your reducers. This works automatically if the initial state is an Immutable.js data type.
// automatically serializes Immutable.js data types to PouchDB// when the initial state is an Immutableconst counter = state = Immutable action ; const finalReducer = ;
It is even possible to mix immutable and plain Javascript data as internally transit-immutable-js is used for serialization to the database. Just make sure that the top state container of the reducer is an immutable.
// this is a valid stateImmutable; // this will open the doors to hell1 2 Immutable;
Provided callback functions
You may provide the following callback functions as addition options to
persistentReducer
or persistentReducer
:
// example for persistentStore, but works the same for persistentReducer function.;
Additionally you may provide an onReady
callback on the store that is called
every time all persistent recuders finished the initialization.
Notes
The current behavior is to have one document for each persisted reducer that looks like:
_id: 'reducerName' // the name the reducer function, or the name provided by {name: 'custom name'}state: {}| // the state of the reducer_rev: '' // pouchdb keeps track of the revisions