es-entity 0.12.21

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
#![cfg(feature = "instrument")]
//! Proves nested batching composes to arbitrary depth: a three-level
//! parent -> child -> grandchild hierarchy, where the grandchild phase
//! (gathered *across every child of every parent* in one
//! `update_all_in_op` call on the top-level parent repo) collapses to a
//! single statement per grandchild repo fn, exactly like the one-level case
//! in `nested_batch_statement_count.rs`.
//!
//! The mechanism has no depth-specific code: `create_nested_*_in_op` /
//! `update_nested_*_in_op` always take `&mut [&mut P]` and the middle
//! (`GcChild`) repo's own `update_all_mut_in_op` runs its *own* nested phase
//! the same way any other bulk fn does — so this is exercising the exact
//! same generated code path as the one-level test, just nested one level
//! deeper.

mod helpers;

use std::{
    collections::HashMap,
    sync::{Arc, Mutex},
};

use derive_builder::Builder;
use es_entity::*;
use helpers::init_pool;
use serde::{Deserialize, Serialize};
use sqlx::PgPool;
use tracing_subscriber::layer::SubscriberExt;

es_entity::entity_id! { GcParentId, GcChildId, GcGrandchildId }

// ─── Grandchild ─────────────────────────────────────────────────────────
#[derive(EsEvent, Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
#[es_event(id = "GcGrandchildId")]
pub enum GcGrandchildEvent {
    Initialized {
        id: GcGrandchildId,
        child_id: GcChildId,
        note: String,
    },
    NoteUpdated {
        note: String,
    },
}

#[derive(EsEntity, Builder)]
#[builder(pattern = "owned", build_fn(error = "EntityHydrationError"))]
pub struct GcGrandchild {
    pub id: GcGrandchildId,
    pub child_id: GcChildId,
    pub note: String,
    events: EntityEvents<GcGrandchildEvent>,
}

impl GcGrandchild {
    pub fn update_note(&mut self, note: impl Into<String>) {
        let note = note.into();
        self.note = note.clone();
        self.events.push(GcGrandchildEvent::NoteUpdated { note });
    }
}

impl TryFromEvents<GcGrandchildEvent> for GcGrandchild {
    fn try_from_events(
        events: EntityEvents<GcGrandchildEvent>,
    ) -> Result<Self, EntityHydrationError> {
        let mut builder = GcGrandchildBuilder::default();
        for event in events.iter_all() {
            match event {
                GcGrandchildEvent::Initialized { id, child_id, note } => {
                    builder = builder.id(*id).child_id(*child_id).note(note.clone());
                }
                GcGrandchildEvent::NoteUpdated { note } => {
                    builder = builder.note(note.clone());
                }
            }
        }
        builder.events(events).build()
    }
}

#[derive(Debug, Clone, Builder)]
pub struct NewGcGrandchild {
    pub id: GcGrandchildId,
    pub child_id: GcChildId,
    #[builder(setter(into))]
    pub note: String,
}

impl NewGcGrandchild {
    pub fn builder() -> NewGcGrandchildBuilder {
        NewGcGrandchildBuilder::default()
    }
}

impl IntoEvents<GcGrandchildEvent> for NewGcGrandchild {
    fn into_events(self) -> EntityEvents<GcGrandchildEvent> {
        EntityEvents::init(
            self.id,
            vec![GcGrandchildEvent::Initialized {
                id: self.id,
                child_id: self.child_id,
                note: self.note,
            }],
        )
    }
}

// ─── Child ──────────────────────────────────────────────────────────────
#[derive(EsEvent, Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
#[es_event(id = "GcChildId")]
pub enum GcChildEvent {
    Initialized {
        id: GcChildId,
        parent_id: GcParentId,
    },
}

#[derive(EsEntity, Builder)]
#[builder(pattern = "owned", build_fn(error = "EntityHydrationError"))]
pub struct GcChild {
    pub id: GcChildId,
    pub parent_id: GcParentId,
    events: EntityEvents<GcChildEvent>,

    #[es_entity(nested)]
    #[builder(default)]
    grandchildren: Nested<GcGrandchild>,
}

impl GcChild {
    pub fn add_grandchild(&mut self, new: NewGcGrandchild) {
        self.grandchildren.add_new(new);
    }

    pub fn update_grandchild_note(&mut self, note: impl Into<String>) {
        let note = note.into();
        let child = self
            .grandchildren
            .iter_persisted_mut()
            .next()
            .expect("child has a persisted grandchild");
        child.update_note(note);
    }

    pub fn n_grandchildren(&self) -> usize {
        self.grandchildren.len_persisted()
    }
}

