python-event-sourcery 0.6.1__py3-none-any.whl → 0.7.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. event_sourcery/__init__.py +10 -0
  2. event_sourcery/_event_store/_async/__init__.py +0 -0
  3. event_sourcery/_event_store/_async/backend.py +154 -0
  4. event_sourcery/_event_store/_async/dispatcher.py +39 -0
  5. event_sourcery/_event_store/_async/encryption.py +222 -0
  6. event_sourcery/_event_store/_async/event_store.py +268 -0
  7. event_sourcery/_event_store/_async/in_memory.py +347 -0
  8. event_sourcery/_event_store/_async/outbox.py +99 -0
  9. event_sourcery/_event_store/_async/serde.py +70 -0
  10. event_sourcery/_event_store/_async/subscription.py +202 -0
  11. event_sourcery/_event_store/backend.py +28 -23
  12. event_sourcery/_event_store/event/encryption.py +56 -14
  13. event_sourcery/_event_store/event/serde.py +62 -31
  14. event_sourcery/_event_store/event_store.py +23 -16
  15. event_sourcery/_event_store/in_memory.py +56 -33
  16. event_sourcery/_event_store/subscription/builder.py +3 -16
  17. event_sourcery/_event_store/subscription/helpers.py +15 -0
  18. event_sourcery/_event_store/subscription/in_transaction.py +29 -4
  19. event_sourcery/async_/__init__.py +13 -0
  20. event_sourcery/async_/backend.py +20 -0
  21. event_sourcery/async_/encryption.py +9 -0
  22. event_sourcery/async_/interfaces.py +13 -0
  23. event_sourcery/async_/outbox.py +11 -0
  24. event_sourcery/async_/subscription.py +13 -0
  25. event_sourcery/encryption.py +4 -0
  26. event_sourcery/event_sourcing/__init__.py +2 -0
  27. event_sourcery/event_sourcing/repository_async.py +86 -0
  28. event_sourcery/read_model/__init__.py +8 -0
  29. event_sourcery/read_model/projector_async.py +94 -0
  30. event_sourcery_django/dto.py +2 -0
  31. event_sourcery_django/event_store.py +13 -1
  32. event_sourcery_django/migrations/0002_remove_event_ix_events_stream_id_version_and_more.py +67 -0
  33. event_sourcery_django/models.py +1 -5
  34. event_sourcery_kurrentdb/async_/__init__.py +96 -0
  35. event_sourcery_kurrentdb/async_/event_store.py +133 -0
  36. event_sourcery_kurrentdb/async_/outbox.py +141 -0
  37. event_sourcery_kurrentdb/async_/subscription.py +97 -0
  38. event_sourcery_kurrentdb/event_store.py +28 -38
  39. event_sourcery_kurrentdb/outbox.py +2 -0
  40. event_sourcery_sqlalchemy/async_/__init__.py +125 -0
  41. event_sourcery_sqlalchemy/async_/cursors_dao.py +94 -0
  42. event_sourcery_sqlalchemy/async_/event_store.py +297 -0
  43. event_sourcery_sqlalchemy/async_/outbox.py +106 -0
  44. event_sourcery_sqlalchemy/async_/subscription.py +243 -0
  45. event_sourcery_sqlalchemy/event_store.py +26 -3
  46. event_sourcery_sqlalchemy/models/__init__.py +2 -3
  47. event_sourcery_sqlalchemy/outbox.py +2 -0
  48. {python_event_sourcery-0.6.1.dist-info → python_event_sourcery-0.7.0.dist-info}/METADATA +4 -2
  49. python_event_sourcery-0.7.0.dist-info/RECORD +98 -0
  50. {python_event_sourcery-0.6.1.dist-info → python_event_sourcery-0.7.0.dist-info}/WHEEL +1 -1
  51. python_event_sourcery-0.6.1.dist-info/RECORD +0 -70
  52. {python_event_sourcery-0.6.1.dist-info → python_event_sourcery-0.7.0.dist-info}/licenses/LICENSE +0 -0
