Redux Middleware for syncing state and a PouchDB database.
Propagates state changes into PouchDB. Propagates PouchDB changes into the state.
$ npm install pouch-redux-middleware --save
Example of configuring a store:
import * as types from '../constants/ActionTypes'
import PouchMiddleware from 'pouch-redux-middleware'
import { createStore, applyMiddleware } from 'redux'
import rootReducer from '../reducers'
import PouchDB from 'pouchdb'
export default function configureStore() {
const db = new PouchDB('todos');
const pouchMiddleware = PouchMiddleware({
path: '/todos',
db,
actions: {
remove: doc => { return { type: types.DELETE_TODO, id: doc._id } },
insert: doc => { return { type: types.INSERT_TODO, todo: doc } },
update: doc => { return { type: types.UPDATE_TODO, todo: doc } },
}
})
const store = createStore(
rootReducer,
undefined,
applyMiddleware(pouchMiddleware)
)
return store
}
paths
: path or array containing path specs
A path spec is an object describing the behaviour of a sub-tree of the state it has the following attributes:
path
: a JsonPath path where the documents will stored in the state as an arraydb
: a PouchDB databaseactions
: an object describing the actions to perform when a change in the Po. It's an object containing a function that returns an action for each of the events (remove
,insert
andupdate
)changeFilter
: a function that receives a changed document, and if it returns false, the document will be ignored for the path. This is useful when you have multiple paths in a single database that are differentiated through an attribute (liketype
).handleResponse
a function that is invoked with the direct response of the database, which is useful when metadata is needed or errors need custom handling. Arguments areerror, data, callback
.callback
must be invoked with a potential error after custom handling is done.
Example of a path spec:
{
path: '/todos',
db,
actions: {
remove: doc => { return { type: types.DELETE_TODO, id: doc._id } },
insert: doc => { return { type: types.INSERT_TODO, todo: doc } },
update: doc => { return { type: types.UPDATE_TODO, todo: doc } },
}
}
ISC