🔗 Integrate with your app

Event Sourcery supports variety of backends and configurations.

Integrating it with your project requires following steps:

  1. instantiating a corresponding factory class, depending on your storage
  2. optional configuration, like enabling additional features
  3. building so-called Backend that exposes features of the library

Event Sourcery defines a few models to keep your events and streams in the database.

When working with SQLAlchemy, you'll typically use alembic to manage your migrations. Before alembic can detect Event Sourcery models, you need to register them once via configure_models:

from event_sourcery_sqlalchemy import configure_models

configure_models(Base)  # Base is your declarative base class

Once our models are registered, migrations generated and executed, you can continue.

You need an instance of Session to instantiate event_sourcery_sqlalchemy.SQLAlchemyBackend:

from event_sourcery_sqlalchemy import SQLAlchemyBackend

session = Session()  # SQLAlchemy session
backend = SQLAlchemyBackend(session)

First, you need an instance of kurrentdbclient.KurrentDBClient that represents a connection to EventStoreDB. Then, you can pass it to event_sourcery_kurrentdb.KurrentDBBackend:

from kurrentdbclient import KurrentDBClient
from event_sourcery_kurrentdb import KurrentDBBackend

client = KurrentDBClient(uri="kurrentdb://localhost:2113?Tls=false")
backend = KurrentDBBackend(client)
backend.event_store.load_stream(StreamId())  # test if connection works

Your first step will be adding "event_sourcery_django" to the list of INSTALLED_APPS in your settings.

Then you can simply create an instance of event_sourcery_django.DjangoBackend:

from event_sourcery_django import DjangoBackend

backend = DjangoBackend()

This can be done once. Then, you can import backend from other parts of code to use it.

From backend you can grab EventStore instance:

backend.event_store

You can now use EventStore to load events from a stream or append new events.