tideorm 0.9.4

A developer-friendly ORM for Rust with clean, expressive syntax
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

use crate::columns::IntoColumnName;
use crate::error::{Error, Result};
use crate::internal::InternalModel;
use crate::model::Model;
use crate::query::{Order, QueryBuilder};

fn apply_primary_key_filter<M: Model>(
    mut query: QueryBuilder<M>,
    primary_key: &M::PrimaryKey,
) -> Result<QueryBuilder<M>> {
    let values = match serde_json::to_value(primary_key)
        .map_err(|e| Error::conversion(format!("Failed to serialize primary key: {}", e)))?
    {
        serde_json::Value::Array(values) => values,
        value => vec![value],
    };

    let columns = M::primary_key_names();
    if values.len() != columns.len() {
        return Err(Error::invalid_query(format!(
            "Primary key value for {} did not match declared key columns",
            M::table_name()
        )));
    }

    for (column, value) in columns.iter().zip(values.into_iter()) {
        query = query.where_eq(*column, value);
    }

    Ok(query)
}

#[derive(Debug, Clone, Default)]
pub struct RelationConstraints {
    pub conditions: Vec<(String, serde_json::Value)>,
    pub order_by: Option<(String, Order)>,
    pub limit: Option<u64>,
    pub offset: Option<u64>,
    pub with_trashed: bool,
    pub only_trashed: bool,
}

impl RelationConstraints {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn where_eq(
        mut self,
        column: impl IntoColumnName,
        value: impl Into<serde_json::Value>,
    ) -> Self {
        self.conditions
            .push((column.column_name().to_string(), value.into()));
        self
    }

    pub fn order_by(mut self, column: impl IntoColumnName, order: Order) -> Self {
        self.order_by = Some((column.column_name().to_string(), order));
        self
    }

    pub fn limit(mut self, n: u64) -> Self {
        self.limit = Some(n);
        self
    }

    pub fn offset(mut self, n: u64) -> Self {
        self.offset = Some(n);
        self
    }

    pub fn with_trashed(mut self) -> Self {
        self.with_trashed = true;
        self
    }

    pub fn only_trashed(mut self) -> Self {
        self.only_trashed = true;
        self
    }

    pub fn apply<M: Model>(self, mut query: QueryBuilder<M>) -> QueryBuilder<M> {
        for (column, value) in self.conditions {
            query = query.where_eq(&column, value);
        }

        if let Some((column, order)) = self.order_by {
            query = query.order_by(&column, order);
        }

        if let Some(limit) = self.limit {
            query = query.limit(limit);
        }

        if let Some(offset) = self.offset {
            query = query.offset(offset);
        }

        if self.with_trashed {
            query = query.with_trashed();
        }

        if self.only_trashed {
            query = query.only_trashed();
        }

        query
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WithRelations<M> {
    #[serde(flatten)]
    pub model: M,
    #[serde(skip_serializing_if = "HashMap::is_empty")]
    pub relations: HashMap<String, serde_json::Value>,
}

impl<M: Model> WithRelations<M> {
    pub fn new(model: M) -> Self {
        Self {
            model,
            relations: HashMap::new(),
        }
    }

    pub fn with_relation(mut self, name: &str, data: serde_json::Value) -> Self {
        self.relations.insert(name.to_string(), data);
        self
    }

    pub fn get_relation<R: for<'de> Deserialize<'de>>(&self, name: &str) -> Option<R> {
        self.relations
            .get(name)
            .and_then(|v| serde_json::from_value(v.clone()).ok())
    }

    pub fn has_relation(&self, name: &str) -> bool {
        self.relations.contains_key(name)
    }

    pub fn into_inner(self) -> M {
        self.model
    }
}

impl<M> std::ops::Deref for WithRelations<M> {
    type Target = M;

    fn deref(&self) -> &Self::Target {
        &self.model
    }
}

impl<M> std::ops::DerefMut for WithRelations<M> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.model
    }
}

#[derive(Debug, Clone)]
pub struct RelationPath {
    pub full_path: String,
    pub segments: Vec<String>,
}

impl RelationPath {
    pub fn parse(path: &str) -> Self {
        let segments: Vec<String> = path.split('.').map(|s| s.to_string()).collect();
        Self {
            full_path: path.to_string(),
            segments,
        }
    }

    pub fn root(&self) -> &str {
        self.segments.first().map(|s| s.as_str()).unwrap_or("")
    }

    pub fn nested(&self) -> Option<RelationPath> {
        if self.segments.len() > 1 {
            Some(RelationPath {
                full_path: self.segments[1..].join("."),
                segments: self.segments[1..].to_vec(),
            })
        } else {
            None
        }
    }

    pub fn is_nested(&self) -> bool {
        self.segments.len() > 1
    }

    pub fn depth(&self) -> usize {
        self.segments.len()
    }
}

#[derive(Debug, Clone, Default)]
pub struct RelationTree {
    children: HashMap<String, RelationTree>,
}

impl RelationTree {
    pub fn new() -> Self {
        Self {
            children: HashMap::new(),
        }
    }

