New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

firebase-event-store

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

firebase-event-store

Simple multi-tenant event sourcing store based on firestore

latest
Source
npmnpm
Version
0.2.4
Version published
Maintainers
1
Created
Source

firebase-event-store Build Status Coverage Status

A basic multi-tenant event sourcing library using firestore to record aggregates, events, and documents (read model)

Installation

npm install firebase-event-store

Usage

const {
  Aggregate,
  Command,
  Evento,
  FirestoreEventStore,
  Bus,
  ERRORS
} = require('../index')

const firestoreDb = //TODO init firesore db
const evtStore = new FirestoreEventStore(firestoreDb)
const bus = new Bus(evtStore)
bus.addEventHandler(new EventCounter(docStore))

class AddNumbers extends Command {
  validate(_) {
    if (!_.number1) throw ERRORS.INVALID_ARGUMENTS_ERROR('number1')
    if (!_.number2) throw ERRORS.INVALID_ARGUMENTS_ERROR('number2')
    this.number1 = _.number1
    this.number2 = _.number2
  }
}

class NumbersAdded extends Evento { }

class Calculator extends Aggregate {
  constructor () {
    super()
    this.sum = 0
  }

  get path () { return '/calculators' }

  async handleCommand (actor, command) {
    switch (command.constructor) {
      case AddNumbers:
        this.addEvent(actor.id, NumbersAdded, { a: command.number1, b: command.number2 })
        break
    }
  }

  applyEvent (event) {
    switch (event.constructor) {
      case NumbersAdded:
        this.creator = event.eventCreator
        this.sum += (event.a + event.b)
        break
    }
  }
}

class EventCounter extends IEventHandler {
  constructor(db) {
    super()
    this.db = db
  }

  applyEvent (actor, event, aggregate) {
    const path = '/counters/counter1'
    if (event.eventName === NumbersAdded.name) {
      let snap = await this.db.doc(path).get()
      let doc = snap.data() || {}
      doc.eventCount = (doc.eventCount || 0) + 1
      return await this.db.doc(path).set(doc)
    }
  }
}

let actor = { id: 'user1', tenant: 'tenant1' }
let calc = await bus.sendCommand(actor, Command.create(AddNumbers, { number1: 1, number2: 2 }), Calculator, 'calc1')
calc = await bus.sendCommand(actor, Command.create(AddNumbers, { number1: 3, number2: 4 }), Calculator, calc.aggregateId, calc.aggregateVersion)
console.log(calc.sum)

Tests

npm test

Contributing

In lieu of a formal style guide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code.

Keywords

firebase

FAQs

Package last updated on 28 Aug 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts