cratestack_sqlx/delegate/model_batch.rs
1//! `ModelDelegate` batch entry points (`batch_get`, `batch_create`,
2//! `batch_update`, `batch_delete`, `batch_upsert`).
3
4use crate::{BatchCreate, BatchDelete, BatchGet, BatchUpdate, BatchUpdateItem, BatchUpsert};
5
6use super::model::ModelDelegate;
7
8impl<'a, M: 'static, PK: 'static> ModelDelegate<'a, M, PK> {
9 /// Fetch many rows by primary key in a single round-trip; missing
10 /// rows surface as per-item `NotFound` in the envelope rather
11 /// than aborting.
12 pub fn batch_get(&self, ids: Vec<PK>) -> BatchGet<'a, M, PK> {
13 BatchGet {
14 runtime: self.runtime,
15 descriptor: self.descriptor,
16 ids,
17 }
18 }
19
20 /// Insert many rows in one outer transaction; each input runs
21 /// under a nested SAVEPOINT, so a per-item failure (validation,
22 /// policy, unique conflict) doesn't take down the rest of the
23 /// batch.
24 pub fn batch_create<I>(&self, inputs: Vec<I>) -> BatchCreate<'a, M, PK, I> {
25 BatchCreate {
26 runtime: self.runtime,
27 descriptor: self.descriptor,
28 inputs,
29 }
30 }
31
32 /// Update many rows in one outer transaction with per-item
33 /// patches and optional `if_match` versions. Per-item failures
34 /// roll back at the savepoint; successful items commit together.
35 pub fn batch_update<I>(
36 &self,
37 items: Vec<BatchUpdateItem<PK, I>>,
38 ) -> BatchUpdate<'a, M, PK, I> {
39 BatchUpdate {
40 runtime: self.runtime,
41 descriptor: self.descriptor,
42 items,
43 }
44 }
45
46 /// Delete many rows by primary key in a single statement; rows
47 /// that don't exist (or that policy hid) surface as per-item
48 /// `NotFound`.
49 pub fn batch_delete(&self, ids: Vec<PK>) -> BatchDelete<'a, M, PK> {
50 BatchDelete {
51 runtime: self.runtime,
52 descriptor: self.descriptor,
53 ids,
54 }
55 }
56
57 /// Insert-or-update many rows in one outer transaction with
58 /// per-item savepoints. Eligible only for models whose `@id` is
59 /// client-supplied — same compile-time gate as the single-row
60 /// `.upsert(...)`.
61 pub fn batch_upsert<I>(&self, inputs: Vec<I>) -> BatchUpsert<'a, M, PK, I> {
62 BatchUpsert {
63 runtime: self.runtime,
64 descriptor: self.descriptor,
65 inputs,
66 }
67 }
68}