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 SQLAlchemyBackend:
First, you need an instance of kurrentdbclient.KurrentDBClient that represents a connection to EventStoreDB. Then, you can pass it to KurrentDBBackend:
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 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:
You can now use EventStore to load events from a stream or append new events.