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