projections

command
v1.1.15 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 19, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

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 projectionsOrderSummaryProjection embeds mink.ProjectionBase and implements Apply, folding OrderCreated, ItemAdded, and OrderShipped into an OrderSummary read model synchronously via engine.ProcessInlineProjections.
  • Live projectionsOrderDashboardProjection embeds mink.LiveProjectionBase and implements OnEvent, emitting human-readable updates on a channel as events are notified via engine.NotifyLiveProjections.
  • Read model repositorymink.NewInMemoryRepository stores OrderSummary documents keyed by OrderID, with Insert/Update from the projection and Get/Find/Count for queries.
  • Checkpointsmemory.NewCheckpointStore tracks projection progress, wired into the engine with mink.WithCheckpointStore.
  • Projection statusengine.GetAllStatuses reports each projection's Name and State.

Running

go run ./examples/projections

No infrastructure required — uses the in-memory adapter.

What happens

  1. An in-memory EventStore, an OrderSummary repository, a checkpoint store, and a ProjectionEngine are created, then the engine is started. Both projections (OrderSummary inline, OrderDashboard live) are registered.
  2. Order 1 createdOrderCreated is appended (Order-001), reloaded with store.LoadRaw, and fed to ProcessInlineProjections + NotifyLiveProjections. The dashboard prints a 📦 New order line.
  3. Items added to Order 1 — two ItemAdded events are appended and processed inline, incrementing ItemCount and TotalAmount on the summary.
  4. Order 2 created — a second OrderCreated (Order-002) is appended and projected; the dashboard announces it.
  5. Order 1 shippedOrderShipped is appended; the inline projection flips Status to Shipped and the dashboard prints a 🚚 Order ... shipped line.
  6. Query read modelorderRepo.Get prints both order summaries, showing item counts, totals, and shipped status.
  7. Query builderorderRepo.Find(ctx, mink.Query{}) and orderRepo.Count(ctx, mink.Query{}) report the total number of orders.
  8. Projection statusengine.GetAllStatuses prints the name and state of each registered projection, then the demo exits.

Key APIs

  • mink.New(adapter) — create the EventStore over 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 with Insert, Update, Get, Find, Count.
  • mink.Query — the query value passed to Find/Count.
  • memory.NewCheckpointStore — in-memory checkpoint tracking.
  • store.Append / store.LoadRaw — append events and load them back as raw StoredEvents.
  • mink.ExpectVersion(mink.NoStream) — optimistic-concurrency expected-version option.

Documentation

Overview

Package main demonstrates the projection and read model features of go-mink.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL