cratestack_sqlx/delegate/
scoped_batch.rs1use std::hash::Hash;
7
8use cratestack_core::{BatchResponse, CoolContext, CoolError};
9
10use crate::{
11 BatchCreate, BatchDelete, BatchGet, BatchUpdate, BatchUpsert, CreateModelInput,
12 UpdateModelInput, UpsertModelInput, sqlx,
13};
14
15#[derive(Debug, Clone)]
16pub struct ScopedBatchGet<'a, M: 'static, PK: 'static> {
17 request: BatchGet<'a, M, PK>,
18 ctx: CoolContext,
19}
20
21impl<'a, M: 'static, PK: 'static> ScopedBatchGet<'a, M, PK> {
22 pub(super) fn new(request: BatchGet<'a, M, PK>, ctx: CoolContext) -> Self {
23 Self { request, ctx }
24 }
25
26 pub async fn run(self) -> Result<BatchResponse<M>, CoolError>
27 where
28 for<'r> M: Send + Unpin + sqlx::FromRow<'r, sqlx::postgres::PgRow> + crate::ModelPrimaryKey<PK>,
29 PK: Clone + Eq + Hash + Send + sqlx::Type<sqlx::Postgres> + for<'q> sqlx::Encode<'q, sqlx::Postgres>,
30 {
31 self.request.run(&self.ctx).await
32 }
33}
34
35#[derive(Debug, Clone)]
36pub struct ScopedBatchCreate<'a, M: 'static, PK: 'static, I> {
37 request: BatchCreate<'a, M, PK, I>,
38 ctx: CoolContext,
39}
40
41impl<'a, M: 'static, PK: 'static, I> ScopedBatchCreate<'a, M, PK, I> {
42 pub(super) fn new(request: BatchCreate<'a, M, PK, I>, ctx: CoolContext) -> Self {
43 Self { request, ctx }
44 }
45}
46
47impl<'a, M: 'static, PK: 'static, I> ScopedBatchCreate<'a, M, PK, I>
48where
49 I: CreateModelInput<M> + Send,
50{
51 pub async fn run(self) -> Result<BatchResponse<M>, CoolError>
52 where
53 for<'r> M: Send + Unpin + sqlx::FromRow<'r, sqlx::postgres::PgRow> + serde::Serialize,
54 {
55 self.request.run(&self.ctx).await
56 }
57}
58
59#[derive(Debug, Clone)]
60pub struct ScopedBatchUpdate<'a, M: 'static, PK: 'static, I> {
61 request: BatchUpdate<'a, M, PK, I>,
62 ctx: CoolContext,
63}
64
65impl<'a, M: 'static, PK: 'static, I> ScopedBatchUpdate<'a, M, PK, I> {
66 pub(super) fn new(request: BatchUpdate<'a, M, PK, I>, ctx: CoolContext) -> Self {
67 Self { request, ctx }
68 }
69}
70
71impl<'a, M: 'static, PK: 'static, I> ScopedBatchUpdate<'a, M, PK, I>
72where
73 I: UpdateModelInput<M> + Send,
74{
75 pub async fn run(self) -> Result<BatchResponse<M>, CoolError>
76 where
77 for<'r> M: Send + Unpin + sqlx::FromRow<'r, sqlx::postgres::PgRow> + serde::Serialize,
78 PK: Clone + Eq + Hash + Send + sqlx::Type<sqlx::Postgres> + for<'q> sqlx::Encode<'q, sqlx::Postgres>,
79 {
80 self.request.run(&self.ctx).await
81 }
82}
83
84#[derive(Debug, Clone)]
85pub struct ScopedBatchDelete<'a, M: 'static, PK: 'static> {
86 request: BatchDelete<'a, M, PK>,
87 ctx: CoolContext,
88}
89
90impl<'a, M: 'static, PK: 'static> ScopedBatchDelete<'a, M, PK> {
91 pub(super) fn new(request: BatchDelete<'a, M, PK>, ctx: CoolContext) -> Self {
92 Self { request, ctx }
93 }
94
95 pub async fn run(self) -> Result<BatchResponse<M>, CoolError>
96 where
97 for<'r> M: Send + Unpin + sqlx::FromRow<'r, sqlx::postgres::PgRow> + crate::ModelPrimaryKey<PK> + serde::Serialize,
98 PK: Clone + Eq + Hash + Send + sqlx::Type<sqlx::Postgres> + for<'q> sqlx::Encode<'q, sqlx::Postgres>,
99 {
100 self.request.run(&self.ctx).await
101 }
102}
103
104#[derive(Debug, Clone)]
105pub struct ScopedBatchUpsert<'a, M: 'static, PK: 'static, I> {
106 request: BatchUpsert<'a, M, PK, I>,
107 ctx: CoolContext,
108}
109
110impl<'a, M: 'static, PK: 'static, I> ScopedBatchUpsert<'a, M, PK, I> {
111 pub(super) fn new(request: BatchUpsert<'a, M, PK, I>, ctx: CoolContext) -> Self {
112 Self { request, ctx }
113 }
114}
115
116impl<'a, M: 'static, PK: 'static, I> ScopedBatchUpsert<'a, M, PK, I>
117where
118 I: UpsertModelInput<M>,
119{
120 pub async fn run(self) -> Result<BatchResponse<M>, CoolError>
121 where
122 for<'r> M: Send + Unpin + sqlx::FromRow<'r, sqlx::postgres::PgRow> + serde::Serialize,
123 PK: Send + sqlx::Type<sqlx::Postgres> + for<'q> sqlx::Encode<'q, sqlx::Postgres>,
124 {
125 self.request.run(&self.ctx).await
126 }
127}