es-entity 0.10.19

Event Sourcing Entity Framework
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
/// Prevent duplicate event processing by checking for idempotent operations.
///
/// Guards against replaying the same mutation in event-sourced systems.
/// Returns [`AlreadyApplied`][crate::Idempotent::AlreadyApplied] early if matching events are found, allowing the caller
/// to skip redundant operations. Use break pattern to allow re-applying past operations.
///
/// # Parameters
///
/// - `$events`: Event collection to search (usually chronologically reversed)
/// - `$pattern`: Event patterns that indicate operation already applied
/// - `$break_pattern`: Optional break pattern to stop searching
///
/// # Examples
///
/// ```rust
/// use es_entity::{idempotency_guard, Idempotent};
/// pub enum UserEvent{
///     Initialized {id: u64, name: String},
///     NameUpdated {name: String}
/// }
///
/// pub struct User{
///     events: Vec<UserEvent>
/// }
///
/// impl User{
///     pub fn update_name(&mut self, new_name: impl Into<String>) -> Idempotent<()>{
///         let name = new_name.into();
///         idempotency_guard!(
///             self.events.iter().rev(),
///             UserEvent::NameUpdated { name: existing_name } if existing_name == &name
///             // above line returns early if same name found
///         );
///         self.events.push(UserEvent::NameUpdated{name});
///         Idempotent::Executed(())
///     }
///     
///     pub fn update_name_with_break(&mut self, new_name: impl Into<String>) -> Idempotent<()>{
///         let name = new_name.into();
///         idempotency_guard!(
///             self.events.iter().rev(),
///             UserEvent::NameUpdated { name: existing_name } if existing_name == &name,
///             => UserEvent::NameUpdated {..}
///             // above line breaks iteration if same event found
///        );
///        self.events.push(UserEvent::NameUpdated{name});
///        Idempotent::Executed(())     
///     }
/// }
///   
/// let mut user1 = User{ events: vec![] };
/// let mut user2 = User{ events: vec![] };
/// assert!(user1.update_name("Alice").did_execute());
/// // updating "ALice" again ignored because same event with same name exists
/// assert!(user1.update_name("Alice").was_already_applied());
///     
/// assert!(user2.update_name_with_break("Alice").did_execute());
/// assert!(user2.update_name_with_break("Bob").did_execute());
/// // updating "ALice" again works because of early break condition
/// assert!(user2.update_name_with_break("Alice").did_execute());
/// ```
#[macro_export]
macro_rules! idempotency_guard {
    ($events:expr, $( $pattern:pat $(if $guard:expr)? ),+ $(,)?) => {
        for event in $events {
            match event {
                $(
                    $pattern $(if $guard)? => return $crate::FromAlreadyApplied::from_already_applied(),
                )+
                _ => {}
            }
        }
    };
    ($events:expr, $( $pattern:pat $(if $guard:expr)? ),+,
     => $break_pattern:pat $(if $break_guard:expr)?) => {
        for event in $events {
            match event {
                $($pattern $(if $guard)? => return $crate::FromAlreadyApplied::from_already_applied(),)+
                $break_pattern $(if $break_guard)? => break,
                _ => {}
            }
        }
    };
}

/// Execute an event-sourced query with automatic entity hydration.
///
/// Executes user-defined queries and returns entities by internally
/// joining with events table to hydrate entities, essentially giving the
/// illusion of working with just the index table.
///
/// **Important**: This macro only works inside functions (`fn`) that are defined
/// within structs that have `#[derive(EsRepo)]` applied. The macro relies on
/// the repository context to properly hydrate entities.
///
/// # Returns
///
/// Returns an [`EsQuery`](crate::query::EsQuery) struct that provides methods
/// like [`fetch_one()`](crate::query::EsQuery::fetch_one),
/// [`fetch_optional()`](crate::query::EsQuery::fetch_optional), and
/// [`fetch_n()`](crate::query::EsQuery::fetch_n) for executing the
/// query and retrieving hydrated entities.
///
/// # Parameters
///
/// - `tbl_prefix`: Table prefix to ignore when deriving entity names from table names (optional)
/// - `entity`: Override the entity type (optional, useful when table name doesn't match entity name)
/// - SQL query string
/// - Additional arguments for the SQL query (optional)
///
/// # Examples
/// ```ignore
/// // Basic usage
/// es_query!("SELECT id FROM users WHERE id = $1", id)
///
/// // With table prefix
/// es_query!(
///     tbl_prefix = "app",
///     "SELECT id FROM app_users WHERE active = true"
/// )
///
/// // With custom entity type
/// es_query!(
///     entity = User,
///     "SELECT id FROM custom_users_table WHERE id = $1",
///     id as UserId
/// )
/// ```
#[macro_export]
macro_rules! es_query {
    // With entity override
    (
        entity = $entity:ident,
        $query:expr,
        $($args:tt)*
    ) => ({
        $crate::expand_es_query!(
            entity = $entity,
            sql = $query,
            args = [$($args)*]
        )
    });
    // With entity override - no args
    (
        entity = $entity:ident,
        $query:expr
    ) => ({
        $crate::expand_es_query!(
            entity = $entity,
            sql = $query
        )
    });

    // With tbl_prefix
    (
        tbl_prefix = $tbl_prefix:literal,
        $query:expr,
        $($args:tt)*
    ) => ({
        $crate::expand_es_query!(
            tbl_prefix = $tbl_prefix,
            sql = $query,
            args = [$($args)*]
        )
    });
    // With tbl_prefix - no args
    (
        tbl_prefix = $tbl_prefix:literal,
        $query:expr
    ) => ({
        $crate::expand_es_query!(
            tbl_prefix = $tbl_prefix,
            sql = $query
        )
    });

    // Basic form
    (
        $query:expr,
        $($args:tt)*
    ) => ({
        $crate::expand_es_query!(
            sql = $query,
            args = [$($args)*]
        )
    });
    // Basic form - no args
    (
        $query:expr
    ) => ({
        $crate::expand_es_query!(
            sql = $query
        )
    });
}

