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
AsyncSQLAlchemyBackend
Bases: AsyncTransactionalBackend
Async SQLAlchemy integration backend for Event Sourcery.
Async counterpart of SQLAlchemyBackend. Uses an AsyncSession for all
event store operations, exposed via AsyncEventStore, AsyncOutbox and
AsyncSubscriptionBuilder resolved from this container.
In-transaction listeners remain synchronous callables, as they are dispatched within the append transaction.
Source code in event_sourcery_sqlalchemy/async_/__init__.py
configure(session, config=None, custom_models=None)
Sets the backend configuration for the async SQLAlchemy session.
Mirrors SQLAlchemyBackend.configure, but accepts an AsyncSession.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
AsyncSession
|
The async 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/async_/__init__.py
with_outbox(filterer=no_filter)
Enables the outbox for the backend.
Mirrors SQLAlchemyBackend.with_outbox.
Source code in event_sourcery_sqlalchemy/async_/__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
|
Source code in event_sourcery_sqlalchemy/models/__init__.py
BaseEvent
Source code in event_sourcery_sqlalchemy/models/base.py
BaseStream
Source code in event_sourcery_sqlalchemy/models/base.py
BaseOutboxEntry
Source code in event_sourcery_sqlalchemy/models/base.py
BaseProjectorCursor
Source code in event_sourcery_sqlalchemy/models/base.py
BaseSnapshot
Source code in event_sourcery_sqlalchemy/models/base.py
AsyncSqlAlchemyStorageStrategy
Bases: AsyncStorageStrategy
Async counterpart of SqlAlchemyStorageStrategy.
Stores events using an AsyncSession. Unlike the sync variant, it does not
use ORM relationship arms (stream.events.extend(...), stream.snapshots.append(...))
to associate rows — those lazy-load collections, which is not available in an
asyncio context. It assigns foreign keys (entry._db_stream_id) directly instead,
producing identical INSERT statements.
Source code in event_sourcery_sqlalchemy/async_/event_store.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 | |
AsyncSqlAlchemySubscriptionStrategy
Bases: AsyncSubscriptionStrategy
Async counterpart of SqlAlchemySubscriptionStrategy.
Polls the events table with gap detection using an AsyncSession,
yielding batches of recorded events as an async iterator.
Source code in event_sourcery_sqlalchemy/async_/subscription.py
AsyncSqlAlchemyOutboxStorageStrategy
Bases: AsyncOutboxStorageStrategy
Async counterpart of SqlAlchemyOutboxStorageStrategy.
Stores outbox entries in a relational table using an AsyncSession.
Source code in event_sourcery_sqlalchemy/async_/outbox.py
AsyncSqlAlchemyCursorsDao
Bases: AsyncCursorsDao
Async counterpart of SqlAlchemyCursorsDao: cursor persistence for
AsyncProjector, powered by an AsyncSession.