Skip to content

Instantly share code, notes, and snippets.



Last active Jan 7, 2018
What would you like to do?
Meteor-style client-side database + Redux
// Meteor-style client-side database + Redux
// kind of a hack. not idiomatic Redux
// might become an npm module at some point idk
import PouchDB from 'pouchdb'
export function databaseReducer(state = {}, action) {
if (action.type === 'DATABASE_UPDATE') {
return Object.assign({}, state, { [action.db]: })
return state
export function databaseSetup(store, name) {
const database = new PouchDB(name)
if (!window.DB) window.DB = {}
window.DB[name] = database
function update() {
// super inefficient to dump all docs each time. should really replace
// the docs listed in the change event
database.allDocs({ include_docs: true }).then(res => {
const docs = => obj.doc)
store.dispatch({ type: 'DATABASE_UPDATE', db: name, docs })
database.changes({ since: 'now', live: true }).on('change', update)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment