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}
16
17impl InsertCommand {
18    pub fn new(entity: impl Into<String>) -> Self {
19        Self {
20            entity: entity.into(),
21            values: Record::new(),
22        }
23    }
24
25    pub fn value(mut self, field: impl Into<String>, value: impl Into<Value>) -> Self {
26        self.values.insert(field.into(), value.into());
27        self
28    }
29}
30
31#[derive(Debug, Clone, PartialEq)]
32pub struct UpdateCommand {
33    pub entity: String,
34    pub id: Value,
35    pub expected_version: Option<i64>,
36    pub values: Record,
37}
38
39impl UpdateCommand {
40    pub fn new(entity: impl Into<String>, id: impl Into<Value>) -> Self {
41        Self {
42            entity: entity.into(),
43            id: id.into(),
44            expected_version: None,
45            values: Record::new(),
46        }
47    }
48
49    pub fn expected_version(mut self, version: i64) -> Self {
50        self.expected_version = Some(version);
51        self
52    }
53
54    pub fn value(mut self, field: impl Into<String>, value: impl Into<Value>) -> Self {
55        self.values.insert(field.into(), value.into());
56        self
57    }
58}
59
60#[derive(Debug, Clone, PartialEq)]
61pub struct DeleteCommand {
62    pub entity: String,
63    pub id: Value,
64    pub expected_version: Option<i64>,
65    pub soft_delete: bool,
66}
67
68impl DeleteCommand {
69    pub fn new(entity: impl Into<String>, id: impl Into<Value>) -> Self {
70        Self {
71            entity: entity.into(),
72            id: id.into(),
73            expected_version: None,
74            soft_delete: true,
75        }
76    }
77
78    pub fn expected_version(mut self, version: i64) -> Self {
79        self.expected_version = Some(version);
80        self
81    }
82
83    pub fn hard_delete(mut self) -> Self {
84        self.soft_delete = false;
85        self
86    }
87}
88
89#[derive(Debug, Clone, PartialEq)]
90pub struct RecoverCommand {
91    pub entity: String,
92    pub id: Value,
93    pub expected_version: i64,
94}
95
96impl RecoverCommand {
97    pub fn new(entity: impl Into<String>, id: impl Into<Value>, expected_version: i64) -> Self {
98        Self {
99            entity: entity.into(),
100            id: id.into(),
101            expected_version,
102        }
103    }
104}