pouchdb-event-store
v1.4.5
Published
event store on top of pouchdb
Downloads
15
Readme
pouchdb-event-store
mimimal eventStore on top of pouchdb
Usage
npm i pouchdb-event-store
const PouchDB = require('pouchdb') // or pouchdb-node / pouchdb-browser / pouchdb-react-native
const pouchdbEventStore = require('pouchdb-event-store')
let lastId = 0
const options = {
pouchdb: new PouchDB('events'),
idGenerator: {
next () { return new Promise((resolve, reject) => { resolve(lastId++) }) }
},
viewModels: [
{
pouchdb: new PouchDB('viewModel'),
reducer (state, event) {
if (!state.items) state.items = []
state.items.push(event)
return state
}
}
]
}
const eventStoreFactory = pouchdbEventStore(options)
eventStoreFactory.create()
.then((eventStore) => eventStore.add({type: 'CREATE'}, 123))
.then((result) => {
console.log(result)
})
.catch((error) => console.error(error))
eventStoreFactory.get('1')
.then((eventStore) => {
console.log(eventStore.getEvents())
})
.catch((error) => console.error(error))
// or callback style
eventStoreFactory(options).create((error, eventStore) => {
if (error) return console.error(error)
eventStore.add({type: 'CREATE'}, 123, (error, result) => {
if (error) return console.error(error)
console.log(result)
})
})
eventStoreFactory.get('2', (error, eventStore) => {
if (error) return console.error(error)
console.log(eventStore.getEvents())
})