Skip to main content

teaql_core/
mutation.rs

1use crate::{Record, Value};
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq)]
4pub enum MutationKind {
5    Insert,
6    Update,
7    Delete,
8    Recover,
9}
10
11#[derive(Debug, Clone, PartialEq)]
12pub struct InsertCommand {
13    pub entity: String,
14    pub values: Record,
15    pub trace_chain: Vec<crate::TraceNode>,
16}
17
18impl InsertCommand {
19    pub fn new(entity: impl Into<String>) -> Self {
20        Self {
21            entity: entity.into(),
22            values: Record::new(),
23            trace_chain: Vec::new(),
24        }
25    }
26
27    pub fn value(mut self, field: impl Into<String>, value: impl Into<Value>) -> Self {
28        self.values.insert(field.into(), value.into());
29        self
30    }
31}
32
33#[derive(Debug, Clone, PartialEq)]
34pub struct UpdateCommand {
35    pub entity: String,
36    pub id: Value,
37    pub expected_version: Option<i64>,
38    pub values: Record,
39    pub trace_chain: Vec<crate::TraceNode>,
40    pub old_values: Option<Record>,
41}
42
43impl UpdateCommand {
44    pub fn new(entity: impl Into<String>, id: impl Into<Value>) -> Self {
45        Self {
46            entity: entity.into(),
47            id: id.into(),
48            expected_version: None,
49            values: Record::new(),
50            trace_chain: Vec::new(),
51            old_values: None,
52        }
53    }
54
55    pub fn expected_version(mut self, version: i64) -> Self {
56        self.expected_version = Some(version);
57        self
58    }
59
60    pub fn value(mut self, field: impl Into<String>, value: impl Into<Value>) -> Self {
61        self.values.insert(field.into(), value.into());
62        self
63    }
64}
65
66#[derive(Debug, Clone, PartialEq)]
67pub struct BatchInsertCommand {
68    pub entity: String,
69    pub batch_values: Vec<Record>,
70    pub trace_chains: Vec<Vec<crate::TraceNode>>,
71}
72
73impl BatchInsertCommand {
74    pub fn new(entity: impl Into<String>) -> Self {
75        Self {
76            entity: entity.into(),
77            batch_values: Vec::new(),
78            trace_chains: Vec::new(),
79        }
80    }
81}
82
83#[derive(Debug, Clone, PartialEq)]
84pub struct BatchUpdateCommand {
85    pub entity: String,
86    pub batch_ids: Vec<Value>,
87    pub batch_expected_versions: Vec<Option<i64>>,
88    pub batch_values: Vec<Record>,
89    pub update_fields: Vec<String>,
90    pub trace_chains: Vec<Vec<crate::TraceNode>>,
91    pub batch_old_values: Vec<Option<Record>>,
92}
93
94impl BatchUpdateCommand {
95    pub fn new(entity: impl Into<String>, update_fields: Vec<String>) -> Self {
96        Self {
97            entity: entity.into(),
98            batch_ids: Vec::new(),
99            batch_expected_versions: Vec::new(),
100            batch_values: Vec::new(),
101            update_fields,
102            trace_chains: Vec::new(),
103            batch_old_values: Vec::new(),
104        }
105    }
106}
107
108#[derive(Debug, Clone, PartialEq)]
109pub struct DeleteCommand {
110    pub entity: String,
111    pub id: Value,
112    pub expected_version: Option<i64>,
113    pub soft_delete: bool,
114    pub trace_chain: Vec<crate::TraceNode>,
115}
116
117impl DeleteCommand {
118    pub fn new(entity: impl Into<String>, id: impl Into<Value>) -> Self {
119        Self {
120            entity: entity.into(),
121            id: id.into(),
122            expected_version: None,
123            soft_delete: true,
124            trace_chain: Vec::new(),
125        }
126    }
127
128    pub fn expected_version(mut self, version: i64) -> Self {
129        self.expected_version = Some(version);
130        self
131    }
132
133    pub fn hard_delete(mut self) -> Self {
134        self.soft_delete = false;
135        self
136    }
137}
138
139#[derive(Debug, Clone, PartialEq)]
140pub struct RecoverCommand {
141    pub entity: String,
142    pub id: Value,
143    pub expected_version: i64,
144    pub trace_chain: Vec<crate::TraceNode>,
145}
146
147impl RecoverCommand {
148    pub fn new(entity: impl Into<String>, id: impl Into<Value>, expected_version: i64) -> Self {
149        Self {
150            entity: entity.into(),
151            id: id.into(),
152            expected_version,
153            trace_chain: Vec::new(),
154        }
155    }
156}