backbone-core 3.0.3

Backbone Framework Core - Foundation for generic CRUD system
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
//! Generic bulk operations — create, update, delete multiple entities in one call.
//!
//! Generated code emits a type alias:
//!
//! ```rust,ignore
//! // Generated:
//! pub type StoredFileBulkService = GenericBulkService<
//!     StoredFile,
//!     CreateStoredFileDto,
//!     StoredFileService,
//! >;
//! ```
//!
//! Bulk operations run as individual transactions (one per item) by default.
//! Override `BulkStrategy` to wrap all items in a single transaction.

use async_trait::async_trait;
use std::marker::PhantomData;
use std::sync::Arc;

use crate::service::{ServiceError, ServiceResult};

// ─── Configuration ────────────────────────────────────────────────────────────

/// Controls how bulk operations behave on partial failure.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BulkFailureMode {
    /// Continue processing all items and report per-item errors at the end.
    ContinueOnError,
    /// Abort on the first failure (no partial results).
    AbortOnFirstError,
}

impl Default for BulkFailureMode {
    fn default() -> Self {
        Self::ContinueOnError
    }
}

/// Configuration for a bulk operation.
#[derive(Debug, Clone)]
pub struct BulkOperationConfig {
    /// Maximum number of items per bulk request.
    pub max_batch_size: usize,
    /// How to handle item failures.
    pub failure_mode: BulkFailureMode,
}

impl Default for BulkOperationConfig {
    fn default() -> Self {
        Self {
            max_batch_size: 100,
            failure_mode: BulkFailureMode::ContinueOnError,
        }
    }
}

// ─── Progress tracking ────────────────────────────────────────────────────────

/// Per-item outcome in a bulk operation.
#[derive(Debug, Clone)]
pub struct BulkItemResult<E> {
    pub index: usize,
    pub result: Result<E, String>,
}

impl<E> BulkItemResult<E> {
    pub fn ok(index: usize, entity: E) -> Self {
        Self {
            index,
            result: Ok(entity),
        }
    }

    pub fn err(index: usize, error: impl Into<String>) -> Self {
        Self {
            index,
            result: Err(error.into()),
        }
    }
}

/// Aggregated result of a completed bulk operation.
#[derive(Debug, Clone)]
pub struct BulkOperationResult<E> {
    pub succeeded: Vec<E>,
    pub failed: Vec<(usize, String)>,
    pub total: usize,
}

impl<E: Clone> BulkOperationResult<E> {
    pub fn new() -> Self {
        Self {
            succeeded: Vec::new(),
            failed: Vec::new(),
            total: 0,
        }
    }

    pub fn success_count(&self) -> usize {
        self.succeeded.len()
    }

    pub fn failure_count(&self) -> usize {
        self.failed.len()
    }

    pub fn is_fully_successful(&self) -> bool {
        self.failed.is_empty()
    }

    /// Flatten into an ordered list of per-item results, indexed by insertion order.
    ///
    /// Succeeded items are assigned consecutive indices starting at 0;
    /// failed items use the original input index from `(index, reason)`.
    pub fn into_item_results(self) -> Vec<BulkItemResult<E>> {
        let mut items: Vec<BulkItemResult<E>> = self
            .succeeded
            .into_iter()
            .enumerate()
            .map(|(i, e)| BulkItemResult::ok(i, e))
            .collect();

        for (index, reason) in self.failed {
            items.push(BulkItemResult::err(index, reason));
        }

        items.sort_by_key(|item| item.index);
        items
    }
}

impl<E: Clone> Default for BulkOperationResult<E> {
    fn default() -> Self {
        Self::new()
    }
}

// ─── Progress tracking ────────────────────────────────────────────────────────

/// Tracks live progress of an in-flight bulk operation.
///
/// Useful for streaming progress updates or building progress bars.
#[derive(Debug, Clone, Default)]
pub struct BulkOperationProgress {
    /// Total number of items submitted.
    pub total: usize,
    /// Items processed so far (succeeded + failed).
    pub processed: usize,
    /// Items that completed successfully.
    pub succeeded: usize,
    /// Items that failed.
    pub failed: usize,
}

impl BulkOperationProgress {
    pub fn new(total: usize) -> Self {
        Self {
            total,
            processed: 0,
            succeeded: 0,
            failed: 0,
        }
    }

    pub fn record_success(&mut self) {
        self.processed += 1;
        self.succeeded += 1;
    }

    pub fn record_failure(&mut self) {
        self.processed += 1;
        self.failed += 1;
    }

    pub fn is_complete(&self) -> bool {
        self.processed >= self.total
    }

    pub fn success_rate(&self) -> f64 {
        if self.total == 0 {
            return 1.0;
        }
        self.succeeded as f64 / self.total as f64
    }
}

// ─── Service contract ─────────────────────────────────────────────────────────

/// The minimal contract needed from a service to power bulk operations.
#[async_trait]
pub trait BulkCapableService<E: Send + Sync + 'static, DTO: Send + Sync + 'static>:
    Send + Sync
{
    async fn create_one(&self, dto: DTO) -> ServiceResult<E>;
    async fn delete_one(&self, id: &str) -> ServiceResult<bool>;
}

// ─── GenericBulkService ───────────────────────────────────────────────────────

/// Generic bulk service over any entity+DTO+service triple.
///
/// `E`   — entity type
/// `DTO` — create DTO type
/// `S`   — underlying single-entity service (implements `BulkCapableService<E, DTO>`)
pub struct GenericBulkService<E, DTO, S> {
    service: Arc<S>,
    config: BulkOperationConfig,
    _phantom: PhantomData<(E, DTO)>,
}