impl TryFromEvents<GcChildEvent> for GcChild {
    fn try_from_events(events: EntityEvents<GcChildEvent>) -> Result<Self, EntityHydrationError> {
        let mut builder = GcChildBuilder::default();
        for event in events.iter_all() {
            match event {
                GcChildEvent::Initialized { id, parent_id } => {
                    builder = builder.id(*id).parent_id(*parent_id);
                }
            }
        }
        builder.events(events).build()
    }
}

#[derive(Debug, Clone, Builder)]
pub struct NewGcChild {
    pub id: GcChildId,
    pub parent_id: GcParentId,
}

impl IntoEvents<GcChildEvent> for NewGcChild {
    fn into_events(self) -> EntityEvents<GcChildEvent> {
        EntityEvents::init(
            self.id,
            vec![GcChildEvent::Initialized {
                id: self.id,
                parent_id: self.parent_id,
            }],
        )
    }
}

// ─── Parent ─────────────────────────────────────────────────────────────
#[derive(EsEvent, Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
#[es_event(id = "GcParentId")]
pub enum GcParentEvent {
    Initialized { id: GcParentId },
}

#[derive(EsEntity, Builder)]
#[builder(pattern = "owned", build_fn(error = "EntityHydrationError"))]
pub struct GcParent {
    pub id: GcParentId,
    events: EntityEvents<GcParentEvent>,

    #[es_entity(nested)]
    #[builder(default)]
    children: Nested<GcChild>,
}

impl GcParent {
    pub fn add_child(&mut self, new: NewGcChild) {
        self.children.add_new(new);
    }

    pub fn persisted_children_mut(&mut self) -> impl Iterator<Item = &mut GcChild> {
        self.children.iter_persisted_mut()
    }
}

impl TryFromEvents<GcParentEvent> for GcParent {
    fn try_from_events(events: EntityEvents<GcParentEvent>) -> Result<Self, EntityHydrationError> {
        let mut builder = GcParentBuilder::default();
        for event in events.iter_all() {
            match event {
                GcParentEvent::Initialized { id } => {
                    builder = builder.id(*id);
                }
            }
        }
        builder.events(events).build()
    }
}

#[derive(Debug, Clone, Builder)]
pub struct NewGcParent {
    pub id: GcParentId,
}

impl IntoEvents<GcParentEvent> for NewGcParent {
    fn into_events(self) -> EntityEvents<GcParentEvent> {
        EntityEvents::init(self.id, vec![GcParentEvent::Initialized { id: self.id }])
    }
}

// ─── Repos ──────────────────────────────────────────────────────────────
#[derive(EsRepo, Debug)]
#[es_repo(
    entity = "GcGrandchild",
    tbl = "gc_grandchildren",
    events_tbl = "gc_grandchild_events",
    delete = "soft",
    columns(child_id(ty = "GcChildId", update(persist = false), parent))
)]
pub struct GcGrandchildren {
    pool: PgPool,
}

impl GcGrandchildren {
    pub fn new(pool: PgPool) -> Self {
        Self { pool }
    }
}

#[derive(EsRepo, Debug)]
#[es_repo(
    entity = "GcChild",
    tbl = "gc_children",
    events_tbl = "gc_child_events",
    delete = "soft",
    columns(parent_id(ty = "GcParentId", update(persist = false), parent))
)]
pub struct GcChildren {
    pool: PgPool,

    #[es_repo(nested)]
    grandchildren: GcGrandchildren,
}

impl GcChildren {
    pub fn new(pool: PgPool) -> Self {
        Self {
            pool: pool.clone(),
            grandchildren: GcGrandchildren::new(pool),
        }
    }
}

#[derive(EsRepo, Debug)]
#[es_repo(
    entity = "GcParent",
    tbl = "gc_parents",
    events_tbl = "gc_parent_events",
    delete = "soft"
)]
pub struct GcParents {
    pool: PgPool,

    #[es_repo(nested)]
    children: GcChildren,
}

impl GcParents {
    pub fn new(pool: PgPool) -> Self {
        Self {
            pool: pool.clone(),
            children: GcChildren::new(pool),
        }
    }
}

// ─── Span counting ──────────────────────────────────────────────────────
#[derive(Clone, Default)]
struct SpanCounts(Arc<Mutex<HashMap<String, usize>>>);

impl SpanCounts {
    fn count(&self, name: &str) -> usize {
        self.0.lock().unwrap().get(name).copied().unwrap_or(0)
    }
}