    pub fn add_path(&mut self, path: &RelationPath) {
        if path.segments.is_empty() {
            return;
        }

        let root = path.root().to_string();
        let child = self.children.entry(root).or_default();

        if let Some(nested) = path.nested() {
            child.add_path(&nested);
        }
    }

    pub fn roots(&self) -> Vec<String> {
        self.children.keys().cloned().collect()
    }

    pub fn get_nested(&self, name: &str) -> Option<&RelationTree> {
        self.children.get(name)
    }

    pub fn is_empty(&self) -> bool {
        self.children.is_empty()
    }

    pub fn has_nested(&self, name: &str) -> bool {
        self.children
            .get(name)
            .map(|t| !t.is_empty())
            .unwrap_or(false)
    }
}

pub struct EagerQueryBuilder<M: Model> {
    query: QueryBuilder<M>,
    relation_tree: RelationTree,
}

#[async_trait]
#[doc(hidden)]
pub trait EagerLoadModel: Model + InternalModel {
    async fn __eager_load(
        models: &mut [WithRelations<Self>],
        relation_tree: &RelationTree,
    ) -> Result<()>
    where
        Self: Sized;
}

impl<M: Model> EagerQueryBuilder<M> {
    pub fn new() -> Self {
        Self {
            query: QueryBuilder::new(),
            relation_tree: RelationTree::new(),
        }
    }

    pub fn with(mut self, relation: &str) -> Self {
        let path = RelationPath::parse(relation);
        self.relation_tree.add_path(&path);
        self
    }

    pub fn with_many(mut self, relations: &[&str]) -> Self {
        for relation in relations {
            self = self.with(relation);
        }
        self
    }

    pub fn where_eq<V: Into<serde_json::Value>>(
        mut self,
        column: impl IntoColumnName,
        value: V,
    ) -> Self {
        self.query = self.query.where_eq(column, value);
        self
    }

    pub fn where_in<V: Into<serde_json::Value>>(
        mut self,
        column: impl IntoColumnName,
        values: Vec<V>,
    ) -> Self {
        self.query = self.query.where_in(column, values);
        self
    }

    pub fn where_raw(mut self, sql: &str) -> Self {
        self.query = self.query.where_raw(sql);
        self
    }

    pub fn order_by(mut self, column: impl IntoColumnName, order: Order) -> Self {
        self.query = self.query.order_by(column, order);
        self
    }

    pub fn limit(mut self, n: u64) -> Self {
        self.query = self.query.limit(n);
        self
    }

    pub fn offset(mut self, n: u64) -> Self {
        self.query = self.query.offset(n);
        self
    }

    pub fn get_relation_tree(&self) -> &RelationTree {
        &self.relation_tree
    }

    pub async fn get(self) -> Result<Vec<WithRelations<M>>>
    where
        M: EagerLoadModel,
    {
        let models = self.query.get().await?;
        let mut results: Vec<WithRelations<M>> =
            models.into_iter().map(WithRelations::new).collect();
        M::__eager_load(&mut results, &self.relation_tree).await?;
        Ok(results)
    }

    pub async fn first(mut self) -> Result<Option<WithRelations<M>>>
    where
        M: EagerLoadModel,
    {
        self.query = self.query.limit(1);
        let results = self.get().await?;
        Ok(results.into_iter().next())
    }

    pub async fn find(mut self, id: M::PrimaryKey) -> Result<Option<WithRelations<M>>>
    where
        M: EagerLoadModel,
    {
        self.query = apply_primary_key_filter(self.query, &id)?.limit(1);
        self.first().await
    }
}

impl<M: Model> Default for EagerQueryBuilder<M> {
    fn default() -> Self {
        Self::new()
    }
}

pub struct RelationLoader<M> {
    pub name: String,
    #[allow(clippy::type_complexity)]
    pub loader: Box<
        dyn Fn(
                &[M],
            ) -> std::pin::Pin<
                Box<
                    dyn std::future::Future<Output = Result<HashMap<String, serde_json::Value>>>
                        + Send,
                >,
            > + Send
            + Sync,
    >,
}

pub trait EagerLoadExt: Model {
    fn eager() -> EagerQueryBuilder<Self>
    where
        Self: Sized,
    {
        EagerQueryBuilder::new()
    }

    fn with_relation(relation_name: &str) -> EagerQueryBuilder<Self>
    where
        Self: Sized,
    {
        EagerQueryBuilder::new().with(relation_name)
    }

    fn with_relations(relations: &[&str]) -> EagerQueryBuilder<Self>
    where
        Self: Sized,
    {
        EagerQueryBuilder::new().with_many(relations)
    }
}

impl<T: Model> EagerLoadExt for T {}

#[async_trait]
pub trait RelationExt: Model {
    fn get_field_value(&self, field: &str) -> Result<serde_json::Value> {
        let json = serde_json::to_value(self)
            .map_err(|e| Error::query(format!("Failed to serialize model: {}", e)))?;

        json.get(field)
            .cloned()
            .ok_or_else(|| Error::query(format!("Field '{}' not found on model", field)))
    }
}

impl<T: Model> RelationExt for T {}