Get unlimited public & private packages + package-based permissions with npm Pro.Get started »

pouchdb-event-store

1.4.5 • Public • Published

pouchdb-event-store

mimimal eventStore on top of pouchdb

bitHound Overall Score npm Package npm Package travis-ci.org js-standard-style license

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())
})
 

Twitter URL GitHub stars

Install

npm i pouchdb-event-store

DownloadsWeekly Downloads

0

Version

1.4.5

License

MIT

Last publish

Collaborators

  • avatar