nullnet_libdatastore/builders/
upsert_request_builder.rs

1use crate::datastore::{Params, Query, UpsertBody, UpsertRequest};
2
3#[derive(Debug, Default)]
4pub struct UpsertRequestBuilder {
5    id: Option<String>,
6    table: Option<String>,
7    pluck: Option<String>,
8    durability: Option<String>,
9    data: Option<String>,
10    conflict_columns: Vec<String>,
11    is_root: bool,
12}
13
14impl UpsertRequestBuilder {
15    pub fn new() -> Self {
16        Self::default()
17    }
18
19    pub fn id(mut self, id: impl Into<String>) -> Self {
20        self.id = Some(id.into());
21        self
22    }
23
24    pub fn table(mut self, table: impl Into<String>) -> Self {
25        self.table = Some(table.into());
26        self
27    }
28
29    pub fn query(mut self, pluck: impl Into<String>, durability: impl Into<String>) -> Self {
30        self.pluck = Some(pluck.into());
31        self.durability = Some(durability.into());
32        self
33    }
34
35    pub fn data(mut self, data: impl Into<String>) -> Self {
36        self.data = Some(data.into());
37        self
38    }
39
40    pub fn conflict_column(mut self, column: impl Into<String>) -> Self {
41        self.conflict_columns.push(column.into());
42        self
43    }
44
45    pub fn conflict_columns<I, S>(mut self, columns: I) -> Self
46    where
47        I: IntoIterator<Item = S>,
48        S: Into<String>,
49    {
50        self.conflict_columns
51            .extend(columns.into_iter().map(|s| s.into()));
52        self
53    }
54
55    pub fn performed_by_root(mut self, value: bool) -> Self {
56        self.is_root = value;
57        self
58    }
59
60    pub fn build(self) -> UpsertRequest {
61        UpsertRequest {
62            params: Some(Params {
63                id: self.id.unwrap_or_default(),
64                table: self.table.unwrap_or_default(),
65                r#type: if self.is_root {
66                    String::from("root")
67                } else {
68                    String::new()
69                },
70            }),
71            query: Some(Query {
72                pluck: self.pluck.unwrap_or_default(),
73                durability: self.durability.unwrap_or_default(),
74            }),
75            body: Some(UpsertBody {
76                data: self.data.unwrap_or_default(),
77                conflict_columns: self.conflict_columns,
78            }),
79        }
80    }
81}