🔗 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.SQLAlchemyBackendFactory, then call .build():

from event_sourcery_sqlalchemy import SQLAlchemyBackendFactory

session = Session()  # SQLAlchemy session
factory = SQLAlchemyBackendFactory(session)
backend = factory.build()

First, you need an instance of esdbclient.EventStoreDBClient that represents a connection to EventStoreDB. Then, you can pass it to event_sourcery_esdb.ESDBBackendFactory:

from esdbclient import EventStoreDBClient
from event_sourcery_esdb import ESDBBackendFactory

client = EventStoreDBClient(uri="esdb://localhost:2113?Tls=false")
factory = ESDBBackendFactory(client)
backend = factory.build()

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

Then you can create an instance of event_sourcery_django.DjangoBackendFactory right away and call .build().

from event_sourcery_django import DjangoBackendFactory

factory = DjangoBackendFactory()
backend = factory.build()

This can be done once. Then, you can import backend from other parts of code and start using 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.