cratestack_sqlx/delegate/
scoped.rs1use cratestack_core::CoolContext;
6
7use crate::{BatchUpdateItem, ModelDescriptor};
8
9use super::model::ModelDelegate;
10use super::scoped_aggregate::ScopedAggregate;
11use super::scoped_batch::{
12 ScopedBatchCreate, ScopedBatchDelete, ScopedBatchGet, ScopedBatchUpdate, ScopedBatchUpsert,
13};
14use super::scoped_delete::{ScopedDeleteMany, ScopedDeleteRecord};
15use super::scoped_find_many::ScopedFindMany;
16use super::scoped_find_unique::ScopedFindUnique;
17use super::scoped_update_many::ScopedUpdateMany;
18use super::scoped_upsert::ScopedUpsertRecord;
19use super::scoped_writes::{ScopedCreateRecord, ScopedUpdateRecord};
20
21#[derive(Debug, Clone)]
22pub struct ScopedModelDelegate<'a, M: 'static, PK: 'static> {
23 pub(super) delegate: ModelDelegate<'a, M, PK>,
24 pub(super) ctx: CoolContext,
25}
26
27impl<'a, M: 'static, PK: 'static> ScopedModelDelegate<'a, M, PK> {
28 pub(super) fn new(delegate: ModelDelegate<'a, M, PK>, ctx: CoolContext) -> Self {
29 Self { delegate, ctx }
30 }
31
32 pub fn descriptor(&self) -> &'static ModelDescriptor<M, PK> {
33 self.delegate.descriptor()
34 }
35
36 pub fn context(&self) -> &CoolContext {
37 &self.ctx
38 }
39
40 pub fn find_many(&self) -> ScopedFindMany<'a, M, PK> {
41 ScopedFindMany::new(self.delegate.find_many(), self.ctx.clone())
42 }
43
44 pub fn find_unique(&self, id: PK) -> ScopedFindUnique<'a, M, PK> {
45 ScopedFindUnique::new(self.delegate.find_unique(id), self.ctx.clone())
46 }
47
48 pub fn create<I>(&self, input: I) -> ScopedCreateRecord<'a, M, PK, I> {
49 ScopedCreateRecord::new(self.delegate.create(input), self.ctx.clone())
50 }
51
52 pub fn upsert<I>(&self, input: I) -> ScopedUpsertRecord<'a, M, PK, I> {
53 ScopedUpsertRecord::new(self.delegate.upsert(input), self.ctx.clone())
54 }
55
56 pub fn update(&self, id: PK) -> ScopedUpdateRecord<'a, M, PK> {
57 ScopedUpdateRecord::new(self.delegate.update(id), self.ctx.clone())
58 }
59
60 pub fn update_many(&self) -> ScopedUpdateMany<'a, M, PK> {
61 ScopedUpdateMany::new(self.delegate.update_many(), self.ctx.clone())
62 }
63
64 pub fn delete(&self, id: PK) -> ScopedDeleteRecord<'a, M, PK> {
65 ScopedDeleteRecord::new(self.delegate.delete(id), self.ctx.clone())
66 }
67
68 pub fn delete_many(&self) -> ScopedDeleteMany<'a, M, PK> {
69 ScopedDeleteMany::new(self.delegate.delete_many(), self.ctx.clone())
70 }
71
72 pub fn aggregate(&self) -> ScopedAggregate<'a, M, PK> {
73 ScopedAggregate::new(self.delegate.aggregate(), self.ctx.clone())
74 }
75
76 pub fn batch_get(&self, ids: Vec<PK>) -> ScopedBatchGet<'a, M, PK> {
77 ScopedBatchGet::new(self.delegate.batch_get(ids), self.ctx.clone())
78 }
79
80 pub fn batch_create<I>(&self, inputs: Vec<I>) -> ScopedBatchCreate<'a, M, PK, I> {
81 ScopedBatchCreate::new(self.delegate.batch_create(inputs), self.ctx.clone())
82 }
83
84 pub fn batch_update<I>(
85 &self,
86 items: Vec<BatchUpdateItem<PK, I>>,
87 ) -> ScopedBatchUpdate<'a, M, PK, I> {
88 ScopedBatchUpdate::new(self.delegate.batch_update(items), self.ctx.clone())
89 }
90
91 pub fn batch_delete(&self, ids: Vec<PK>) -> ScopedBatchDelete<'a, M, PK> {
92 ScopedBatchDelete::new(self.delegate.batch_delete(ids), self.ctx.clone())
93 }
94
95 pub fn batch_upsert<I>(&self, inputs: Vec<I>) -> ScopedBatchUpsert<'a, M, PK, I> {
96 ScopedBatchUpsert::new(self.delegate.batch_upsert(inputs), self.ctx.clone())
97 }
98}