@@ -1,6 +1,10 @@
1
1
  __all__ = [
2
2
  "DEFAULT_TENANT",
3
3
  "NO_VERSIONING",
4
+ "AsyncBackend",
5
+ "AsyncEventStore",
6
+ "AsyncOutbox",
7
+ "AsyncTransactionalBackend",
4
8
  "Backend",
5
9
  "Event",
6
10
  "EventStore",
@@ -12,6 +16,12 @@ __all__ = [
12
16
  "TransactionalBackend",
13
17
  ]
14
18
 
19
+ from event_sourcery._event_store._async.backend import (
20
+ AsyncBackend,
21
+ AsyncTransactionalBackend,
22
+ )
23
+ from event_sourcery._event_store._async.event_store import AsyncEventStore
24
+ from event_sourcery._event_store._async.outbox import AsyncOutbox
15
25
  from event_sourcery._event_store.backend import Backend, TransactionalBackend
16
26
  from event_sourcery._event_store.event.dto import Event
17
27
  from event_sourcery._event_store.event_store import EventStore
File without changes
@@ -0,0 +1,154 @@
1
+ from typing import cast
2
+
3
+ from typing_extensions import Self
4
+
5
+ from event_sourcery._event_store._async.dispatcher import AsyncDispatcher
6
+ from event_sourcery._event_store._async.encryption import (
7
+ AsyncEncryption,
8
+ AsyncEncryptionKeyStorageStrategy,
9
+ AsyncNoKeyStorageStrategy,
10
+ )
11
+ from event_sourcery._event_store._async.event_store import (
12
+ AsyncEventStore,
13
+ AsyncStorageStrategy,
14
+ )
15
+ from event_sourcery._event_store._async.outbox import (
16
+ AsyncNoOutboxStorageStrategy,
17
+ AsyncOutbox,
18
+ AsyncOutboxStorageStrategy,
19
+ )
20
+ from event_sourcery._event_store._async.serde import AsyncSerde
21
+ from event_sourcery._event_store._async.subscription import (
22
+ AsyncPositionPhase,
23
+ AsyncSubscriptionBuilder,
24
+ AsyncSubscriptionStrategy,
25
+ )
26
+ from event_sourcery._event_store.backend import (
27
+ ContextEnrichers,
28
+ _BackendContainer,
29
+ _Container,
30
+ not_configured,
31
+ singleton,
32
+ )
33
+ from event_sourcery._event_store.event.encryption import (
34
+ EncryptionStrategy,
35
+ )
36
+ from event_sourcery._event_store.event.registry import EventRegistry
37
+ from event_sourcery._event_store.outbox import OutboxFiltererStrategy, no_filter
38
+ from event_sourcery._event_store.subscription.in_transaction import (
39
+ Listeners,
40
+ )
41
+ from event_sourcery._event_store.tenant_id import TenantId
42
+
43
+ DEFAULT_SAS = "Use one of pyES async backends: SQLAlchemy, KurrentDB or In-Memory"
44
+
45
+
46
+ class AsyncBackend(_BackendContainer):
47
+ """
48
+ Dependency Injection container for async Event Sourcery components.
49
+
50
+ Async counterpart of `Backend`. Registers async counterparts of event
51
+ store, outbox, subscription and encryption components.
52
+ """
53
+
54
+ def __init__(self) -> None:
55
+ super().__init__()
56
+ self[AsyncEncryption] = lambda c: AsyncEncryption(
57
+ registry=c[EventRegistry],
58
+ strategy=c[EncryptionStrategy],
59
+ key_storage=c[AsyncEncryptionKeyStorageStrategy],
60
+ )
61
+ self[AsyncEncryptionKeyStorageStrategy] = (
62
+ lambda c: AsyncNoKeyStorageStrategy().scoped_for_tenant(c[TenantId])
63
+ )
64
+ self[AsyncStorageStrategy] = not_configured(DEFAULT_SAS)
65
+ self[AsyncSubscriptionStrategy] = not_configured(DEFAULT_SAS)
66
+ self[AsyncOutboxStorageStrategy] = lambda _: AsyncNoOutboxStorageStrategy()
67
+ self[AsyncEventStore] = lambda c: AsyncEventStore(
68
+ storage_strategy=c[AsyncStorageStrategy],
69
+ serde=self._serde_for(c),
70
+ context_enrichers=c[ContextEnrichers],
71
+ )
72
+ self[AsyncOutbox] = lambda c: AsyncOutbox(
73
+ strategy=c[AsyncOutboxStorageStrategy],
74
+ serde=self._serde_for(c),
75
+ )
76
+ self[AsyncPositionPhase] = lambda c: AsyncSubscriptionBuilder(
77
+ self._serde_for(c),
78
+ c[AsyncSubscriptionStrategy],
79
+ )
80
+
81
+ @staticmethod
82
+ def _serde_for(container: _Container) -> AsyncSerde:
83
+ return AsyncSerde(
84
+ registry=container[EventRegistry],
85
+ encryption=container[AsyncEncryption],
86
+ )
87
+
88
+ def with_outbox(self, filterer: OutboxFiltererStrategy = no_filter) -> Self:
89
+ """
90
+ Configure the outbox with a custom filter.
91
+ """
92
+ raise NotImplementedError()
93
+
94
+ def with_encryption(
95
+ self,
96
+ strategy: EncryptionStrategy,
97
+ key_storage: AsyncEncryptionKeyStorageStrategy,
98
+ ) -> Self:
99
+ """
100
+ Configures event encryption with the provided strategy and key storage.
101
+
102
+ Key storage operations are asynchronous.
103
+ """
104
+ self[EncryptionStrategy] = strategy
105
+ self[AsyncEncryptionKeyStorageStrategy] = (
106
+ lambda c: key_storage.scoped_for_tenant(c[TenantId])
107
+ )
108
+ return self
109
+
110
+ @property
111
+ def event_store(self) -> AsyncEventStore:
112
+ """
113
+ Returns the current instance of `AsyncEventStore`.
114
+ """
115
+ return self[AsyncEventStore]
116
+
117
+ @property
118
+ def outbox(self) -> AsyncOutbox:
119
+ """
120
+ Returns the current instance of `AsyncOutbox`.
121
+ """
122
+ return self[AsyncOutbox]
123
+
124
+ @property
125
+ def subscriber(self) -> AsyncPositionPhase:
126
+ """
127
+ Returns the current instance of `AsyncSubscriptionBuilder`
128
+ (as `AsyncPositionPhase`).
129
+ """
130
+ return self[AsyncPositionPhase]
131
+
132
+
133
+ class AsyncTransactionalBackend(AsyncBackend):
134
+ """
135
+ Async backend variant that provides transactional event handling support.
136
+
137
+ Note: in-transaction listeners remain synchronous callables even on async
138
+ backends, as they are dispatched within the append transaction.
139
+ """
140
+
141
+ def __init__(self) -> None:
142
+ super().__init__()
143
+ self[Listeners] = singleton(lambda _: Listeners())
144
+ self[AsyncDispatcher] = lambda c: AsyncDispatcher(
145
+ AsyncBackend._serde_for(c),
146
+ c[Listeners],
147
+ )
148
+
149
+ @property
150
+ def in_transaction(self) -> Listeners:
151
+ """
152
+ Returns the current instance of `Listeners` for transactional event handling.
153
+ """
154
+ return cast(Listeners, self[Listeners])
@@ -0,0 +1,39 @@
1
+ from typing import cast
2
+
3
+ from event_sourcery._event_store._async.serde import AsyncSerde
4
+ from event_sourcery._event_store.event.dto import Event, Recorded, RecordedRaw
5
+ from event_sourcery._event_store.subscription.in_transaction import Listener, Listeners
6
+
7
+
8
+ class AsyncDispatcher:
9
+ """Awaits event deserialization before invoking synchronous listeners."""
10
+
11
+ def __init__(self, serde: AsyncSerde, listeners: Listeners) -> None:
12
+ self._serde = serde
13
+ self._listeners = listeners
14
+
15
+ async def dispatch(self, *raws: RecordedRaw) -> None:
16
+ self.dispatch_prepared(await self.prepare(*raws))
17
+
18
+ async def prepare(self, *raws: RecordedRaw) -> list[tuple[Recorded, set[Listener]]]:
19
+ prepared: list[tuple[Recorded, set[Listener]]] = []
20
+ for raw in raws:
21
+ event = cast(
22
+ type[Event], self._serde.registry.type_for_name(raw.entry.name)
23
+ )
24
+ category = raw.entry.stream_id.category or ""
25
+ listeners = set(self._listeners[event]) | set(self._listeners[category])
26
+ if listeners:
27
+ prepared.append((await self._serde.deserialize_record(raw), listeners))
28
+ return prepared
29
+
30
+ @staticmethod
31
+ def dispatch_prepared(prepared: list[tuple[Recorded, set[Listener]]]) -> None:
32
+ for record, listeners in prepared:
33
+ for listener in listeners:
34
+ listener(
35
+ record.wrapped_event,
36
+ record.stream_id,
37
+ record.tenant_id,
38
+ record.position,
39
+ )
@@ -0,0 +1,222 @@
1
+ import json
2
+ from dataclasses import dataclass
3
+ from typing import Any
4
+
5
+ from pydantic import BaseModel
6
+ from typing_extensions import Self
7
+
8
+ from event_sourcery._event_store.event.encryption import (
9
+ EncryptionStrategy,
10
+ NestedDict,
11
+ _fields_with_subjects,
12
+ _fields_with_subjects_in_data,
13
+ )
14
+ from event_sourcery._event_store.event.registry import EventRegistry
15
+ from event_sourcery._event_store.stream_id import StreamId
16
+ from event_sourcery._event_store.tenant_id import TenantId
17
+ from event_sourcery.exceptions import KeyNotFoundError
18
+
19
+
20
+ class AsyncEncryptionKeyStorageStrategy:
21
+ """
22
+ Interface for async key management strategies used in event encryption.
23
+
24
+ Async counterpart of `EncryptionKeyStorageStrategy`. Operations that
25
+ perform I/O are coroutines. Implementations are responsible for storing,
26
+ retrieving, and deleting encryption keys for data subjects. Supports
27
+ multi-tenancy via `scoped_for_tenant`.
28
+ """
29
+
30
+ async def get(self, subject_id: str) -> bytes | None:
31
+ """
32
+ Retrieves the encryption key for the given subject identifier.
33
+
34
+ Args:
35
+ subject_id (str): The subject identifier.
36
+
37
+ Returns:
38
+ bytes | None: The encryption key, or None if not found.
39
+ """
40
+ raise NotImplementedError()
41
+
42
+ async def store(self, subject_id: str, key: bytes) -> None:
43
+ """
44
+ Stores the encryption key for the given subject identifier.
45
+
46
+ Args:
47
+ subject_id (str): The subject identifier.
48
+ key (bytes): The encryption key to store.
49
+ """
50
+ raise NotImplementedError()
51
+
52
+ async def delete(self, subject_id: str) -> None:
53
+ """
54
+ Deletes the encryption key for the given subject identifier.
55
+
56
+ Args:
57
+ subject_id (str): The subject identifier whose key should be deleted.
58
+ """
59
+ raise NotImplementedError()
60
+
61
+ def scoped_for_tenant(self, tenant_id: TenantId) -> Self:
62
+ """
63
+ Returns a key storage strategy instance scoped for the given tenant.
64
+
65
+ Args:
66
+ tenant_id (TenantId): The tenant identifier.
67
+
68
+ Returns:
69
+ Self: The key storage strategy instance for the tenant.
70
+ """
71
+ raise NotImplementedError()
72
+
73
+
74
+ class AsyncNoKeyStorageStrategy(AsyncEncryptionKeyStorageStrategy):
75
+ """
76
+ Default async key storage strategy rejecting all key operations.
77
+
78
+ Async counterpart of `NoKeyStorageStrategy`.
79
+ """
80
+
81
+ async def get(self, subject_id: str) -> bytes | None:
82
+ raise NotImplementedError(
83
+ "AsyncNoKeyStorageStrategy does not support key retrieval."
84
+ )
85
+
86
+ async def store(self, subject_id: str, key: bytes) -> None:
87
+ raise NotImplementedError(
88
+ "AsyncNoKeyStorageStrategy does not support key storage."
89
+ )
90
+
91
+ async def delete(self, subject_id: str) -> None:
92
+ raise NotImplementedError(
93
+ "AsyncNoKeyStorageStrategy does not support key deletion."
94
+ )
95
+
96
+ def scoped_for_tenant(self, tenant_id: TenantId) -> Self:
97
+ return self
98
+
99
+
100
+ @dataclass
101
+ class AsyncEncryption:
102
+ """
103
+ Async counterpart of `Encryption`. Integrates encryption logic and async
104
+ key management via the `AsyncEncryptionKeyStorageStrategy` interface.
105
+ """
106
+
107
+ registry: EventRegistry
108
+ strategy: EncryptionStrategy
109
+ key_storage: AsyncEncryptionKeyStorageStrategy
110
+
111
+ async def encrypt(self, event: BaseModel, stream_id: StreamId) -> dict[str, Any]:
112
+ """
113
+ Encrypts all fields of the event marked as encrypted.
114
+
115
+ Args:
116
+ event (BaseModel): The event instance to encrypt.
117
+ stream_id (StreamId): The stream identifier used for subject resolution.
118
+
119
+ Returns:
120
+ dict[str, Any]: The event data with encrypted fields.
121
+
122
+ Raises:
123
+ NoSubjectIdFound: If the subject id cannot be determined for encryption.
124
+ KeyNotFoundError: If the encryption key for a subject is missing.
125
+ """
126
+ data = NestedDict(event.model_dump(mode="json"))
127
+ for field_name, subject_id in _fields_with_subjects(
128
+ self.registry,
129
+ event,
130
+ stream_id,
131
+ ):
132
+ data[field_name] = await _encrypt_value(
133
+ self.strategy,
134
+ self.key_storage,
135
+ data[field_name],
136
+ subject_id,
137
+ )
138
+ return data.data
139
+
140
+ async def decrypt(
141
+ self,
142
+ event_type: type[BaseModel],
143
+ raw: dict[str, Any],
144
+ stream_id: StreamId,
145
+ ) -> dict[str, Any]:
146
+ """
147
+ Decrypts all fields of the event marked as encrypted in the registry.
148
+
149
+ Args:
150
+ event_type (type[BaseModel]): The event class type.
151
+ raw (dict[str, Any]): The raw event data with encrypted fields.
152
+ stream_id (StreamId): The stream identifier used for subject resolution.
153
+
154
+ Returns:
155
+ dict[str, Any]: The event data with decrypted fields (or masked if no key).
156
+ """
157
+ data = NestedDict(raw)
158
+ for field_name, subject_id, mask_value in _fields_with_subjects_in_data(
159
+ self.registry,
160
+ event_type,
161
+ data,
162
+ stream_id,
163
+ ):
164
+ data[field_name] = await _decrypt_value(
165
+ self.strategy,
166
+ self.key_storage,
167
+ data[field_name],
168
+ subject_id,
169
+ mask_value,
170
+ )
171
+ return data.data
172
+
173
+ async def ashred(self, subject_id: str) -> None:
174
+ """
175
+ Deletes the encryption key for the given subject, effectively making all
176
+ encrypted data for that subject unrecoverable (crypto-shredding).
177
+
178
+ Args:
179
+ subject_id (str): The subject identifier whose key should be deleted.
180
+ """
181
+ await self.key_storage.delete(subject_id)
182
+
183
+
184
+ async def _decrypt_value(
185
+ strategy: EncryptionStrategy,
186
+ key_storage: AsyncEncryptionKeyStorageStrategy,
187
+ value: str,
188
+ subject_id: str,
189
+ mask_value: Any,
190
+ ) -> Any:
191
+ key = await key_storage.get(subject_id)
192
+ if key is None:
193
+ return mask_value
194
+ decrypted = strategy.decrypt(value, key)
195
+ return _deserialize(decrypted)
196
+
197
+
198
+ async def _encrypt_value(
199
+ strategy: EncryptionStrategy,
200
+ key_storage: AsyncEncryptionKeyStorageStrategy,
201
+ value: Any,
202
+ subject_id: str,
203
+ ) -> str:
204
+ key = await key_storage.get(subject_id)
205
+ if key is None:
206
+ raise KeyNotFoundError(subject_id)
207
+ return strategy.encrypt(_serialize(value), key)
208
+
209
+
210
+ def _serialize(value: Any) -> str:
211
+ match value:
212
+ case str():
213
+ return value
214
+ case _:
215
+ return json.dumps(value)
216
+
217
+
218
+ def _deserialize(value: Any) -> Any:
219
+ try:
220
+ return json.loads(value)
221
+ except (json.JSONDecodeError, TypeError):
222
+ return value
@@ -0,0 +1,268 @@
1
+ from collections.abc import Sequence
2
+ from functools import singledispatchmethod
3
+ from typing import cast
4
+
5
+ from typing_extensions import Self
6
+
7
+ from event_sourcery._event_store._async.serde import AsyncSerde
8
+ from event_sourcery._event_store.event.dto import (
9
+ Event,
10
+ Position,
11
+ RawEvent,
12
+ WrappedEvent,
13
+ )
14
+ from event_sourcery._event_store.event_store import (
15
+ EventContextEnricher,
16
+ enrich_event,
17
+ )
18
+ from event_sourcery._event_store.stream_id import StreamId
19
+ from event_sourcery._event_store.versioning import (
20
+ NO_VERSIONING,
21
+ ExplicitVersioning,
22
+ Versioning,
23
+ )
24
+
25
+
26
+ class AsyncStorageStrategy:
27
+ """
28
+ Interface for async event store backends.
29
+
30
+ Async counterpart of `StorageStrategy`. Defines the contract for low-level
31
+ operations on event streams, such as fetching, inserting, and deleting events.
32
+ All operations that perform I/O are coroutines.
33
+ """
34
+
35
+ async def fetch_events(
36
+ self,
37
+ stream_id: StreamId,
38
+ start: int | None = None,
39
+ stop: int | None = None,
40
+ ) -> list[RawEvent]:
41
+ """
42
+ Fetches events from a stream in the given range.
43
+
44
+ Args:
45
+ stream_id (StreamId): The stream identifier to fetch events from.
46
+ start (int | None): From version (inclusive), or None for the beginning.
47
+ stop (int | None): Stop before version (exclusive), or None for the end.
48
+
49
+ Returns:
50
+ list[RawEvent]: List of raw events in the specified range.
51
+ """
52
+ raise NotImplementedError()
53
+
54
+ async def insert_events(
55
+ self,
56
+ stream_id: StreamId,
57
+ versioning: Versioning,
58
+ events: list[RawEvent],
59
+ ) -> None:
60
+ """
61
+ Inserts events into a stream with using versioning strategy.
62
+
63
+ Args:
64
+ stream_id (StreamId): The stream identifier to insert events into.
65
+ versioning (Versioning): Versioning strategy for optimistic locking.
66
+ events (list[RawEvent]): List of raw events to insert.
67
+ """
68
+ raise NotImplementedError()
69
+
70
+ async def save_snapshot(self, snapshot: RawEvent) -> None:
71
+ """
72
+ Saves a snapshot of the stream. Stream will be fetched from newest snapshot.
73
+
74
+ Args:
75
+ snapshot (RawEvent): The snapshot event to save.
76
+ """
77
+ raise NotImplementedError()
78
+
79
+ async def delete_stream(self, stream_id: StreamId) -> None:
80
+ """
81
+ Deletes a stream and all its events.
82
+
83
+ Args:
84
+ stream_id (StreamId): The stream identifier to delete.
85
+ """
86
+ raise NotImplementedError()
87
+
88
+ async def current_position(self) -> Position | None:
89
+ """
90
+ Returns the current position (offset) in the event store, if supported.
91
+
92
+ Unlike its synchronous counterpart, this is a coroutine, as fetching
93
+ the position performs I/O in real backends.
94
+ """
95
+ raise NotImplementedError()
96
+
97
+ def scoped_for_tenant(self, tenant_id: str) -> Self:
98
+ """
99
+ Returns a backend instance scoped for the given tenant.
100
+
101
+ Args:
102
+ tenant_id (str): The tenant identifier.
103
+
104
+ Returns:
105
+ Self: The backend instance for the tenant.
106
+ """
107
+ raise NotImplementedError()
108
+
109
+
110
+ class AsyncEventStore:
111
+ """
112
+ Async API for working with events.
113
+
114
+ Async counterpart of `EventStore`. All operations are coroutines and
115
+ must be awaited.
116
+ """
117
+
118
+ def __init__(
119
+ self,
120
+ storage_strategy: AsyncStorageStrategy,
121
+ serde: AsyncSerde,
122
+ context_enrichers: tuple[EventContextEnricher, ...] = (),
123
+ ) -> None:
124
+ self._storage_strategy = storage_strategy
125
+ self._serde = serde
126
+ self._context_enrichers = context_enrichers
127
+
128
+ async def load_stream(
129
+ self,
130
+ stream_id: StreamId,
131
+ start: int | None = None,
132
+ stop: int | None = None,
133
+ ) -> Sequence[WrappedEvent]:
134
+ """Loads events from a given stream.
135
+
136
+ Args:
137
+ stream_id: The stream identifier to load events from.
138
+ start: The stream version to start loading from (including).
139
+ stop: The stream version to stop loading at (excluding).
140
+
141
+ Returns:
142
+ A sequence of events or empty list if the stream doesn't exist.
143
+ """
144
+ events = await self._storage_strategy.fetch_events(
145
+ stream_id, start=start, stop=stop
146
+ )
147
+ return [await self._serde.deserialize(event) for event in events]
148
+
149
+ @singledispatchmethod
150
+ async def append(
151
+ self,
152
+ first: WrappedEvent,
153
+ *events: WrappedEvent,
154
+ stream_id: StreamId,
155
+ expected_version: int | Versioning = 0,
156
+ ) -> None:
157
+ """Appends events to a stream with a given ID.
158
+
159
+ Implements optimistic locking to ensure stream wasn't modified since last read.
160
+ To use it, pass the expected version of the stream.
161
+
162
+ Args:
163
+ first: The first event to append (WrappedEvent or Event).
164
+ *events: The rest of the events to append (same type as first argument).
165
+ stream_id: The stream identifier to append events to.
166
+ expected_version: The expected version of the stream
167
+
168
+ Returns:
169
+ None
170
+ """
171
+ await self._append(
172
+ stream_id=stream_id,
173
+ events=(first, *events),
174
+ expected_version=expected_version,
175
+ )
176
+
177
+ @append.register
178
+ async def _append_events(
179
+ self,
180
+ *events: Event,
181
+ stream_id: StreamId,
182
+ expected_version: int | Versioning = 0,
183
+ ) -> None:
184
+ wrapped_events = self._wrap_events(expected_version, events)
185
+ await self.append(
186
+ *wrapped_events,
187
+ stream_id=stream_id,
188
+ expected_version=expected_version,
189
+ )
190
+
191
+ @singledispatchmethod
192
+ def _wrap_events(
193
+ self,
194
+ expected_version: int,
195
+ events: Sequence[Event],
196
+ ) -> Sequence[WrappedEvent]:
197
+ return [
198
+ WrappedEvent.wrap(event=event, version=version)
199
+ for version, event in enumerate(events, start=expected_version + 1)
200
+ ]
201
+
202
+ @_wrap_events.register
203
+ def _wrap_events_versioning(
204
+ self, expected_version: Versioning, events: Sequence[Event]
205
+ ) -> Sequence[WrappedEvent]:
206
+ return [WrappedEvent.wrap(event=event, version=None) for event in events]
207
+
208
+ async def _append(
209
+ self,
210
+ stream_id: StreamId,
211
+ events: Sequence[WrappedEvent],
212
+ expected_version: int | Versioning,
213
+ ) -> None:
214
+ new_version = events[-1].version
215
+ versioning: Versioning
216
+ if expected_version is not NO_VERSIONING:
217
+ versioning = ExplicitVersioning(
218
+ expected_version=cast(int, expected_version),
219
+ initial_version=cast(int, new_version),
220
+ )
221
+ else:
222
+ versioning = NO_VERSIONING
223
+
224
+ await self._storage_strategy.insert_events(
225
+ stream_id=stream_id,
226
+ versioning=versioning,
227
+ events=[
228
+ await self._serde.serialize(
229
+ enrich_event(event, stream_id, self._context_enrichers),
230
+ stream_id,
231
+ )
232
+ for event in events
233
+ ],
234
+ )
235
+
236
+ async def delete_stream(self, stream_id: StreamId) -> None:
237
+ """Deletes a stream with a given ID.
238
+
239
+ If a stream does not exist, this method does nothing.
240
+
241
+ Args:
242
+ stream_id: The stream identifier to delete.
243
+
244
+ Returns:
245
+ None
246
+ """
247
+ await self._storage_strategy.delete_stream(stream_id)
248
+
249
+ async def save_snapshot(self, stream_id: StreamId, snapshot: WrappedEvent) -> None:
250
+ """Saves a snapshot of the stream.
251
+
252
+ Args:
253
+ stream_id: The stream identifier to save the snapshot.
254
+ snapshot: The snapshot to save.
255
+
256
+ Returns:
257
+ None
258
+ """
259
+ serialized = await self._serde.serialize(event=snapshot, stream_id=stream_id)
260
+ await self._storage_strategy.save_snapshot(serialized)
261
+
262
+ async def position(self) -> Position | None:
263
+ """Returns the current position of the event store.
264
+
265
+ Unlike its synchronous counterpart, this is a coroutine (not a property),
266
+ as fetching the position performs I/O in real backends.
267
+ """
268
+ return await self._storage_strategy.current_position()