pub struct TitoModel<E: TitoEngine, T> {
pub model: T,
pub engine: E,
}
Fields§
§model: T
§engine: E
Implementations§
Source§impl<E: TitoEngine, T: TitoModelConstraints> TitoModel<E, T>
impl<E: TitoEngine, T: TitoModelConstraints> TitoModel<E, T>
pub fn get_embedded_relationships(&self) -> Vec<TitoEmbeddedRelationshipConfig>
pub fn get_table(&self) -> String
pub fn get_id_from_table(&self, key: String) -> String
Sourcepub fn query_by_index(
&self,
index: impl Into<String>,
) -> IndexQueryBuilder<E, T>
pub fn query_by_index( &self, index: impl Into<String>, ) -> IndexQueryBuilder<E, T>
Examples found in repository?
examples/blog.rs (line 288)
123async fn main() -> Result<(), TitoError> {
124 // Connect to TiKV
125 let tito_db = TiKV::connect(vec!["127.0.0.1:2379"]).await?;
126
127 // Create models
128 let post_model = tito_db.clone().model::<Post>();
129 let tag_model = tito_db.clone().model::<Tag>();
130
131 // Create some tags
132 let tech_tag = tito_db
133 .transaction(|tx| {
134 let tag_model = tag_model.clone();
135 let tag = Tag {
136 id: DBUuid::new_v4().to_string(),
137 name: "Technology".to_string(),
138 description: "All about tech".to_string(),
139 };
140 let tag_clone = tag.clone();
141
142 async move {
143 tag_model.build(tag_clone, &tx).await?;
144 Ok::<_, TitoError>(tag)
145 }
146 })
147 .await?;
148
149 let travel_tag = tito_db
150 .transaction(|tx| {
151 let tag_model = tag_model.clone();
152 let tag = Tag {
153 id: DBUuid::new_v4().to_string(),
154 name: "Travel".to_string(),
155 description: "Adventures around the world".to_string(),
156 };
157 let tag_clone = tag.clone();
158
159 async move {
160 tag_model.build(tag_clone, &tx).await?;
161 Ok::<_, TitoError>(tag)
162 }
163 })
164 .await?;
165
166 let rust_tag = tito_db
167 .transaction(|tx| {
168 let tag_model = tag_model.clone();
169 let tag = Tag {
170 id: DBUuid::new_v4().to_string(),
171 name: "Rust".to_string(),
172 description: "Rust programming language".to_string(),
173 };
174 let tag_clone = tag.clone();
175
176 async move {
177 tag_model.build(tag_clone, &tx).await?;
178 Ok::<_, TitoError>(tag)
179 }
180 })
181 .await?;
182
183 let database_tag = tito_db
184 .transaction(|tx| {
185 let tag_model = tag_model.clone();
186 let tag = Tag {
187 id: DBUuid::new_v4().to_string(),
188 name: "Databases".to_string(),
189 description: "Database systems and technologies".to_string(),
190 };
191 let tag_clone = tag.clone();
192
193 async move {
194 tag_model.build(tag_clone, &tx).await?;
195 Ok::<_, TitoError>(tag)
196 }
197 })
198 .await?;
199
200 println!("Created tags:");
201 println!("- {}: {}", tech_tag.name, tech_tag.id);
202 println!("- {}: {}", travel_tag.name, travel_tag.id);
203 println!("- {}: {}", rust_tag.name, rust_tag.id);
204 println!("- {}: {}", database_tag.name, database_tag.id);
205
206 // Create some posts with multiple tags
207 let post1 = tito_db
208 .transaction(|tx| {
209 let post_model = post_model.clone();
210 let post = Post {
211 id: DBUuid::new_v4().to_string(),
212 title: "Introduction to TiKV".to_string(),
213 content: "TiKV is a distributed key-value storage system...".to_string(),
214 author: "Alice".to_string(),
215 tag_ids: vec![tech_tag.id.clone(), database_tag.id.clone()],
216 tags: Vec::new(),
217 };
218 let post_clone = post.clone();
219
220 async move {
221 post_model.build(post_clone, &tx).await?;
222 Ok::<_, TitoError>(post)
223 }
224 })
225 .await?;
226
227 let post2 = tito_db
228 .transaction(|tx| {
229 let post_model = post_model.clone();
230 let post = Post {
231 id: DBUuid::new_v4().to_string(),
232 title: "Best cities to visit in Europe".to_string(),
233 content: "Europe offers a diverse range of cities...".to_string(),
234 author: "Bob".to_string(),
235 tag_ids: vec![travel_tag.id.clone()],
236 tags: Vec::new(),
237 };
238 let post_clone = post.clone();
239
240 async move {
241 post_model.build(post_clone, &tx).await?;
242 Ok::<_, TitoError>(post)
243 }
244 })
245 .await?;
246
247 let post3 = tito_db
248 .transaction(|tx| {
249 let post_model = post_model.clone();
250 let post = Post {
251 id: DBUuid::new_v4().to_string(),
252 title: "Using Rust with TiKV".to_string(),
253 content: "Here are some examples of using Rust with TiKV...".to_string(),
254 author: "Alice".to_string(),
255 tag_ids: vec![
256 tech_tag.id.clone(),
257 rust_tag.id.clone(),
258 database_tag.id.clone(),
259 ],
260 tags: Vec::new(),
261 };
262 let post_clone = post.clone();
263
264 async move {
265 post_model.build(post_clone, &tx).await?;
266 Ok::<_, TitoError>(post)
267 }
268 })
269 .await?;
270
271 println!("\nCreated posts:");
272 println!("1. {} (by {})", post1.title, post1.author);
273 println!("2. {} (by {})", post2.title, post2.author);
274 println!("3. {} (by {})", post3.title, post3.author);
275
276 // Get a post with all its tags
277 let post_with_tags = post_model
278 .find_by_id(&post1.id, vec!["tags".to_string()])
279 .await?;
280 println!("\nPost with tags:");
281 println!("Title: {}", post_with_tags.title);
282 println!("Tags:");
283 for tag in &post_with_tags.tags {
284 println!("- {}", tag.name);
285 }
286
287 // Find posts by tag using the query builder
288 let mut tech_query = post_model.query_by_index("post-by-tag");
289 let tech_results = tech_query
290 .value(tech_tag.id.clone())
291 .relationship("tags")
292 .execute()
293 .await?;
294
295 println!("\nTechnology posts:");
296 for post in &tech_results.items {
297 println!("- {} (by {})", post.title, post.author);
298 println!(
299 " Tags: {}",
300 post.tags
301 .iter()
302 .map(|t| t.name.clone())
303 .collect::<Vec<_>>()
304 .join(", ")
305 );
306 }
307
308 // Find posts by author using the query builder
309 let mut alice_query = post_model.query_by_index("post-by-author");
310 let alice_results = alice_query
311 .value("Alice".to_string())
312 .relationship("tags")
313 .execute()
314 .await?;
315
316 println!("\nAlice's posts:");
317 for post in &alice_results.items {
318 println!("- {}", post.title);
319 println!(
320 " Tags: {}",
321 post.tags
322 .iter()
323 .map(|t| t.name.clone())
324 .collect::<Vec<_>>()
325 .join(", ")
326 );
327 }
328
329 Ok(())
330}
pub async fn tx<F, Fut, R, Err>(&self, f: F) -> Result<R, Err>
pub async fn get_key( &self, key: &str, tx: &E::Transaction, ) -> Result<Value, TitoError>
pub async fn delete( &self, key: String, tx: &E::Transaction, ) -> Result<bool, TitoError>
pub fn to_paginated_items_with_cursor( &self, items: Vec<(String, Value)>, cursor: String, ) -> Result<TitoPaginated<T>, TitoError>
pub fn to_paginated_items( &self, items: Vec<(String, Value)>, has_more: bool, ) -> Result<TitoPaginated<T>, TitoError>
pub fn get_nested_values( &self, json: &Value, field_path: &str, ) -> Option<Vec<Value>>
Sourcepub async fn build(
&self,
payload: T,
tx: &E::Transaction,
) -> Result<T, TitoError>where
T: DeserializeOwned,
pub async fn build(
&self,
payload: T,
tx: &E::Transaction,
) -> Result<T, TitoError>where
T: DeserializeOwned,
Examples found in repository?
examples/crud.rs (line 68)
49async fn main() -> Result<(), TitoError> {
50 // Connect to TiKV
51 let tito_db = TiKV::connect(vec!["127.0.0.1:2379"]).await?;
52
53 // Create model with dynamic backend
54 let user_model = tito_db.clone().model::<User>();
55
56 // Create a user
57 let user_id = DBUuid::new_v4().to_string();
58 let user = User {
59 id: user_id.clone(),
60 name: "John Doe".to_string(),
61 email: "john@example.com".to_string(),
62 };
63
64 // Create user with transaction
65 let saved_user = tito_db
66 .transaction(|tx| {
67 let user_model = user_model.clone();
68 async move { user_model.build(user, &tx).await }
69 })
70 .await?;
71
72 println!("Created user: {:?}", saved_user);
73
74 // Find user (find_by_id already uses transaction internally)
75 let found_user = user_model.find_by_id(&user_id, vec![]).await?;
76 println!("Found user: {:?}", found_user);
77
78 // Update user
79 let updated_user = User {
80 id: user_id.clone(),
81 name: "John Updated".to_string(),
82 email: "john_updated@example.com".to_string(),
83 };
84
85 tito_db
86 .transaction(|tx| {
87 let user_model = user_model.clone();
88 async move { user_model.update(updated_user, &tx).await }
89 })
90 .await?;
91
92 println!("User updated successfully");
93
94 // Delete user
95 tito_db
96 .transaction(|tx| {
97 let user_model = user_model.clone();
98 async move { user_model.delete_by_id(&user_id, &tx).await }
99 })
100 .await?;
101
102 println!("User deleted successfully");
103
104 Ok(())
105}
More examples
examples/blog.rs (line 143)
123async fn main() -> Result<(), TitoError> {
124 // Connect to TiKV
125 let tito_db = TiKV::connect(vec!["127.0.0.1:2379"]).await?;
126
127 // Create models
128 let post_model = tito_db.clone().model::<Post>();
129 let tag_model = tito_db.clone().model::<Tag>();
130
131 // Create some tags
132 let tech_tag = tito_db
133 .transaction(|tx| {
134 let tag_model = tag_model.clone();
135 let tag = Tag {
136 id: DBUuid::new_v4().to_string(),
137 name: "Technology".to_string(),
138 description: "All about tech".to_string(),
139 };
140 let tag_clone = tag.clone();
141
142 async move {
143 tag_model.build(tag_clone, &tx).await?;
144 Ok::<_, TitoError>(tag)
145 }
146 })
147 .await?;
148
149 let travel_tag = tito_db
150 .transaction(|tx| {
151 let tag_model = tag_model.clone();
152 let tag = Tag {
153 id: DBUuid::new_v4().to_string(),
154 name: "Travel".to_string(),
155 description: "Adventures around the world".to_string(),
156 };
157 let tag_clone = tag.clone();
158
159 async move {
160 tag_model.build(tag_clone, &tx).await?;
161 Ok::<_, TitoError>(tag)
162 }
163 })
164 .await?;
165
166 let rust_tag = tito_db
167 .transaction(|tx| {
168 let tag_model = tag_model.clone();
169 let tag = Tag {
170 id: DBUuid::new_v4().to_string(),
171 name: "Rust".to_string(),
172 description: "Rust programming language".to_string(),
173 };
174 let tag_clone = tag.clone();
175
176 async move {
177 tag_model.build(tag_clone, &tx).await?;
178 Ok::<_, TitoError>(tag)
179 }
180 })
181 .await?;
182
183 let database_tag = tito_db
184 .transaction(|tx| {
185 let tag_model = tag_model.clone();
186 let tag = Tag {
187 id: DBUuid::new_v4().to_string(),
188 name: "Databases".to_string(),
189 description: "Database systems and technologies".to_string(),
190 };
191 let tag_clone = tag.clone();
192
193 async move {
194 tag_model.build(tag_clone, &tx).await?;
195 Ok::<_, TitoError>(tag)
196 }
197 })
198 .await?;
199
200 println!("Created tags:");
201 println!("- {}: {}", tech_tag.name, tech_tag.id);
202 println!("- {}: {}", travel_tag.name, travel_tag.id);
203 println!("- {}: {}", rust_tag.name, rust_tag.id);
204 println!("- {}: {}", database_tag.name, database_tag.id);
205
206 // Create some posts with multiple tags
207 let post1 = tito_db
208 .transaction(|tx| {
209 let post_model = post_model.clone();
210 let post = Post {
211 id: DBUuid::new_v4().to_string(),
212 title: "Introduction to TiKV".to_string(),
213 content: "TiKV is a distributed key-value storage system...".to_string(),
214 author: "Alice".to_string(),
215 tag_ids: vec![tech_tag.id.clone(), database_tag.id.clone()],
216 tags: Vec::new(),
217 };
218 let post_clone = post.clone();
219
220 async move {
221 post_model.build(post_clone, &tx).await?;
222 Ok::<_, TitoError>(post)
223 }
224 })
225 .await?;
226
227 let post2 = tito_db
228 .transaction(|tx| {
229 let post_model = post_model.clone();
230 let post = Post {
231 id: DBUuid::new_v4().to_string(),
232 title: "Best cities to visit in Europe".to_string(),
233 content: "Europe offers a diverse range of cities...".to_string(),
234 author: "Bob".to_string(),
235 tag_ids: vec![travel_tag.id.clone()],
236 tags: Vec::new(),
237 };
238 let post_clone = post.clone();
239
240 async move {
241 post_model.build(post_clone, &tx).await?;
242 Ok::<_, TitoError>(post)
243 }
244 })
245 .await?;
246
247 let post3 = tito_db
248 .transaction(|tx| {
249 let post_model = post_model.clone();
250 let post = Post {
251 id: DBUuid::new_v4().to_string(),
252 title: "Using Rust with TiKV".to_string(),
253 content: "Here are some examples of using Rust with TiKV...".to_string(),
254 author: "Alice".to_string(),
255 tag_ids: vec![
256 tech_tag.id.clone(),
257 rust_tag.id.clone(),
258 database_tag.id.clone(),
259 ],
260 tags: Vec::new(),
261 };
262 let post_clone = post.clone();
263
264 async move {
265 post_model.build(post_clone, &tx).await?;
266 Ok::<_, TitoError>(post)
267 }
268 })
269 .await?;
270
271 println!("\nCreated posts:");
272 println!("1. {} (by {})", post1.title, post1.author);
273 println!("2. {} (by {})", post2.title, post2.author);
274 println!("3. {} (by {})", post3.title, post3.author);
275
276 // Get a post with all its tags
277 let post_with_tags = post_model
278 .find_by_id(&post1.id, vec!["tags".to_string()])
279 .await?;
280 println!("\nPost with tags:");
281 println!("Title: {}", post_with_tags.title);
282 println!("Tags:");
283 for tag in &post_with_tags.tags {
284 println!("- {}", tag.name);
285 }
286
287 // Find posts by tag using the query builder
288 let mut tech_query = post_model.query_by_index("post-by-tag");
289 let tech_results = tech_query
290 .value(tech_tag.id.clone())
291 .relationship("tags")
292 .execute()
293 .await?;
294
295 println!("\nTechnology posts:");
296 for post in &tech_results.items {
297 println!("- {} (by {})", post.title, post.author);
298 println!(
299 " Tags: {}",
300 post.tags
301 .iter()
302 .map(|t| t.name.clone())
303 .collect::<Vec<_>>()
304 .join(", ")
305 );
306 }
307
308 // Find posts by author using the query builder
309 let mut alice_query = post_model.query_by_index("post-by-author");
310 let alice_results = alice_query
311 .value("Alice".to_string())
312 .relationship("tags")
313 .execute()
314 .await?;
315
316 println!("\nAlice's posts:");
317 for post in &alice_results.items {
318 println!("- {}", post.title);
319 println!(
320 " Tags: {}",
321 post.tags
322 .iter()
323 .map(|t| t.name.clone())
324 .collect::<Vec<_>>()
325 .join(", ")
326 );
327 }
328
329 Ok(())
330}
pub async fn build_with_options(
&self,
payload: T,
options: TitoOptions,
tx: &E::Transaction,
) -> Result<T, TitoError>where
T: DeserializeOwned,
pub async fn find_by_id_tx(
&self,
id: &str,
rels: Vec<String>,
tx: &E::Transaction,
) -> Result<T, TitoError>where
T: DeserializeOwned,
Sourcepub async fn find_by_id(
&self,
id: &str,
rels: Vec<String>,
) -> Result<T, TitoError>where
T: DeserializeOwned,
pub async fn find_by_id(
&self,
id: &str,
rels: Vec<String>,
) -> Result<T, TitoError>where
T: DeserializeOwned,
Examples found in repository?
examples/crud.rs (line 75)
49async fn main() -> Result<(), TitoError> {
50 // Connect to TiKV
51 let tito_db = TiKV::connect(vec!["127.0.0.1:2379"]).await?;
52
53 // Create model with dynamic backend
54 let user_model = tito_db.clone().model::<User>();
55
56 // Create a user
57 let user_id = DBUuid::new_v4().to_string();
58 let user = User {
59 id: user_id.clone(),
60 name: "John Doe".to_string(),
61 email: "john@example.com".to_string(),
62 };
63
64 // Create user with transaction
65 let saved_user = tito_db
66 .transaction(|tx| {
67 let user_model = user_model.clone();
68 async move { user_model.build(user, &tx).await }
69 })
70 .await?;
71
72 println!("Created user: {:?}", saved_user);
73
74 // Find user (find_by_id already uses transaction internally)
75 let found_user = user_model.find_by_id(&user_id, vec![]).await?;
76 println!("Found user: {:?}", found_user);
77
78 // Update user
79 let updated_user = User {
80 id: user_id.clone(),
81 name: "John Updated".to_string(),
82 email: "john_updated@example.com".to_string(),
83 };
84
85 tito_db
86 .transaction(|tx| {
87 let user_model = user_model.clone();
88 async move { user_model.update(updated_user, &tx).await }
89 })
90 .await?;
91
92 println!("User updated successfully");
93
94 // Delete user
95 tito_db
96 .transaction(|tx| {
97 let user_model = user_model.clone();
98 async move { user_model.delete_by_id(&user_id, &tx).await }
99 })
100 .await?;
101
102 println!("User deleted successfully");
103
104 Ok(())
105}
More examples
examples/blog.rs (line 278)
123async fn main() -> Result<(), TitoError> {
124 // Connect to TiKV
125 let tito_db = TiKV::connect(vec!["127.0.0.1:2379"]).await?;
126
127 // Create models
128 let post_model = tito_db.clone().model::<Post>();
129 let tag_model = tito_db.clone().model::<Tag>();
130
131 // Create some tags
132 let tech_tag = tito_db
133 .transaction(|tx| {
134 let tag_model = tag_model.clone();
135 let tag = Tag {
136 id: DBUuid::new_v4().to_string(),
137 name: "Technology".to_string(),
138 description: "All about tech".to_string(),
139 };
140 let tag_clone = tag.clone();
141
142 async move {
143 tag_model.build(tag_clone, &tx).await?;
144 Ok::<_, TitoError>(tag)
145 }
146 })
147 .await?;
148
149 let travel_tag = tito_db
150 .transaction(|tx| {
151 let tag_model = tag_model.clone();
152 let tag = Tag {
153 id: DBUuid::new_v4().to_string(),
154 name: "Travel".to_string(),
155 description: "Adventures around the world".to_string(),
156 };
157 let tag_clone = tag.clone();
158
159 async move {
160 tag_model.build(tag_clone, &tx).await?;
161 Ok::<_, TitoError>(tag)
162 }
163 })
164 .await?;
165
166 let rust_tag = tito_db
167 .transaction(|tx| {
168 let tag_model = tag_model.clone();
169 let tag = Tag {
170 id: DBUuid::new_v4().to_string(),
171 name: "Rust".to_string(),
172 description: "Rust programming language".to_string(),
173 };
174 let tag_clone = tag.clone();
175
176 async move {
177 tag_model.build(tag_clone, &tx).await?;
178 Ok::<_, TitoError>(tag)
179 }
180 })
181 .await?;
182
183 let database_tag = tito_db
184 .transaction(|tx| {
185 let tag_model = tag_model.clone();
186 let tag = Tag {
187 id: DBUuid::new_v4().to_string(),
188 name: "Databases".to_string(),
189 description: "Database systems and technologies".to_string(),
190 };
191 let tag_clone = tag.clone();
192
193 async move {
194 tag_model.build(tag_clone, &tx).await?;
195 Ok::<_, TitoError>(tag)
196 }
197 })
198 .await?;
199
200 println!("Created tags:");
201 println!("- {}: {}", tech_tag.name, tech_tag.id);
202 println!("- {}: {}", travel_tag.name, travel_tag.id);
203 println!("- {}: {}", rust_tag.name, rust_tag.id);
204 println!("- {}: {}", database_tag.name, database_tag.id);
205
206 // Create some posts with multiple tags
207 let post1 = tito_db
208 .transaction(|tx| {
209 let post_model = post_model.clone();
210 let post = Post {
211 id: DBUuid::new_v4().to_string(),
212 title: "Introduction to TiKV".to_string(),
213 content: "TiKV is a distributed key-value storage system...".to_string(),
214 author: "Alice".to_string(),
215 tag_ids: vec![tech_tag.id.clone(), database_tag.id.clone()],
216 tags: Vec::new(),
217 };
218 let post_clone = post.clone();
219
220 async move {
221 post_model.build(post_clone, &tx).await?;
222 Ok::<_, TitoError>(post)
223 }
224 })
225 .await?;
226
227 let post2 = tito_db
228 .transaction(|tx| {
229 let post_model = post_model.clone();
230 let post = Post {
231 id: DBUuid::new_v4().to_string(),
232 title: "Best cities to visit in Europe".to_string(),
233 content: "Europe offers a diverse range of cities...".to_string(),
234 author: "Bob".to_string(),
235 tag_ids: vec![travel_tag.id.clone()],
236 tags: Vec::new(),
237 };
238 let post_clone = post.clone();
239
240 async move {
241 post_model.build(post_clone, &tx).await?;
242 Ok::<_, TitoError>(post)
243 }
244 })
245 .await?;
246
247 let post3 = tito_db
248 .transaction(|tx| {
249 let post_model = post_model.clone();
250 let post = Post {
251 id: DBUuid::new_v4().to_string(),
252 title: "Using Rust with TiKV".to_string(),
253 content: "Here are some examples of using Rust with TiKV...".to_string(),
254 author: "Alice".to_string(),
255 tag_ids: vec![
256 tech_tag.id.clone(),
257 rust_tag.id.clone(),
258 database_tag.id.clone(),
259 ],
260 tags: Vec::new(),
261 };
262 let post_clone = post.clone();
263
264 async move {
265 post_model.build(post_clone, &tx).await?;
266 Ok::<_, TitoError>(post)
267 }
268 })
269 .await?;
270
271 println!("\nCreated posts:");
272 println!("1. {} (by {})", post1.title, post1.author);
273 println!("2. {} (by {})", post2.title, post2.author);
274 println!("3. {} (by {})", post3.title, post3.author);
275
276 // Get a post with all its tags
277 let post_with_tags = post_model
278 .find_by_id(&post1.id, vec!["tags".to_string()])
279 .await?;
280 println!("\nPost with tags:");
281 println!("Title: {}", post_with_tags.title);
282 println!("Tags:");
283 for tag in &post_with_tags.tags {
284 println!("- {}", tag.name);
285 }
286
287 // Find posts by tag using the query builder
288 let mut tech_query = post_model.query_by_index("post-by-tag");
289 let tech_results = tech_query
290 .value(tech_tag.id.clone())
291 .relationship("tags")
292 .execute()
293 .await?;
294
295 println!("\nTechnology posts:");
296 for post in &tech_results.items {
297 println!("- {} (by {})", post.title, post.author);
298 println!(
299 " Tags: {}",
300 post.tags
301 .iter()
302 .map(|t| t.name.clone())
303 .collect::<Vec<_>>()
304 .join(", ")
305 );
306 }
307
308 // Find posts by author using the query builder
309 let mut alice_query = post_model.query_by_index("post-by-author");
310 let alice_results = alice_query
311 .value("Alice".to_string())
312 .relationship("tags")
313 .execute()
314 .await?;
315
316 println!("\nAlice's posts:");
317 for post in &alice_results.items {
318 println!("- {}", post.title);
319 println!(
320 " Tags: {}",
321 post.tags
322 .iter()
323 .map(|t| t.name.clone())
324 .collect::<Vec<_>>()
325 .join(", ")
326 );
327 }
328
329 Ok(())
330}
pub async fn scan(
&self,
payload: TitoScanPayload,
tx: &E::Transaction,
) -> Result<(Vec<(String, Value)>, bool), TitoError>where
T: DeserializeOwned,
pub async fn find_by_ids_tx(
&self,
ids: Vec<String>,
rels: Vec<String>,
tx: &E::Transaction,
) -> Result<Vec<T>, TitoError>where
T: DeserializeOwned,
pub async fn find_by_ids_raw(
&self,
ids: Vec<String>,
rels: Vec<String>,
tx: &E::Transaction,
) -> Result<Vec<(String, Value)>, TitoError>where
T: DeserializeOwned,
pub async fn find_by_ids(
&self,
ids: Vec<String>,
rels: Vec<String>,
) -> Result<Vec<T>, TitoError>where
T: DeserializeOwned,
pub async fn scan_reverse(
&self,
payload: TitoScanPayload,
tx: &E::Transaction,
) -> Result<(Vec<(String, Value)>, bool), TitoError>where
T: DeserializeOwned,
Sourcepub async fn update(
&self,
payload: T,
tx: &E::Transaction,
) -> Result<bool, TitoError>where
T: DeserializeOwned,
pub async fn update(
&self,
payload: T,
tx: &E::Transaction,
) -> Result<bool, TitoError>where
T: DeserializeOwned,
Examples found in repository?
examples/crud.rs (line 88)
49async fn main() -> Result<(), TitoError> {
50 // Connect to TiKV
51 let tito_db = TiKV::connect(vec!["127.0.0.1:2379"]).await?;
52
53 // Create model with dynamic backend
54 let user_model = tito_db.clone().model::<User>();
55
56 // Create a user
57 let user_id = DBUuid::new_v4().to_string();
58 let user = User {
59 id: user_id.clone(),
60 name: "John Doe".to_string(),
61 email: "john@example.com".to_string(),
62 };
63
64 // Create user with transaction
65 let saved_user = tito_db
66 .transaction(|tx| {
67 let user_model = user_model.clone();
68 async move { user_model.build(user, &tx).await }
69 })
70 .await?;
71
72 println!("Created user: {:?}", saved_user);
73
74 // Find user (find_by_id already uses transaction internally)
75 let found_user = user_model.find_by_id(&user_id, vec![]).await?;
76 println!("Found user: {:?}", found_user);
77
78 // Update user
79 let updated_user = User {
80 id: user_id.clone(),
81 name: "John Updated".to_string(),
82 email: "john_updated@example.com".to_string(),
83 };
84
85 tito_db
86 .transaction(|tx| {
87 let user_model = user_model.clone();
88 async move { user_model.update(updated_user, &tx).await }
89 })
90 .await?;
91
92 println!("User updated successfully");
93
94 // Delete user
95 tito_db
96 .transaction(|tx| {
97 let user_model = user_model.clone();
98 async move { user_model.delete_by_id(&user_id, &tx).await }
99 })
100 .await?;
101
102 println!("User deleted successfully");
103
104 Ok(())
105}
pub fn get_last_id(&self, key: String) -> Option<String>
pub async fn update_with_options(
&self,
payload: T,
options: TitoOptions,
tx: &E::Transaction,
) -> Result<bool, TitoError>where
T: DeserializeOwned,
pub async fn lock_keys( &self, keys: Vec<String>, tx: &E::Transaction, ) -> Result<bool, TitoError>
pub async fn batch_get( &self, keys: Vec<String>, max_retries: usize, initial_delay_ms: u64, tx: &E::Transaction, ) -> Result<Vec<(String, Value)>, TitoError>
pub async fn delete_by_id_with_options( &self, raw_id: &str, options: TitoOptions, tx: &E::Transaction, ) -> Result<bool, TitoError>
Sourcepub async fn delete_by_id(
&self,
raw_id: &str,
tx: &E::Transaction,
) -> Result<bool, TitoError>
pub async fn delete_by_id( &self, raw_id: &str, tx: &E::Transaction, ) -> Result<bool, TitoError>
Examples found in repository?
examples/crud.rs (line 98)
49async fn main() -> Result<(), TitoError> {
50 // Connect to TiKV
51 let tito_db = TiKV::connect(vec!["127.0.0.1:2379"]).await?;
52
53 // Create model with dynamic backend
54 let user_model = tito_db.clone().model::<User>();
55
56 // Create a user
57 let user_id = DBUuid::new_v4().to_string();
58 let user = User {
59 id: user_id.clone(),
60 name: "John Doe".to_string(),
61 email: "john@example.com".to_string(),
62 };
63
64 // Create user with transaction
65 let saved_user = tito_db
66 .transaction(|tx| {
67 let user_model = user_model.clone();
68 async move { user_model.build(user, &tx).await }
69 })
70 .await?;
71
72 println!("Created user: {:?}", saved_user);
73
74 // Find user (find_by_id already uses transaction internally)
75 let found_user = user_model.find_by_id(&user_id, vec![]).await?;
76 println!("Found user: {:?}", found_user);
77
78 // Update user
79 let updated_user = User {
80 id: user_id.clone(),
81 name: "John Updated".to_string(),
82 email: "john_updated@example.com".to_string(),
83 };
84
85 tito_db
86 .transaction(|tx| {
87 let user_model = user_model.clone();
88 async move { user_model.update(updated_user, &tx).await }
89 })
90 .await?;
91
92 println!("User updated successfully");
93
94 // Delete user
95 tito_db
96 .transaction(|tx| {
97 let user_model = user_model.clone();
98 async move { user_model.delete_by_id(&user_id, &tx).await }
99 })
100 .await?;
101
102 println!("User deleted successfully");
103
104 Ok(())
105}
pub async fn find(
&self,
payload: TitoFindPayload,
) -> Result<TitoPaginated<T>, TitoError>where
T: DeserializeOwned,
pub async fn add_field( &self, field_name: &str, field_value: Value, ) -> Result<(), TitoError>
pub async fn remove_field(&self, field_name: &str) -> Result<(), TitoError>
pub async fn find_all(&self) -> Result<TitoPaginated<T>, TitoError>
Source§impl<E: TitoEngine, T: Default + Clone + Serialize + DeserializeOwned + Unpin + Send + Sync + TitoModelTrait> TitoModel<E, T>
impl<E: TitoEngine, T: Default + Clone + Serialize + DeserializeOwned + Unpin + Send + Sync + TitoModelTrait> TitoModel<E, T>
pub fn get_index_keys( &self, id: String, value: &T, json: &Value, ) -> Result<Vec<(String, Value)>, TitoError>
pub async fn find_by_index_raw(
&self,
payload: TitoFindByIndexPayload,
tx: &E::Transaction,
) -> Result<(Vec<(String, Value)>, bool), TitoError>where
T: DeserializeOwned,
pub async fn find_by_index_reverse_raw(
&self,
payload: TitoFindByIndexPayload,
tx: &E::Transaction,
) -> Result<(Vec<(String, Value)>, bool), TitoError>where
T: DeserializeOwned,
pub async fn find_by_index_tx(
&self,
payload: TitoFindByIndexPayload,
tx: &E::Transaction,
) -> Result<TitoPaginated<T>, TitoError>where
T: DeserializeOwned,
pub async fn find_by_index(
&self,
payload: TitoFindByIndexPayload,
) -> Result<TitoPaginated<T>, TitoError>where
T: DeserializeOwned,
pub async fn find_by_index_reverse_tx(
&self,
payload: TitoFindByIndexPayload,
tx: &E::Transaction,
) -> Result<TitoPaginated<T>, TitoError>where
T: DeserializeOwned,
pub async fn find_by_index_reverse(
&self,
payload: TitoFindByIndexPayload,
) -> Result<TitoPaginated<T>, TitoError>where
T: DeserializeOwned,
pub async fn find_one_by_index_tx(
&self,
payload: TitoFindOneByIndexPayload,
tx: &E::Transaction,
) -> Result<T, TitoError>where
T: DeserializeOwned,
pub async fn find_one_by_index(
&self,
payload: TitoFindOneByIndexPayload,
) -> Result<T, TitoError>where
T: DeserializeOwned,
pub async fn reindex(&self) -> Result<(), TitoError>
Source§impl<E: TitoEngine, T: Default + Clone + Serialize + DeserializeOwned + Unpin + Send + Sync + TitoModelTrait> TitoModel<E, T>
impl<E: TitoEngine, T: Default + Clone + Serialize + DeserializeOwned + Unpin + Send + Sync + TitoModelTrait> TitoModel<E, T>
Sourcepub fn stitch_relationship(
&self,
item: &mut Value,
rel_map: &HashMap<String, Value>,
config: &TitoEmbeddedRelationshipConfig,
)
pub fn stitch_relationship( &self, item: &mut Value, rel_map: &HashMap<String, Value>, config: &TitoEmbeddedRelationshipConfig, )
Public entry point for stitching a relationship into a JSON Value.
Sourcepub fn get_relationship_data(
&self,
items: &Vec<(String, Value)>,
rels_config: &[TitoEmbeddedRelationshipConfig],
rels: &Vec<String>,
) -> Vec<(TitoEmbeddedRelationshipConfig, String)>
pub fn get_relationship_data( &self, items: &Vec<(String, Value)>, rels_config: &[TitoEmbeddedRelationshipConfig], rels: &Vec<String>, ) -> Vec<(TitoEmbeddedRelationshipConfig, String)>
Gathers all relationship keys that need to be fetched from a list of items.
Sourcepub async fn fetch_and_stitch_relationships(
&self,
items: Vec<(String, Value)>,
rels: Vec<String>,
tx: &E::Transaction,
) -> Result<Vec<(String, Value)>, TitoError>
pub async fn fetch_and_stitch_relationships( &self, items: Vec<(String, Value)>, rels: Vec<String>, tx: &E::Transaction, ) -> Result<Vec<(String, Value)>, TitoError>
Fetches and stitches all requested relationships for a vector of items.
Sourcepub fn extract_relationship(&self, input: &str) -> Option<String>
pub fn extract_relationship(&self, input: &str) -> Option<String>
A helper function to extract a relationship string.
Trait Implementations§
Auto Trait Implementations§
impl<E, T> Freeze for TitoModel<E, T>
impl<E, T> RefUnwindSafe for TitoModel<E, T>where
T: RefUnwindSafe,
E: RefUnwindSafe,
impl<E, T> Send for TitoModel<E, T>where
T: Send,
impl<E, T> Sync for TitoModel<E, T>where
T: Sync,
impl<E, T> Unpin for TitoModel<E, T>
impl<E, T> UnwindSafe for TitoModel<E, T>where
T: UnwindSafe,
E: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
Wrap the input message
T
in a tonic::Request