resolve-es
v0.23.2
Published
The reSolve framework's event store.
Downloads
740
Readme
resolve-es
Provides an event store implementation with the capability to use different storage adapters to store and emit events.
Usage
When initializing an event store, pass the following arguments:
storage
Use a reSolve framework adapter
publishEvent
The hook function after calling "eventStore.saveEvent"
Example
// Import and initializtion
import createEventStore from 'resolve-es'
import createStorageLiteAdapter from 'resolve-storage-lite'
const storage = createStorageLiteAdapter({ databaseFile: './data/event-store.db' })
const publishEvent = async (event) => {
console.log(event) // Send event to subscribers
}
const eventStore = createEventStore({
storage,
publishEvent
})
// Load events
const eventHandler = async event => {
console.log('Event from eventstore', event)
await processEvent(event)
}
const eventFilter = {
eventTypes: ['EVENT_TYPE_1', 'EVENT_TYPE_2'], // Or null to load ALL event types
aggregateIds: ['AGGREGATE_ID_1', 'AGGREGATE_ID_2'], // Or null to load ALL aggregate ids
startTime: Date.now() - 10000, // Or null to load events from beginning of time
finishTime: Date.now() + 10000 // Or null to load events to current time
}
await eventStore.loadEvents(eventFilter, eventHandler)
const latestEvent = await getLatestEvent()
// Save event
const event = {
aggregateId: '1',
aggregateVersion: 2,
type: 'UserCreated',
payload: {
email: '[email protected]'
}
}
await eventStore.saveEvent(event)