Skip to content

Instantly share code, notes, and snippets.

@wtfaremyinitials wtfaremyinitials/pouch.js
Last active Jan 7, 2018

Embed
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]: action.docs })
}
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 = res.rows.map(obj => obj.doc)
store.dispatch({ type: 'DATABASE_UPDATE', db: name, docs })
})
}
database.changes({ since: 'now', live: true }).on('change', update)
update()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.