Skip to content

Using the projector

Trym Skaar edited this page Feb 24, 2018 · 1 revision
import {Projector} from 'ddbes'

const projector = new Projector({
  async User({
    // commitId,
    aggregateId,
    events,
    // committedAt,
  }) {
    const [, keyString] = aggregateId.split(':')

    for (const event of events) {
      switch (type) {
        case 'Created': {
          const {name, createdAt} = event

          await elasticsearch.index({
            index: 'myindex',
            type: 'User',
            id: keyString,
            body: {name, createdAt},
          })

          break
        }
        case 'Updated': {
          const {name, updatedAt} = event

          await elasticsearch.update({
            index: 'myindex',
            type: 'User',
            id: keyString,
            body: {
              doc: {name, updatedAt},
            },
          })
          break
        }
      }
    }
  },

  // async ApplicationSettings( ... ) { ... }
})

projector.start({watch: true})
// ... something happens ...
projector.stop()