README
¶
Projections & Read Models Example
Build queryable read models from events using inline (synchronous) and live (real-time) projections.
Event sourcing stores the history of changes, but applications usually need to query current state. This example shows how the projection engine turns an event stream into read models: an inline projection that updates a queryable OrderSummary synchronously (strong consistency), and a live projection that pushes real-time updates to a dashboard channel.
What this demonstrates
- Inline projections —
OrderSummaryProjectionembedsmink.ProjectionBaseand implementsApply, foldingOrderCreated,ItemAdded, andOrderShippedinto anOrderSummaryread model synchronously viaengine.ProcessInlineProjections. - Live projections —
OrderDashboardProjectionembedsmink.LiveProjectionBaseand implementsOnEvent, emitting human-readable updates on a channel as events are notified viaengine.NotifyLiveProjections. - Read model repository —
mink.NewInMemoryRepositorystoresOrderSummarydocuments keyed byOrderID, withInsert/Updatefrom the projection andGet/Find/Countfor queries. - Checkpoints —
memory.NewCheckpointStoretracks projection progress, wired into the engine withmink.WithCheckpointStore. - Projection status —
engine.GetAllStatusesreports each projection'sNameandState.
Running
go run ./examples/projections
No infrastructure required — uses the in-memory adapter.
What happens
- An in-memory
EventStore, anOrderSummaryrepository, a checkpoint store, and aProjectionEngineare created, then the engine is started. Both projections (OrderSummaryinline,OrderDashboardlive) are registered. - Order 1 created —
OrderCreatedis appended (Order-001), reloaded withstore.LoadRaw, and fed toProcessInlineProjections+NotifyLiveProjections. The dashboard prints a📦 New orderline. - Items added to Order 1 — two
ItemAddedevents are appended and processed inline, incrementingItemCountandTotalAmounton the summary. - Order 2 created — a second
OrderCreated(Order-002) is appended and projected; the dashboard announces it. - Order 1 shipped —
OrderShippedis appended; the inline projection flipsStatustoShippedand the dashboard prints a🚚 Order ... shippedline. - Query read model —
orderRepo.Getprints both order summaries, showing item counts, totals, and shipped status. - Query builder —
orderRepo.Find(ctx, mink.Query{})andorderRepo.Count(ctx, mink.Query{})report the total number of orders. - Projection status —
engine.GetAllStatusesprints the name and state of each registered projection, then the demo exits.
Key APIs
mink.New(adapter)— create theEventStoreover the memory adapter.mink.NewProjectionEngine(store, mink.WithCheckpointStore(...))— the engine that drives projections.engine.RegisterInline/engine.RegisterLive— register synchronous and real-time projections.engine.Start/engine.Stop— start and shut down the engine.engine.ProcessInlineProjections— synchronously apply a batch of events to inline projections.engine.NotifyLiveProjections— push a batch of events to live projections.engine.GetAllStatuses— retrieve[]*mink.ProjectionStatus(name, state, lag).mink.ProjectionBase/mink.NewProjectionBase— base for an inline projection;Apply(ctx, mink.StoredEvent)handles events.mink.LiveProjectionBase/mink.NewLiveProjectionBase— base for a live projection;OnEvent(ctx, mink.StoredEvent)reacts in real time.mink.NewInMemoryRepository— generic read model store withInsert,Update,Get,Find,Count.mink.Query— the query value passed toFind/Count.memory.NewCheckpointStore— in-memory checkpoint tracking.store.Append/store.LoadRaw— append events and load them back as rawStoredEvents.mink.ExpectVersion(mink.NoStream)— optimistic-concurrency expected-version option.
Related
- Examples: full-ecommerce · sagas · cqrs-postgres · basic
- Docs: Read Models · API reference
Click to show internal directories.
Click to hide internal directories.