SQLAlchemyBackend
Bases: TransactionalBackend
SQLAlchemy integration backend for Event Sourcery.
Source code in event_sourcery_sqlalchemy/__init__.py
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | |
configure(session, config=None, custom_models=None)
Sets the backend configuration for SQLAlchemy session, outbox, and models.
Allows you to provide a SQLAlchemy Session instance, an optional Config instance, and optional custom ORM models. If no config or models are provided, the default configuration and models are used. This method must be called before using the backend in production to ensure correct event publishing and subscription reliability.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session |
Session
|
The SQLAlchemy session instance to use for backend operations. |
required |
config |
SQLAlchemyConfig | None
|
Optional custom configuration. If None, uses default Config(). |
None
|
custom_models |
Models | None
|
Optional custom ORM models. If None, uses default models. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Self |
Self
|
The configured backend instance (for chaining). |
Source code in event_sourcery_sqlalchemy/__init__.py
SQLAlchemyConfig
Bases: BaseModel
Configuration for SQLAlchemyBackend event store integration.
Attributes:
| Name | Type | Description |
|---|---|---|
outbox_attempts |
PositiveInt
|
Maximum number of outbox delivery attempts per event before giving up. |
gap_retry_interval |
timedelta
|
Time to wait before retrying a subscription gap. If the subscription detects a gap in event identifiers (e.g., missing event IDs), it assumes there may be an open transaction and the database has already assigned IDs for new events that are not yet committed. This interval determines how long the subscription waits before retrying to fetch events, preventing loss of events that are in the process of being written to the database. |
Source code in event_sourcery_sqlalchemy/__init__.py
Models
Container for SQLAlchemy ORM models used by the backend.
Allows customization of the event, stream, snapshot, and outbox entry models used by the backend. It allows to have different event store models for different modules in modular monolith applications still having in transactional event dispatching between them.
By default, uses the standard models provided by Event Sourcery.
Attributes:
| Name | Type | Description |
|---|---|---|
event_model |
type[BaseEvent]
|
SQLAlchemy model for events. |
stream_model |
type[BaseStream]
|
SQLAlchemy model for streams. |
snapshot_model |
type[BaseSnapshot]
|
SQLAlchemy model for snapshots. |
outbox_entry_model |
type[BaseOutboxEntry]
|
SQLAlchemy model for outbox entries (default: DefaultOutboxEntry). |
Source code in event_sourcery_sqlalchemy/__init__.py
configure_models
Configures SQLAlchemy ORM models for Event Sourcery backend.
Sets up mapping information and registers the provided (or default) models with SQLAlchemy's registry. This function allows customization of event, stream, snapshot, outbox entry, and projector cursor models for advanced scenarios, or uses the default models for standard usage. Ensures all models are mapped declaratively and share the same metadata and class registry, enabling flexible schema management and migrations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base |
type[BaseProto]
|
Base class providing SQLAlchemy MetaData for model registration. |
required |
event_model |
type[BaseEvent]
|
Event model class to use. Defaults to DefaultEvent. |
DefaultEvent
|
stream_model |
type[BaseStream]
|
Stream model class to use. Defaults to DefaultStream. |
DefaultStream
|
snapshot_model |
type[BaseSnapshot]
|
Snapshot model class to use. Defaults to DefaultSnapshot. |
DefaultSnapshot
|
outbox_entry_model |
type[BaseOutboxEntry]
|
Outbox entry model class to use. Defaults to DefaultOutboxEntry. |
DefaultOutboxEntry
|
projector_cursor_model |
type[BaseProjectorCursor]
|
Projector cursor model class to use. Defaults to DefaultProjectorCursor. |
DefaultProjectorCursor
|