impl<S: tracing::Subscriber> tracing_subscriber::Layer<S> for SpanCounts {
    fn on_new_span(
        &self,
        attrs: &tracing::span::Attributes<'_>,
        _id: &tracing::span::Id,
        _ctx: tracing_subscriber::layer::Context<'_, S>,
    ) {
        *self
            .0
            .lock()
            .unwrap()
            .entry(attrs.metadata().name().to_string())
            .or_insert(0) += 1;
    }
}

/// 2 parents x 2 children x 1 persisted grandchild each: mutating every
/// grandchild's note and adding a new grandchild under every child, then
/// flushing the whole tree through a single top-level `update_all_in_op`
/// call, should collapse the grandchild phase to one `update_all_mut` call
/// and one `create_all` call on the grandchild repo — not one per child
/// (there are 4 children) and not one per parent (there are 2).
#[tokio::test]
async fn grandchild_batching_composes_across_the_whole_tree() -> anyhow::Result<()> {
    let pool = init_pool().await?;
    let parents_repo = GcParents::new(pool.clone());
    let children_repo = GcChildren::new(pool);

    const N_PARENTS: usize = 2;
    const N_CHILDREN_PER_PARENT: usize = 2;

    let mut parent_ids = Vec::new();
    for _ in 0..N_PARENTS {
        let parent_id = GcParentId::new();
        let mut parent = parents_repo
            .create(NewGcParentBuilder::default().id(parent_id).build()?)
            .await?;
        for _ in 0..N_CHILDREN_PER_PARENT {
            let child_id = GcChildId::new();
            parent.add_child(
                NewGcChildBuilder::default()
                    .id(child_id)
                    .parent_id(parent_id)
                    .build()?,
            );
        }
        parents_repo.update(&mut parent).await?;
        parent_ids.push(parent_id);
    }

    // Give every persisted child one persisted grandchild, via the child
    // repo directly (children were just created above).
    let mut all_children = children_repo
        .find_all::<GcChild>(
            &sqlx::query_scalar!(
                "SELECT id AS \"id: GcChildId\" FROM gc_children WHERE parent_id = ANY($1) ORDER BY id",
                &parent_ids as &[GcParentId],
            )
            .fetch_all(&children_repo.pool().clone())
            .await?,
        )
        .await?;
    let mut children_vec: Vec<GcChild> = all_children.drain().map(|(_, c)| c).collect();
    assert_eq!(children_vec.len(), N_PARENTS * N_CHILDREN_PER_PARENT);
    for child in children_vec.iter_mut() {
        child.add_grandchild(
            NewGcGrandchild::builder()
                .id(GcGrandchildId::new())
                .child_id(child.id)
                .note("initial")
                .build()?,
        );
    }
    children_repo.update_all(&mut children_vec).await?;

    // Reload the whole tree, mutate every grandchild's note and add a new
    // grandchild under every child, then flush it all through one
    // top-level `update_all_in_op` call.
    let mut loaded = parents_repo.find_all::<GcParent>(&parent_ids).await?;
    let mut batch: Vec<GcParent> = parent_ids
        .iter()
        .map(|id| loaded.remove(id).expect("parent was loaded"))
        .collect();

    for parent in batch.iter_mut() {
        for child in parent.persisted_children_mut() {
            assert_eq!(
                child.n_grandchildren(),
                1,
                "child should have its seeded grandchild"
            );
            child.update_grandchild_note("updated");
            child.add_grandchild(
                NewGcGrandchild::builder()
                    .id(GcGrandchildId::new())
                    .child_id(child.id)
                    .note("new")
                    .build()?,
            );
        }
    }

    let counts = SpanCounts::default();
    let subscriber = tracing_subscriber::registry().with(counts.clone());
    let _guard = tracing::subscriber::set_default(subscriber);

    parents_repo.update_all(&mut batch).await?;

    assert_eq!(
        counts.count("gc_grandchildren.update_all_mut"),
        1,
        "grandchild note updates across every child of every parent should collapse into one \
         gc_grandchildren.update_all_mut call"
    );
    assert_eq!(
        counts.count("gc_grandchildren.create_all"),
        1,
        "new grandchildren across every child of every parent should collapse into one \
         gc_grandchildren.create_all call"
    );
    // The child level itself batches too: one update_all_mut for the
    // (unchanged, but visited) persisted children, none of the old
    // per-entity fallbacks.
    assert_eq!(counts.count("gc_children.update"), 0);
    assert_eq!(counts.count("gc_grandchildren.update"), 0);

    // Functional correctness, not just statement counts.
    let reloaded = parents_repo.find_all::<GcParent>(&parent_ids).await?;
    for (_, mut parent) in reloaded {
        for child in parent.persisted_children_mut() {
            assert_eq!(
                child.n_grandchildren(),
                2,
                "one updated + one new grandchild"
            );
        }
    }

    Ok(())
}