hindsight-core
Copyright(c) 2024
LicenseBSD3
Maintainermaintainer@example.com
Stabilityexperimental
Safe HaskellNone
LanguageGHC2021

Hindsight

Description

Hindsight is a type-safe event sourcing system that provides strong compile-time guarantees for event handling, versioning, and consistency in event-driven applications.

Getting Started

To get started with Hindsight, you'll typically want to:

  1. Define your events using Event instances and type families
  2. Choose a storage backend (Memory, Filesystem, or PostgreSQL)
  3. Set up projections to build read models
  4. Insert and query events through the event store

Example Usage

import Hindsight
import Hindsight.Store.Memory (newMemoryStore)

-- Define your event (using type-level string)
type UserRegistered = "user_registered"

data UserInfo = UserInfo { userId :: UUID, name :: Text }
  deriving (Show, Eq, Generic, FromJSON, ToJSON)

-- Declare versions and register event
type instance MaxVersion UserRegistered = 0
type instance Versions UserRegistered = '[UserInfo]

instance Event UserRegistered
instance MigrateVersion 0 UserRegistered

-- Use it
main = do
  store <- newMemoryStore
  streamId <- StreamId <$> UUID.nextRandom
  let event = mkEvent UserRegistered (UserInfo userId Alice)
  result <- insertEvents store Nothing $ singleEvent streamId NoStream event
  case result of
    SuccessfulInsertion _ -> putStrLn "Event inserted successfully"
    FailedInsertion err -> putStrLn $ "Failed: " <> show err

Storage Backends

Storage backends are in separate packages:

Core Concepts

For projection system, see the hindsight-postgresql-projections package.

Documentation