impl<E, DTO, S> GenericBulkService<E, DTO, S>
where
    E: Send + Sync + Clone + 'static,
    DTO: Send + Sync + 'static,
    S: BulkCapableService<E, DTO>,
{
    pub fn new(service: Arc<S>) -> Self {
        Self {
            service,
            config: BulkOperationConfig::default(),
            _phantom: PhantomData,
        }
    }

    pub fn with_config(service: Arc<S>, config: BulkOperationConfig) -> Self {
        Self {
            service,
            config,
            _phantom: PhantomData,
        }
    }

    /// Create multiple entities.  Respects `failure_mode` from config.
    ///
    /// Returns `(BulkOperationResult, BulkOperationProgress)` so callers can
    /// inspect final progress stats alongside the per-item outcomes.
    pub async fn bulk_create(
        &self,
        items: Vec<DTO>,
    ) -> ServiceResult<(BulkOperationResult<E>, BulkOperationProgress)> {
        if items.len() > self.config.max_batch_size {
            return Err(ServiceError::Validation(format!(
                "bulk create exceeds maximum batch size of {}",
                self.config.max_batch_size
            )));
        }

        let total = items.len();
        let mut result = BulkOperationResult::new();
        result.total = total;
        let mut progress = BulkOperationProgress::new(total);

        for (index, dto) in items.into_iter().enumerate() {
            match self.service.create_one(dto).await {
                Ok(entity) => {
                    progress.record_success();
                    result.succeeded.push(entity);
                }
                Err(e) => {
                    progress.record_failure();
                    result.failed.push((index, e.to_string()));
                    if self.config.failure_mode == BulkFailureMode::AbortOnFirstError {
                        return Ok((result, progress));
                    }
                }
            }
        }

        Ok((result, progress))
    }

    /// Delete multiple entities by ID.
    pub async fn bulk_delete(
        &self,
        ids: Vec<String>,
    ) -> ServiceResult<(BulkOperationResult<()>, BulkOperationProgress)> {
        if ids.len() > self.config.max_batch_size {
            return Err(ServiceError::Validation(format!(
                "bulk delete exceeds maximum batch size of {}",
                self.config.max_batch_size
            )));
        }

        let total = ids.len();
        let mut result: BulkOperationResult<()> = BulkOperationResult::new();
        result.total = total;
        let mut progress = BulkOperationProgress::new(total);

        for (index, id) in ids.iter().enumerate() {
            match self.service.delete_one(id).await {
                Ok(_) => {
                    progress.record_success();
                    result.succeeded.push(());
                }
                Err(e) => {
                    progress.record_failure();
                    result.failed.push((index, e.to_string()));
                    if self.config.failure_mode == BulkFailureMode::AbortOnFirstError {
                        return Ok((result, progress));
                    }
                }
            }
        }

        Ok((result, progress))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[derive(Debug, Clone)]
    struct Item {
        id: String,
    }

    struct CreateItemDto {
        id: String,
    }

    struct FakeService {
        fail_ids: Vec<String>,
    }

    #[async_trait]
    impl BulkCapableService<Item, CreateItemDto> for FakeService {
        async fn create_one(&self, dto: CreateItemDto) -> ServiceResult<Item> {
            if self.fail_ids.contains(&dto.id) {
                Err(ServiceError::Internal("injected failure".into()))
            } else {
                Ok(Item { id: dto.id })
            }
        }

        async fn delete_one(&self, id: &str) -> ServiceResult<bool> {
            if self.fail_ids.contains(&id.to_string()) {
                Err(ServiceError::NotFound)
            } else {
                Ok(true)
            }
        }
    }

    #[tokio::test]
    async fn bulk_create_collects_errors_in_continue_mode() {
        let service = Arc::new(FakeService {
            fail_ids: vec!["bad".into()],
        });
        let bulk = GenericBulkService::new(service);

        let dtos = vec![
            CreateItemDto { id: "ok1".into() },
            CreateItemDto { id: "bad".into() },
            CreateItemDto { id: "ok2".into() },
        ];

        let (result, progress) = bulk.bulk_create(dtos).await.unwrap();
        assert_eq!(result.success_count(), 2);
        assert_eq!(result.failure_count(), 1);
        assert_eq!(progress.succeeded, 2);
        assert_eq!(progress.failed, 1);
    }

    #[tokio::test]
    async fn bulk_create_aborts_on_first_error() {
        let service = Arc::new(FakeService {
            fail_ids: vec!["bad".into()],
        });
        let config = BulkOperationConfig {
            max_batch_size: 100,
            failure_mode: BulkFailureMode::AbortOnFirstError,
        };
        let bulk = GenericBulkService::with_config(service, config);

        let dtos = vec![
            CreateItemDto { id: "bad".into() },
            CreateItemDto { id: "ok".into() },
        ];

        let (result, progress) = bulk.bulk_create(dtos).await.unwrap();
        // Aborted after first failure — second item never processed
        assert_eq!(result.success_count(), 0);
        assert_eq!(result.failure_count(), 1);
        assert!(progress.is_complete() == false); // aborted early
    }

    #[tokio::test]
    async fn bulk_create_rejects_oversized_batch() {
        let service = Arc::new(FakeService { fail_ids: vec![] });
        let config = BulkOperationConfig {
            max_batch_size: 2,
            failure_mode: BulkFailureMode::ContinueOnError,
        };
        let bulk = GenericBulkService::with_config(service, config);

        let dtos: Vec<_> = (0..3)
            .map(|i| CreateItemDto {
                id: i.to_string(),
            })
            .collect();

        assert!(bulk.bulk_create(dtos).await.is_err()); // batch size exceeded
    }
}