🔗 Integrate with your app
Event Sourcery supports variety of backends and configurations.
Integrating it with your project requires following steps:
- instantiating a corresponding factory class, depending on your storage
- optional configuration, like enabling additional features
- 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()
:
First, you need an instance of esdbclient.EventStoreDBClient
that represents a connection to EventStoreDB. Then, you can pass it to event_sourcery_esdb.ESDBBackendFactory
:
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:
You can now use EventStore to load events from a stream or append new events.