#[macro_export]
macro_rules! from_es_entity_error {
    ($name:ident) => {
        impl $name {
            pub fn was_not_found(&self) -> bool {
                matches!(self, $name::EsEntityError($crate::EsEntityError::NotFound))
            }
            pub fn was_concurrent_modification(&self) -> bool {
                matches!(
                    self,
                    $name::EsEntityError($crate::EsEntityError::ConcurrentModification)
                )
            }
        }
        impl From<$crate::EsEntityError> for $name {
            fn from(e: $crate::EsEntityError) -> Self {
                $name::EsEntityError(e)
            }
        }
    };
}

// Helper macro for common entity_id implementations (internal use only)
#[doc(hidden)]
#[macro_export]
macro_rules! __entity_id_common_impls {
    ($name:ident) => {
        impl $name {
            #[allow(clippy::new_without_default)]
            pub fn new() -> Self {
                $crate::prelude::uuid::Uuid::now_v7().into()
            }
        }

        impl From<$crate::prelude::uuid::Uuid> for $name {
            fn from(uuid: $crate::prelude::uuid::Uuid) -> Self {
                Self(uuid)
            }
        }

        impl From<$name> for $crate::prelude::uuid::Uuid {
            fn from(id: $name) -> Self {
                id.0
            }
        }

        impl From<&$name> for $crate::prelude::uuid::Uuid {
            fn from(id: &$name) -> Self {
                id.0
            }
        }

        impl std::fmt::Display for $name {
            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                write!(f, "{}", self.0)
            }
        }

        impl std::str::FromStr for $name {
            type Err = $crate::prelude::uuid::Error;

            fn from_str(s: &str) -> Result<Self, Self::Err> {
                Ok(Self($crate::prelude::uuid::Uuid::parse_str(s)?))
            }
        }
    };
}

// Helper macro for GraphQL-specific entity_id implementations (internal use only)
#[doc(hidden)]
#[macro_export]
macro_rules! __entity_id_graphql_impls {
    ($name:ident) => {
        impl From<$crate::graphql::UUID> for $name {
            fn from(id: $crate::graphql::UUID) -> Self {
                $name($crate::prelude::uuid::Uuid::from(&id))
            }
        }

        impl From<&$crate::graphql::UUID> for $name {
            fn from(id: &$crate::graphql::UUID) -> Self {
                $name($crate::prelude::uuid::Uuid::from(id))
            }
        }
    };
}

// Helper macro for additional conversions (internal use only)
#[doc(hidden)]
#[macro_export]
macro_rules! __entity_id_conversions {
    ($($from:ty => $to:ty),* $(,)?) => {
        $(
            impl From<$from> for $to {
                fn from(id: $from) -> Self {
                    <$to>::from($crate::prelude::uuid::Uuid::from(id))
                }
            }
            impl From<$to> for $from {
                fn from(id: $to) -> Self {
                    <$from>::from($crate::prelude::uuid::Uuid::from(id))
                }
            }
        )*
    };
}

#[doc(hidden)]
#[cfg(all(feature = "graphql", feature = "json-schema"))]
#[macro_export]
macro_rules! entity_id {
    // Match identifiers without conversions
    ($($name:ident),+ $(,)?) => {
        $crate::entity_id! { $($name),+ ; }
    };
    ($($name:ident),+ $(,)? ; $($from:ty => $to:ty),* $(,)?) => {
        $(
            #[derive(
                $crate::prelude::sqlx::Type,
                Debug,
                Clone,
                Copy,
                PartialEq,
                Eq,
                PartialOrd,
                Ord,
                Hash,
                $crate::prelude::serde::Deserialize,
                $crate::prelude::serde::Serialize,
                $crate::prelude::schemars::JsonSchema,
            )]
            #[schemars(crate = "es_entity::prelude::schemars")]
            #[serde(crate = "es_entity::prelude::serde")]
            #[serde(transparent)]
            #[sqlx(transparent)]
            pub struct $name($crate::prelude::uuid::Uuid);
            $crate::__entity_id_common_impls!($name);
            $crate::__entity_id_graphql_impls!($name);
        )+
        $crate::__entity_id_conversions!($($from => $to),*);
    };
}

