Chronicle.Client (cratis_chronicle v0.2.0)

Copy Markdown View Source

Supervisor that manages a Chronicle connection and all registered observers.

Chronicle.Client is the main entry point for the Chronicle Elixir client. Start it in your application's supervision tree, providing a connection string and the list of event types, migrations, reactors, reducers, read models, discoverable webhooks, and discoverable event store subscriptions to register.

Usage

In your Application.start/2:

def start(_type, _args) do
  children = [
    {Chronicle.Client,
      connection_string: "chronicle://localhost:35000?disableTls=true",
      event_store: "my-store",
      event_types: [
        MyApp.Events.AccountOpened,
        MyApp.Events.FundsDeposited
      ],
      migrations: [MyApp.Migrations.AccountOpenedV2Migration],
      reactors: [MyApp.Reactors.NotificationReactor],
      reducers: [MyApp.Reducers.AccountReducer],
      read_models: [MyApp.ReadModels.Account],
      webhooks: [MyApp.WebHooks.AccountEvents],
      event_store_subscriptions: [
        MyApp.EventStoreSubscriptions.DefaultAccountEvents
      ]}
  ]

  Supervisor.start_link(children, strategy: :one_for_one)
end

Or enable artifact auto-discovery from an OTP application:

{Chronicle.Client,
  connection_string: "chronicle://localhost:35000?disableTls=true",
  event_store: "my-store",
  otp_app: :my_app}

In that mode, Chronicle scans modules in :my_app and discovers artifacts generated by Chronicle macros (use Chronicle.Events.EventType, use Chronicle.Events.Migration, use Chronicle.Seeding.Seeder, use Chronicle.Reactors.Reactor, use Chronicle.Reducers.Reducer, use Chronicle.ReadModels.ReadModel, use Chronicle.WebHooks.Webhook, and use Chronicle.EventStoreSubscriptions.Subscription).

By default, Chronicle also auto-discovers artifacts from loaded modules, so consumers can start with minimal configuration.

Read models that contain from/2, join/2, or removed_with/2 declarations are automatically registered as server-side projections.

Multiple clients

You can start multiple clients with different names:

{Chronicle.Client,
  name: :bank_chronicle,
  connection_string: "chronicle://bank-server:35000",
  event_store: "bank"}

{Chronicle.Client,
  name: :crm_chronicle,
  connection_string: "chronicle://crm-server:35000",
  event_store: "crm"}

Options

  • :name — registered name for this client. Defaults to Chronicle.Client.
  • :connection_string — a connection string binary or Chronicle.Connections.ConnectionString struct. Defaults to ConnectionString.default/0 (localhost:35000).
  • :event_store — the event store name. Defaults to "default".
  • :namespace — the namespace within the event store. Defaults to "default".
  • :discover — enable/disable artifact auto-discovery. Defaults to true.
  • :otp_app — optional OTP application to auto-discover Chronicle artifacts from. When omitted, discovery scans loaded modules. Discovered artifacts are merged with explicit :event_types, :migrations, :reactors, :reducers, :read_models, :webhooks, and :event_store_subscriptions entries.
  • :event_types — list of event type modules to register with the event store.
  • :migrations — list of migration modules to register with the event store.
  • :reactors — list of reactor modules to start (each use Chronicle.Reactors.Reactor).
  • :reducers — list of reducer modules to start (each use Chronicle.Reducers.Reducer).
  • :read_models — list of read model modules (each use Chronicle.ReadModels.ReadModel). Modules that contain from/2 declarations are registered as projections.
  • :seeders — list of seeder modules (each use Chronicle.Seeding.Seeder). Seeders populate the event store with initial events during client startup.
  • :webhooks — list of discoverable webhook modules (each use Chronicle.WebHooks.Webhook). These are registered on startup.
  • :event_store_subscriptions — list of discoverable event store subscription modules (each use Chronicle.EventStoreSubscriptions.Subscription). These are registered on startup.

Convenience functions

Once started, use Chronicle.append/3 and Chronicle.read_model/3 for the most common operations, or use the subsystem modules directly:

Summary

Functions

Returns a specification to start this module under a supervisor.

Returns the stored configuration for the given client name.

Starts a Chronicle client supervisor linked to the current process.

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

config(client \\ __MODULE__)

@spec config(atom() | pid()) :: map()

Returns the stored configuration for the given client name.

Used internally by Chronicle.EventSequences.EventLog, Chronicle.ReadModels, etc.

start_link(opts \\ [])

@spec start_link(keyword()) :: Supervisor.on_start()

Starts a Chronicle client supervisor linked to the current process.