dgraph_tonic/api/mutation.rs
1use serde::Serialize;
2use serde_json::Error;
3
4use crate::Mutation;
5
6impl Mutation {
7 ///
8 /// Create new Dgraph Mutation object.
9 ///
10 /// Mutation represent required modification of data in DB.
11 /// Mutation provides two main ways to set data: JSON and RDF N-Quad.
12 /// You can choose whichever way is convenient.
13 /// JSON way has implemented to helper functions.
14 ///
15 pub fn new() -> Self {
16 Self {
17 ..Default::default()
18 }
19 }
20
21 ///
22 /// Can be applied on a Mutation object to not run conflict detection over the index,
23 /// which would decrease the number of transaction conflicts and aborts.
24 /// However, this would come at the cost of potentially inconsistent upsert operations.
25 ///
26 /// # Example
27 ///
28 /// ```
29 /// use dgraph_tonic::Mutation;
30 /// let mut mu = Mutation::new().with_ignored_index_conflict();
31 /// ```
32 ///
33 #[cfg(feature = "dgraph-1-0")]
34 pub fn with_ignored_index_conflict(mut self) -> Self {
35 self.ignore_index_conflict = true;
36 self
37 }
38
39 ///
40 /// Set set JSON data in Mutation.
41 ///
42 /// # Arguments
43 ///
44 /// * `value` - ref to struct which can be serialized into JSON
45 ///
46 /// # Errors
47 ///
48 /// Return serde_json:Error when value cannot be serialized to JSON format
49 ///
50 /// # Examples
51 ///
52 /// ```
53 /// use dgraph_tonic::Mutation;
54 /// use serde::Serialize;
55 ///
56 /// #[derive(Serialize)]
57 /// struct Person {
58 /// uid: String,
59 /// name: String,
60 /// }
61 ///
62 /// let p = Person {
63 /// uid: "_:alice".into(),
64 /// name: "Alice".into(),
65 /// };
66 ///
67 /// let mut mu = Mutation::new();
68 /// mu.set_set_json(&p).expect("JSON");
69 /// ```
70 ///
71 pub fn set_set_json<T: ?Sized>(&mut self, value: &T) -> Result<(), Error>
72 where
73 T: Serialize,
74 {
75 let set_json = serde_json::to_vec(&value)?;
76 self.set_json = set_json;
77 Ok(())
78 }
79
80 ///
81 /// Set delete JSON data in Mutation.
82 ///
83 /// # Arguments
84 ///
85 /// * `value` - ref to struct which can be serialized into JSON
86 ///
87 /// # Errors
88 ///
89 /// Return serde_json:Error when value cannot be serialized to JSON format
90 ///
91 /// # Examples
92 ///
93 /// ```
94 /// use dgraph_tonic::Mutation;
95 /// use serde::Serialize;
96 ///
97 /// #[derive(Serialize)]
98 /// struct Person {
99 /// uid: String,
100 /// name: Option<String>,
101 /// }
102 ///
103 /// let p = Person {
104 /// uid: "_:0x1".into(),
105 /// name: None,
106 /// };
107 ///
108 /// let mut mu = Mutation::new();
109 /// //remove name predicate
110 /// mu.set_delete_json(&p).expect("JSON");
111 /// ```
112 ///
113 pub fn set_delete_json<T: ?Sized>(&mut self, value: &T) -> Result<(), Error>
114 where
115 T: Serialize,
116 {
117 let delete_json = serde_json::to_vec(&value)?;
118 self.delete_json = delete_json;
119 Ok(())
120 }
121
122 ///
123 /// Set set Nquads in Mutation.
124 ///
125 /// # Arguments
126 ///
127 /// * `nquads` - set nquads
128 ///
129 /// # Examples
130 ///
131 /// ```
132 /// use dgraph_tonic::Mutation;
133 ///
134 /// let mut mu = Mutation::new();
135 /// //remove name predicate
136 /// mu.set_set_nquads(r#"uid(user) <email> "correct_email@dgraph.io" ."#);
137 /// ```
138 ///
139 pub fn set_set_nquads<S: Into<String>>(&mut self, nquads: S) {
140 let n_quads: String = nquads.into();
141 self.set_nquads = n_quads.as_bytes().to_vec();
142 }
143
144 ///
145 /// Set delete Nquads in Mutation.
146 ///
147 /// # Arguments
148 ///
149 /// * `nquads` - delete nquads
150 ///
151 /// # Examples
152 ///
153 /// ```
154 /// use dgraph_tonic::Mutation;
155 ///
156 /// let mut mu = Mutation::new();
157 /// //remove name predicate
158 /// mu.set_set_nquads(r#"uid(user) <email> * ."#);
159 /// ```
160 ///
161 pub fn set_delete_nquads<S: Into<String>>(&mut self, nquads: S) {
162 let n_quads: String = nquads.into();
163 self.del_nquads = n_quads.as_bytes().to_vec();
164 }
165
166 ///
167 /// Set set condition in Mutation.
168 ///
169 /// # Arguments
170 ///
171 /// * `cond` - set nquads
172 ///
173 /// # Examples
174 ///
175 /// ```
176 /// use dgraph_tonic::Mutation;
177 ///
178 /// let mut mu = Mutation::new();
179 /// //remove name predicate
180 /// mu.set_cond("@if(eq(len(user), 1))");
181 /// ```
182 ///
183 pub fn set_cond<S: Into<String>>(&mut self, cond: S) {
184 self.cond = cond.into();
185 }
186}