-
-
Notifications
You must be signed in to change notification settings - Fork 111
/
upsert.rs
145 lines (117 loc) · 3.83 KB
/
upsert.rs
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
use prisma_models::PrismaValue;
use query_core::{Operation, SelectionBuilder};
use crate::{
include::Include,
select::{Select, SelectType},
BatchQuery, ModelAction, ModelActionType, ModelActions, ModelMutationType,
PrismaClientInternals, WhereInput,
};
pub struct Upsert<'a, Actions>
where
Actions: ModelActions,
{
client: &'a PrismaClientInternals,
pub where_param: Actions::Where,
pub create_params: Vec<Actions::Set>,
pub update_params: Vec<Actions::Set>,
pub with_params: Vec<Actions::With>,
}
impl<'a, Actions> ModelAction for Upsert<'a, Actions>
where
Actions: ModelActions,
{
type Actions = Actions;
const TYPE: ModelActionType = ModelActionType::Mutation(ModelMutationType::Upsert);
}
impl<'a, Actions> Upsert<'a, Actions>
where
Actions: ModelActions,
{
pub fn new(
client: &'a PrismaClientInternals,
where_param: Actions::Where,
create_params: Vec<Actions::Set>,
update_params: Vec<Actions::Set>,
) -> Self {
Self {
client,
where_param,
create_params,
update_params,
with_params: vec![],
}
}
pub fn with(mut self, param: impl Into<Actions::With>) -> Self {
self.with_params.push(param.into());
self
}
fn to_selection(
where_param: Actions::Where,
create_params: Vec<Actions::Set>,
update_params: Vec<Actions::Set>,
) -> SelectionBuilder {
let mut selection = Self::base_selection();
selection.push_argument(
"where",
PrismaValue::Object(vec![where_param.serialize().transform_equals()]),
);
selection.push_argument(
"create",
PrismaValue::Object(create_params.into_iter().map(Into::into).collect()),
);
selection.push_argument(
"update",
PrismaValue::Object(update_params.into_iter().map(Into::into).collect()),
);
selection
}
pub fn select<S: SelectType<ModelData = Actions::Data>>(
self,
select: S,
) -> Select<'a, S::Data> {
let mut selection =
Self::to_selection(self.where_param, self.create_params, self.update_params);
selection.nested_selections(select.to_selections());
let op = Operation::Write(selection.build());
Select::new(self.client, op)
}
pub fn include<I: SelectType<ModelData = Actions::Data>>(
self,
select: I,
) -> Include<'a, I::Data> {
let mut selection =
Self::to_selection(self.where_param, self.create_params, self.update_params);
selection.nested_selections(select.to_selections());
let op = Operation::Write(selection.build());
Include::new(self.client, op)
}
pub(crate) fn exec_operation(self) -> (Operation, &'a PrismaClientInternals) {
let mut selection =
Self::to_selection(self.where_param, self.create_params, self.update_params);
let mut scalar_selections = Actions::scalar_selections();
if self.with_params.len() > 0 {
scalar_selections.append(&mut self.with_params.into_iter().map(Into::into).collect());
}
selection.nested_selections(scalar_selections);
(Operation::Write(selection.build()), self.client)
}
pub async fn exec(self) -> super::Result<Actions::Data> {
let (op, client) = self.exec_operation();
let res = client.execute(op).await?;
client.notify_model_mutation::<Self>();
Ok(res)
}
}
impl<'a, Actions> BatchQuery for Upsert<'a, Actions>
where
Actions: ModelActions,
{
type RawType = Actions::Data;
type ReturnType = Self::RawType;
fn graphql(self) -> Operation {
self.exec_operation().0
}
fn convert(raw: Self::RawType) -> Self::ReturnType {
raw
}
}