tnightingale / pouch-redux-middleware

PouchDB Redux Middleware

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

pouch-redux-middleware

By Build Status

Redux Middleware for syncing state and a PouchDB database.

Propagates state changes into PouchDB. Propagates PouchDB changes into the state.

Install

$ npm install pouch-redux-middleware --save

Use

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
}

API

PouchMiddleware(paths)

  • 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 array
  • db: a PouchDB database
  • actions: 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 and update)
  • 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 (like type).
  • 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 are error, 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 } },
  }
}

License

ISC

About

PouchDB Redux Middleware


Languages

Language:JavaScript 100.0%