serverless-cqrs
  • Introduction
  • Quickstart
  • Domain
  • Testing
  • Examples
    • Express
    • GraphQL
  • Advanced
    • Adapter Interface
      • Write Model
      • Read Model
    • Deploy
    • Authorization
    • Eventual Consistency
    • Multiple Aggregates
  • Components
    • serverless-cqrs
    • write-model
      • repositoryBuilder
      • commandServiceBuilder
    • read-model
    • memory-adapter
    • dynamodb-adapter
    • elasticsearch-adapter
Powered by GitBook
On this page

Was this helpful?

  1. Components

read-model

PreviouscommandServiceBuilderNextmemory-adapter

Last updated 6 years ago

Was this helpful?

The read-model exports four methods: repositoryBuilder, eventServiceBuilder, queryServiceBuilder, and refreshServiceBuilder.

The query service is used to query the cached projection state of an entity. You can lookup entity by id, an array of ids, or whatever search parameters that are supported by your adapter.

The event service is used to handle incoming events from a write model subscription, and use those events to update the cached projection state. This the push approach we spoke about in .

The refresh service is used to manually load new events from the write model's datastore. This is the pull approach we spoke about in .

Example

readModel.js
const {
  repositoryBuilder,
  eventServiceBuilder,
  queryServiceBuilder,
  refreshServiceBuilder,
} = require('serverless-cqrs.read-model')

const reducer = require('./reducer')
const adapter = require('./adapter')
const eventAdapter = require('./eventAdapter')

const repository = repositoryBuilder({
  reducer,
  adapter,
})

const {
  getById,
  getByIds,
  search,
} = queryServiceBuilder.build({
  repository,
  //no eventAdapter because we don't need access to the write-models events
})

const {
  refresh,
} = refreshServiceBuilder.build({
  repository,
  eventAdapter,
})

const {
  handleEvent,
} = eventServiceBuilder.build({
  repository,
  eventAdapter,
})

module.exports = {
  getById,
  getByIds,
  search,
  refresh,
  handleEvent,
}


Eventual Consistency
Eventual Consistency