#[doc(hidden)]
#[cfg(all(feature = "graphql", not(feature = "json-schema")))]
#[macro_export]
macro_rules! entity_id {
    // Match identifiers without conversions
    ($($name:ident),+ $(,)?) => {
        $crate::entity_id! { $($name),+ ; }
    };
    ($($name:ident),+ $(,)? ; $($from:ty => $to:ty),* $(,)?) => {
        $(
            #[derive(
                $crate::prelude::sqlx::Type,
                Debug,
                Clone,
                Copy,
                PartialEq,
                Eq,
                PartialOrd,
                Ord,
                Hash,
                $crate::prelude::serde::Deserialize,
                $crate::prelude::serde::Serialize,
            )]
            #[serde(crate = "es_entity::prelude::serde")]
            #[serde(transparent)]
            #[sqlx(transparent)]
            pub struct $name($crate::prelude::uuid::Uuid);
            $crate::__entity_id_common_impls!($name);
            $crate::__entity_id_graphql_impls!($name);
        )+
        $crate::__entity_id_conversions!($($from => $to),*);
    };
}

#[doc(hidden)]
#[cfg(all(feature = "json-schema", not(feature = "graphql")))]
#[macro_export]
macro_rules! entity_id {
    // Match identifiers without conversions
    ($($name:ident),+ $(,)?) => {
        $crate::entity_id! { $($name),+ ; }
    };
    ($($name:ident),+ $(,)? ; $($from:ty => $to:ty),* $(,)?) => {
        $(
            #[derive(
                $crate::prelude::sqlx::Type,
                Debug,
                Clone,
                Copy,
                PartialEq,
                Eq,
                PartialOrd,
                Ord,
                Hash,
                $crate::prelude::serde::Deserialize,
                $crate::prelude::serde::Serialize,
                $crate::prelude::schemars::JsonSchema,
            )]
            #[schemars(crate = "es_entity::prelude::schemars")]
            #[serde(crate = "es_entity::prelude::serde")]
            #[serde(transparent)]
            #[sqlx(transparent)]
            pub struct $name($crate::prelude::uuid::Uuid);
            $crate::__entity_id_common_impls!($name);
        )+
        $crate::__entity_id_conversions!($($from => $to),*);
    };
}

/// Create UUID-wrappers for database operations.
///
/// This macro generates type-safe UUID-wrapper structs with trait support for
/// serialization, database operations, GraphQL integration, and JSON schema generation.
///
/// # Features
///
/// The macro automatically includes different trait implementations based on enabled features:
/// - `graphql`: Adds GraphQL UUID conversion traits
/// - `json-schema`: Adds JSON schema generation support
///
/// # Generated Traits
///
/// All entity IDs automatically implement:
/// - `Debug`, `Clone`, `Copy`, `PartialEq`, `Eq`, `PartialOrd`, `Ord`, `Hash`
/// - `serde::Serialize`, `serde::Deserialize` (with transparent serialization)
/// - `sqlx::Type` (with transparent database type)
/// - `Display` and `FromStr` for string conversion
/// - `From<Uuid>` and `From<EntityId>` for UUID conversion
///
/// # Parameters
///
/// - `$name`: One or more entity ID type names to create
/// - `$from => $to`: Optional conversion pairs between different entity ID types
///
/// # Examples
///
/// ```rust
/// use es_entity::entity_id;
///
/// entity_id! { UserId, OrderId }
///
/// // Creates:
/// // pub struct UserId(Uuid);
/// // pub struct OrderId(Uuid);
/// ```
///
/// ```rust
/// use es_entity::entity_id;
///
/// entity_id! {
///     UserId,
///     AdminUserId;
///     UserId => AdminUserId
/// }
///
/// // Creates UserId and AdminUserId with `impl From` conversion between them
/// ```
#[cfg(all(not(feature = "json-schema"), not(feature = "graphql")))]
#[macro_export]
macro_rules! entity_id {
    // Match identifiers without conversions
    ($($name:ident),+ $(,)?) => {
        $crate::entity_id! { $($name),+ ; }
    };
    ($($name:ident),+ $(,)? ; $($from:ty => $to:ty),* $(,)?) => {
        $(
            #[derive(
                $crate::prelude::sqlx::Type,
                Debug,
                Clone,
                Copy,
                PartialEq,
                Eq,
                PartialOrd,
                Ord,
                Hash,
                $crate::prelude::serde::Deserialize,
                $crate::prelude::serde::Serialize,
            )]
            #[serde(crate = "es_entity::prelude::serde")]
            #[serde(transparent)]
            #[sqlx(transparent)]
            pub struct $name($crate::prelude::uuid::Uuid);
            $crate::__entity_id_common_impls!($name);
        )+
        $crate::__entity_id_conversions!($($from => $to),*);
    };
}