pub struct TitoModel<E: TitoEngine, T> {
pub engine: E,
pub partition_count: u32,
/* private fields */
}Fields§
§engine: E§partition_count: u32Implementations§
Source§impl<E: TitoEngine, T: TitoModelConstraints> TitoModel<E, T>
impl<E: TitoEngine, T: TitoModelConstraints> TitoModel<E, T>
pub fn new(engine: E, options: TitoModelOptions) -> Self
pub fn relationships(&self) -> Vec<TitoRelationshipConfig> ⓘ
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 161)
88async fn main() -> Result<(), TitoError> {
89 let tito_db = TiKV::connect(vec!["127.0.0.1:2379"]).await?;
90
91 let post_model = tito_db.clone().model::<Post>(TitoModelOptions::default());
92 let tag_model = tito_db.clone().model::<Tag>(TitoModelOptions::default());
93
94 let tech_tag = tito_db
95 .transaction(|tx| {
96 let tag_model = tag_model.clone();
97 let tag = Tag {
98 id: DBUuid::new_v4().to_string(),
99 name: "Technology".to_string(),
100 description: "All about tech".to_string(),
101 };
102 let tag_clone = tag.clone();
103 async move {
104 tag_model.set(tag_clone).execute(&tx).await?;
105 Ok::<_, TitoError>(tag)
106 }
107 })
108 .await?;
109
110 let rust_tag = tito_db
111 .transaction(|tx| {
112 let tag_model = tag_model.clone();
113 let tag = Tag {
114 id: DBUuid::new_v4().to_string(),
115 name: "Rust".to_string(),
116 description: "Rust programming".to_string(),
117 };
118 let tag_clone = tag.clone();
119 async move {
120 tag_model.set(tag_clone).execute(&tx).await?;
121 Ok::<_, TitoError>(tag)
122 }
123 })
124 .await?;
125
126 println!("Created tags: {}, {}", tech_tag.name, rust_tag.name);
127
128 let post = tito_db
129 .transaction(|tx| {
130 let post_model = post_model.clone();
131 let post = Post {
132 id: DBUuid::new_v4().to_string(),
133 title: "Using Rust with TiKV".to_string(),
134 content: "Examples of using Rust with TiKV...".to_string(),
135 author: "Alice".to_string(),
136 tag_ids: vec![tech_tag.id.clone(), rust_tag.id.clone()],
137 tags: Vec::new(),
138 };
139 let post_clone = post.clone();
140 async move {
141 post_model.set(post_clone).execute(&tx).await?;
142 Ok::<_, TitoError>(post)
143 }
144 })
145 .await?;
146
147 println!("Created post: {}", post.title);
148
149 let post_with_tags = post_model
150 .get(&post.id)
151 .relationship("tags")
152 .execute(None)
153 .await?;
154
155 println!("Post: {}", post_with_tags.title);
156 println!("Tags:");
157 for tag in &post_with_tags.tags {
158 println!("- {}", tag.name);
159 }
160
161 let mut query = post_model.query_by_index("post-by-author");
162 let results = query
163 .value("Alice".to_string())
164 .relationship("tags")
165 .execute(None)
166 .await?;
167
168 println!("\nAlice's posts:");
169 for p in &results.items {
170 println!(
171 "- {} (tags: {})",
172 p.title,
173 p.tags
174 .iter()
175 .map(|t| t.name.clone())
176 .collect::<Vec<_>>()
177 .join(", ")
178 );
179 }
180
181 Ok(())
182}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<FieldValue>>
Sourcepub fn set(&self, payload: T) -> SetBuilder<'_, E, T>
pub fn set(&self, payload: T) -> SetBuilder<'_, E, T>
Examples found in repository?
examples/crud.rs (line 52)
38async fn main() -> Result<(), TitoError> {
39 let tito_db = TiKV::connect(vec!["127.0.0.1:2379"]).await?;
40 let user_model = tito_db.clone().model::<User>(TitoModelOptions::default());
41
42 let user_id = DBUuid::new_v4().to_string();
43 let user = User {
44 id: user_id.clone(),
45 name: "John Doe".to_string(),
46 email: "john@example.com".to_string(),
47 };
48
49 let saved_user = tito_db
50 .transaction(|tx| {
51 let user_model = user_model.clone();
52 async move { user_model.set(user).execute(&tx).await }
53 })
54 .await?;
55
56 println!("Created user: {:?}", saved_user);
57
58 let found_user = user_model.get(&user_id).execute(None).await?;
59 println!("Found user: {:?}", found_user);
60
61 let updated_user = User {
62 id: user_id.clone(),
63 name: "John Updated".to_string(),
64 email: "john_updated@example.com".to_string(),
65 };
66
67 tito_db
68 .transaction(|tx| {
69 let user_model = user_model.clone();
70 async move { user_model.set(updated_user).execute(&tx).await }
71 })
72 .await?;
73
74 println!("User updated");
75
76 tito_db
77 .transaction(|tx| {
78 let user_model = user_model.clone();
79 async move { user_model.remove(&user_id, &tx).await }
80 })
81 .await?;
82
83 println!("User deleted");
84
85 Ok(())
86}More examples
examples/blog.rs (line 104)
88async fn main() -> Result<(), TitoError> {
89 let tito_db = TiKV::connect(vec!["127.0.0.1:2379"]).await?;
90
91 let post_model = tito_db.clone().model::<Post>(TitoModelOptions::default());
92 let tag_model = tito_db.clone().model::<Tag>(TitoModelOptions::default());
93
94 let tech_tag = tito_db
95 .transaction(|tx| {
96 let tag_model = tag_model.clone();
97 let tag = Tag {
98 id: DBUuid::new_v4().to_string(),
99 name: "Technology".to_string(),
100 description: "All about tech".to_string(),
101 };
102 let tag_clone = tag.clone();
103 async move {
104 tag_model.set(tag_clone).execute(&tx).await?;
105 Ok::<_, TitoError>(tag)
106 }
107 })
108 .await?;
109
110 let rust_tag = tito_db
111 .transaction(|tx| {
112 let tag_model = tag_model.clone();
113 let tag = Tag {
114 id: DBUuid::new_v4().to_string(),
115 name: "Rust".to_string(),
116 description: "Rust programming".to_string(),
117 };
118 let tag_clone = tag.clone();
119 async move {
120 tag_model.set(tag_clone).execute(&tx).await?;
121 Ok::<_, TitoError>(tag)
122 }
123 })
124 .await?;
125
126 println!("Created tags: {}, {}", tech_tag.name, rust_tag.name);
127
128 let post = tito_db
129 .transaction(|tx| {
130 let post_model = post_model.clone();
131 let post = Post {
132 id: DBUuid::new_v4().to_string(),
133 title: "Using Rust with TiKV".to_string(),
134 content: "Examples of using Rust with TiKV...".to_string(),
135 author: "Alice".to_string(),
136 tag_ids: vec![tech_tag.id.clone(), rust_tag.id.clone()],
137 tags: Vec::new(),
138 };
139 let post_clone = post.clone();
140 async move {
141 post_model.set(post_clone).execute(&tx).await?;
142 Ok::<_, TitoError>(post)
143 }
144 })
145 .await?;
146
147 println!("Created post: {}", post.title);
148
149 let post_with_tags = post_model
150 .get(&post.id)
151 .relationship("tags")
152 .execute(None)
153 .await?;
154
155 println!("Post: {}", post_with_tags.title);
156 println!("Tags:");
157 for tag in &post_with_tags.tags {
158 println!("- {}", tag.name);
159 }
160
161 let mut query = post_model.query_by_index("post-by-author");
162 let results = query
163 .value("Alice".to_string())
164 .relationship("tags")
165 .execute(None)
166 .await?;
167
168 println!("\nAlice's posts:");
169 for p in &results.items {
170 println!(
171 "- {} (tags: {})",
172 p.title,
173 p.tags
174 .iter()
175 .map(|t| t.name.clone())
176 .collect::<Vec<_>>()
177 .join(", ")
178 );
179 }
180
181 Ok(())
182}Sourcepub fn get(&self, id: &str) -> GetBuilder<'_, E, T>
pub fn get(&self, id: &str) -> GetBuilder<'_, E, T>
Examples found in repository?
examples/crud.rs (line 58)
38async fn main() -> Result<(), TitoError> {
39 let tito_db = TiKV::connect(vec!["127.0.0.1:2379"]).await?;
40 let user_model = tito_db.clone().model::<User>(TitoModelOptions::default());
41
42 let user_id = DBUuid::new_v4().to_string();
43 let user = User {
44 id: user_id.clone(),
45 name: "John Doe".to_string(),
46 email: "john@example.com".to_string(),
47 };
48
49 let saved_user = tito_db
50 .transaction(|tx| {
51 let user_model = user_model.clone();
52 async move { user_model.set(user).execute(&tx).await }
53 })
54 .await?;
55
56 println!("Created user: {:?}", saved_user);
57
58 let found_user = user_model.get(&user_id).execute(None).await?;
59 println!("Found user: {:?}", found_user);
60
61 let updated_user = User {
62 id: user_id.clone(),
63 name: "John Updated".to_string(),
64 email: "john_updated@example.com".to_string(),
65 };
66
67 tito_db
68 .transaction(|tx| {
69 let user_model = user_model.clone();
70 async move { user_model.set(updated_user).execute(&tx).await }
71 })
72 .await?;
73
74 println!("User updated");
75
76 tito_db
77 .transaction(|tx| {
78 let user_model = user_model.clone();
79 async move { user_model.remove(&user_id, &tx).await }
80 })
81 .await?;
82
83 println!("User deleted");
84
85 Ok(())
86}More examples
examples/blog.rs (line 150)
88async fn main() -> Result<(), TitoError> {
89 let tito_db = TiKV::connect(vec!["127.0.0.1:2379"]).await?;
90
91 let post_model = tito_db.clone().model::<Post>(TitoModelOptions::default());
92 let tag_model = tito_db.clone().model::<Tag>(TitoModelOptions::default());
93
94 let tech_tag = tito_db
95 .transaction(|tx| {
96 let tag_model = tag_model.clone();
97 let tag = Tag {
98 id: DBUuid::new_v4().to_string(),
99 name: "Technology".to_string(),
100 description: "All about tech".to_string(),
101 };
102 let tag_clone = tag.clone();
103 async move {
104 tag_model.set(tag_clone).execute(&tx).await?;
105 Ok::<_, TitoError>(tag)
106 }
107 })
108 .await?;
109
110 let rust_tag = tito_db
111 .transaction(|tx| {
112 let tag_model = tag_model.clone();
113 let tag = Tag {
114 id: DBUuid::new_v4().to_string(),
115 name: "Rust".to_string(),
116 description: "Rust programming".to_string(),
117 };
118 let tag_clone = tag.clone();
119 async move {
120 tag_model.set(tag_clone).execute(&tx).await?;
121 Ok::<_, TitoError>(tag)
122 }
123 })
124 .await?;
125
126 println!("Created tags: {}, {}", tech_tag.name, rust_tag.name);
127
128 let post = tito_db
129 .transaction(|tx| {
130 let post_model = post_model.clone();
131 let post = Post {
132 id: DBUuid::new_v4().to_string(),
133 title: "Using Rust with TiKV".to_string(),
134 content: "Examples of using Rust with TiKV...".to_string(),
135 author: "Alice".to_string(),
136 tag_ids: vec![tech_tag.id.clone(), rust_tag.id.clone()],
137 tags: Vec::new(),
138 };
139 let post_clone = post.clone();
140 async move {
141 post_model.set(post_clone).execute(&tx).await?;
142 Ok::<_, TitoError>(post)
143 }
144 })
145 .await?;
146
147 println!("Created post: {}", post.title);
148
149 let post_with_tags = post_model
150 .get(&post.id)
151 .relationship("tags")
152 .execute(None)
153 .await?;
154
155 println!("Post: {}", post_with_tags.title);
156 println!("Tags:");
157 for tag in &post_with_tags.tags {
158 println!("- {}", tag.name);
159 }
160
161 let mut query = post_model.query_by_index("post-by-author");
162 let results = query
163 .value("Alice".to_string())
164 .relationship("tags")
165 .execute(None)
166 .await?;
167
168 println!("\nAlice's posts:");
169 for p in &results.items {
170 println!(
171 "- {} (tags: {})",
172 p.title,
173 p.tags
174 .iter()
175 .map(|t| t.name.clone())
176 .collect::<Vec<_>>()
177 .join(", ")
178 );
179 }
180
181 Ok(())
182}pub async fn scan(
&self,
payload: TitoScanPayload,
tx: &E::Transaction,
) -> Result<(Vec<(String, Value)>, bool), TitoError>where
T: DeserializeOwned,
pub async fn get_many_raw(
&self,
ids: Vec<String>,
rels: Vec<String>,
tx: &E::Transaction,
) -> Result<Vec<(String, Value)>, TitoError>where
T: DeserializeOwned,
pub fn get_many(&self, ids: Vec<String>) -> GetManyBuilder<'_, E, T>
pub async fn scan_reverse(
&self,
payload: TitoScanPayload,
tx: &E::Transaction,
) -> Result<(Vec<(String, Value)>, bool), TitoError>where
T: DeserializeOwned,
pub fn get_last_id(&self, key: String) -> Option<String>
pub async fn batch_get( &self, keys: Vec<String>, tx: &E::Transaction, ) -> Result<Vec<(String, Value)>, TitoError>
pub async fn remove_by_index(
&self,
index: &str,
value: &str,
batch_size: u32,
tx: &E::Transaction,
) -> Result<Vec<String>, TitoError>where
T: DeserializeOwned,
Sourcepub async fn remove(
&self,
raw_id: &str,
tx: &E::Transaction,
) -> Result<bool, TitoError>
pub async fn remove( &self, raw_id: &str, tx: &E::Transaction, ) -> Result<bool, TitoError>
Examples found in repository?
examples/crud.rs (line 79)
38async fn main() -> Result<(), TitoError> {
39 let tito_db = TiKV::connect(vec!["127.0.0.1:2379"]).await?;
40 let user_model = tito_db.clone().model::<User>(TitoModelOptions::default());
41
42 let user_id = DBUuid::new_v4().to_string();
43 let user = User {
44 id: user_id.clone(),
45 name: "John Doe".to_string(),
46 email: "john@example.com".to_string(),
47 };
48
49 let saved_user = tito_db
50 .transaction(|tx| {
51 let user_model = user_model.clone();
52 async move { user_model.set(user).execute(&tx).await }
53 })
54 .await?;
55
56 println!("Created user: {:?}", saved_user);
57
58 let found_user = user_model.get(&user_id).execute(None).await?;
59 println!("Found user: {:?}", found_user);
60
61 let updated_user = User {
62 id: user_id.clone(),
63 name: "John Updated".to_string(),
64 email: "john_updated@example.com".to_string(),
65 };
66
67 tito_db
68 .transaction(|tx| {
69 let user_model = user_model.clone();
70 async move { user_model.set(updated_user).execute(&tx).await }
71 })
72 .await?;
73
74 println!("User updated");
75
76 tito_db
77 .transaction(|tx| {
78 let user_model = user_model.clone();
79 async move { user_model.remove(&user_id, &tx).await }
80 })
81 .await?;
82
83 println!("User deleted");
84
85 Ok(())
86}pub async fn find(
&self,
payload: TitoFindPayload,
) -> Result<TitoPaginated<T>, TitoError>where
T: DeserializeOwned,
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(
&self,
payload: TitoFindByIndexPayload,
tx: Option<&E::Transaction>,
) -> Result<TitoPaginated<T>, TitoError>where
T: DeserializeOwned,
pub async fn find_by_index_reverse(
&self,
payload: TitoFindByIndexPayload,
tx: Option<&E::Transaction>,
) -> Result<TitoPaginated<T>, TitoError>where
T: DeserializeOwned,
pub async fn find_one_by_index(
&self,
payload: TitoFindOneByIndexPayload,
tx: Option<&E::Transaction>,
) -> Result<T, TitoError>where
T: DeserializeOwned,
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 stitch_relationship( &self, item: &mut Value, rel_map: &HashMap<String, Value>, config: &TitoRelationshipConfig, )
pub fn get_relationship_data( &self, items: &Vec<(String, Value)>, rels_config: &[TitoRelationshipConfig], rels: &Vec<String>, ) -> Vec<(TitoRelationshipConfig, String)>
pub async fn fetch_and_stitch_relationships( &self, items: Vec<(String, Value)>, rels: Vec<String>, tx: &E::Transaction, ) -> Result<Vec<(String, Value)>, TitoError>
pub fn extract_relationship(&self, input: &str) -> Option<String>
Trait Implementations§
Auto Trait Implementations§
impl<E, T> Freeze for TitoModel<E, T>where
E: Freeze,
impl<E, T> RefUnwindSafe for TitoModel<E, T>where
E: RefUnwindSafe,
T: 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> UnsafeUnpin for TitoModel<E, T>where
E: UnsafeUnpin,
impl<E, T> UnwindSafe for TitoModel<E, T>where
E: UnwindSafe,
T: 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