elif-orm 0.7.1

Production-ready ORM with migrations, database services, connection pooling, and query builder
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
//! Relationship Type Inference - Utilities for inferring relationship types and configurations

use serde::de::DeserializeOwned;
use std::collections::HashMap;
use std::marker::PhantomData;

use super::metadata::{
    ForeignKeyConfig, PivotConfig, PolymorphicConfig, RelationshipMetadata, RelationshipType,
};
use crate::error::ModelResult;
use crate::model::Model;

/// Trait for models that can have their relationships inferred
pub trait InferableModel: Model {
    /// Get relationship inference hints for this model
    fn relationship_hints() -> Vec<RelationshipHint> {
        Vec::new()
    }

    /// Get foreign key naming convention for this model
    fn foreign_key_convention() -> ForeignKeyConvention {
        ForeignKeyConvention::Underscore
    }

    /// Get table naming convention for this model
    fn table_naming_convention() -> TableNamingConvention {
        TableNamingConvention::Plural
    }
}

/// Hint for relationship inference
#[derive(Debug, Clone)]
pub struct RelationshipHint {
    /// The field name in the model
    pub field_name: String,

    /// The expected relationship type
    pub relationship_type: RelationshipType,

    /// The related model type name
    pub related_model: String,

    /// Custom foreign key if different from convention
    pub custom_foreign_key: Option<String>,

    /// Whether this should be eagerly loaded by default
    pub eager_load: bool,
}

/// Foreign key naming conventions
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ForeignKeyConvention {
    /// model_id (e.g., user_id)
    Underscore,
    /// modelId (camelCase)
    CamelCase,
    /// modelID (PascalCase with ID suffix)
    PascalCase,
    /// Custom pattern with {model} placeholder
    Custom(&'static str),
}

/// Table naming conventions
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum TableNamingConvention {
    /// Plural form (users, posts)
    Plural,
    /// Singular form (user, post)
    Singular,
    /// Custom pattern
    Custom(&'static str),
}

/// Relationship type inference engine
pub struct RelationshipInferenceEngine<Parent>
where
    Parent: InferableModel + DeserializeOwned + Send + Sync,
{
    parent_model: PhantomData<Parent>,

    /// Cache of inferred relationships
    inference_cache: HashMap<String, RelationshipMetadata>,
}

impl<Parent> RelationshipInferenceEngine<Parent>
where
    Parent: InferableModel + DeserializeOwned + Send + Sync,
{
    /// Create a new inference engine
    pub fn new() -> Self {
        Self {
            parent_model: PhantomData,
            inference_cache: HashMap::new(),
        }
    }

    /// Infer relationship metadata for a given field name and related model
    pub fn infer_relationship<Related>(
        &mut self,
        field_name: &str,
        relationship_type: RelationshipType,
    ) -> ModelResult<RelationshipMetadata>
    where
        Related: InferableModel + DeserializeOwned + Send + Sync,
    {
        let cache_key = format!("{}::{}", field_name, std::any::type_name::<Related>());

        // Check cache first
        if let Some(cached) = self.inference_cache.get(&cache_key) {
            return Ok(cached.clone());
        }

        let metadata =
            self.infer_relationship_metadata::<Related>(field_name, relationship_type)?;

        // Cache the result
        self.inference_cache.insert(cache_key, metadata.clone());

        Ok(metadata)
    }

    /// Infer all relationships for the parent model using hints
    pub fn infer_all_relationships(&mut self) -> ModelResult<Vec<RelationshipMetadata>> {
        let hints = Parent::relationship_hints();
        let mut relationships = Vec::new();

        for hint in hints {
            let metadata = self.infer_from_hint(&hint)?;
            relationships.push(metadata);
        }

        Ok(relationships)
    }

    /// Infer relationship metadata from model structure and naming conventions
    fn infer_relationship_metadata<Related>(
        &self,
        field_name: &str,
        relationship_type: RelationshipType,
    ) -> ModelResult<RelationshipMetadata>
    where
        Related: InferableModel + DeserializeOwned + Send + Sync,
    {
        let parent_table = Parent::table_name();
        let related_table = Related::table_name();
        let related_model_name = std::any::type_name::<Related>()
            .split("::")
            .last()
            .unwrap_or(std::any::type_name::<Related>());

        let foreign_key_config = match relationship_type {
            RelationshipType::HasOne | RelationshipType::HasMany => {
                // Related table has foreign key pointing to parent
                let foreign_key = self.infer_foreign_key_name(Parent::table_name())?;
                ForeignKeyConfig::simple(foreign_key, related_table.to_string())
            }
            RelationshipType::BelongsTo => {
                // Parent table has foreign key pointing to related
                let foreign_key = self.infer_foreign_key_name(Related::table_name())?;
                ForeignKeyConfig::simple(foreign_key, parent_table.to_string())
            }
            RelationshipType::ManyToMany => {
                // Pivot table with both foreign keys
                let pivot_table = self.infer_pivot_table_name(parent_table, related_table);
                let local_key = self.infer_foreign_key_name(parent_table)?;
                let foreign_key = self.infer_foreign_key_name(related_table)?;

                return Ok(RelationshipMetadata::new(
                    relationship_type,
                    field_name.to_string(),
                    related_table.to_string(),
                    related_model_name.to_string(),
                    ForeignKeyConfig::simple(local_key.clone(), pivot_table.clone()),
                )
                .with_pivot(PivotConfig::new(pivot_table, local_key, foreign_key)));
            }
            RelationshipType::MorphOne
            | RelationshipType::MorphMany
            | RelationshipType::MorphTo => {
                // Polymorphic relationships
                let (type_column, id_column) = self.infer_polymorphic_columns(field_name);

                return Ok(RelationshipMetadata::new(
                    relationship_type,
                    field_name.to_string(),
                    related_table.to_string(),
                    related_model_name.to_string(),
                    ForeignKeyConfig::simple(id_column.clone(), related_table.to_string()),
                )
                .with_polymorphic(PolymorphicConfig::new(
                    field_name.to_string(),
                    type_column,
                    id_column,
                )));
            }
        };

        Ok(RelationshipMetadata::new(
            relationship_type,
            field_name.to_string(),
            related_table.to_string(),
            related_model_name.to_string(),
            foreign_key_config,
        ))
    }

    /// Infer relationship metadata from a hint
    fn infer_from_hint(&self, hint: &RelationshipHint) -> ModelResult<RelationshipMetadata> {
        let foreign_key = hint.custom_foreign_key.clone().unwrap_or_else(|| {
            match self.infer_foreign_key_name(&hint.related_model.to_lowercase()) {
                Ok(fk) => fk,
                Err(_) => format!("{}_id", hint.related_model.to_lowercase()),
            }
        });

        let related_table = self.infer_table_name(&hint.related_model);

        let mut metadata = RelationshipMetadata::new(
            hint.relationship_type,
            hint.field_name.clone(),
            related_table,
            hint.related_model.clone(),
            ForeignKeyConfig::simple(foreign_key, hint.related_model.to_lowercase()),
        );

        metadata.eager_load = hint.eager_load;

        Ok(metadata)
    }

    /// Infer foreign key name based on convention
    pub fn infer_foreign_key_name(&self, table_or_model: &str) -> ModelResult<String> {
        let convention = Parent::foreign_key_convention();

        match convention {
            ForeignKeyConvention::Underscore => {
                let singular = self.singularize_table_name(table_or_model);
                Ok(format!("{}_id", singular))
            }
            ForeignKeyConvention::CamelCase => {
                let singular = self.singularize_table_name(table_or_model);
                Ok(format!("{}Id", self.to_camel_case(&singular)))
            }
            ForeignKeyConvention::PascalCase => {
                let singular = self.singularize_table_name(table_or_model);
                Ok(format!("{}ID", self.to_pascal_case(&singular)))
            }
            ForeignKeyConvention::Custom(pattern) => {
                let singular = self.singularize_table_name(table_or_model);
                Ok(pattern.replace("{model}", &singular))
            }
        }
    }

    /// Infer pivot table name for many-to-many relationships
    fn infer_pivot_table_name(&self, table1: &str, table2: &str) -> String {
        let mut tables = [table1, table2];
        tables.sort();
        tables.join("_")
    }

    /// Infer polymorphic column names
    fn infer_polymorphic_columns(&self, field_name: &str) -> (String, String) {
        // Standard Laravel-style naming: commentable_type, commentable_id
        let base = if field_name.ends_with("able") {
            field_name.to_string()
        } else {
            format!("{}_able", field_name)
        };

        (format!("{}_type", base), format!("{}_id", base))
    }

    /// Infer table name from model name
    pub fn infer_table_name(&self, model_name: &str) -> String {
        let convention = Parent::table_naming_convention();
        let base_name = model_name.to_lowercase();

        match convention {
            TableNamingConvention::Plural => self.pluralize_name(&base_name),
            TableNamingConvention::Singular => base_name,
            TableNamingConvention::Custom(pattern) => pattern.replace("{model}", &base_name),
        }
    }

    /// Simple pluralization (English-centric)
    pub fn pluralize_name(&self, name: &str) -> String {
        if name.ends_with('y')
            && !name.ends_with("ay")
            && !name.ends_with("ey")
            && !name.ends_with("iy")
            && !name.ends_with("oy")
            && !name.ends_with("uy")
        {
            format!("{}ies", &name[..name.len() - 1])
        } else if name.ends_with('s')
            || name.ends_with("sh")
            || name.ends_with("ch")
            || name.ends_with('x')
            || name.ends_with('z')
        {
            format!("{}es", name)
        } else {
            format!("{}s", name)
        }
    }

    /// Simple singularization (English-centric)  
    pub fn singularize_table_name(&self, name: &str) -> String {
        if name.ends_with("ies") {
            format!("{}y", &name[..name.len() - 3])
        } else if name.ends_with("ses")
            || name.ends_with("ches")
            || name.ends_with("shes")
            || name.ends_with("xes")
            || name.ends_with("zes")
        {
            name[..name.len() - 2].to_string()
        } else if name.ends_with('s') && name.len() > 1 {
            name[..name.len() - 1].to_string()
        } else {
            name.to_string()
        }
    }

    /// Convert to camelCase
    pub fn to_camel_case(&self, s: &str) -> String {
        let parts: Vec<&str> = s.split('_').collect();
        if parts.is_empty() {
            return s.to_string();
        }

        let mut result = parts[0].to_lowercase();
        for part in &parts[1..] {
            if !part.is_empty() {
                let mut chars = part.chars();
                if let Some(first) = chars.next() {
                    result.push(first.to_uppercase().next().unwrap());
                    result.extend(chars.flat_map(|c| c.to_lowercase()));
                }
            }
        }

        result
    }

    /// Convert to PascalCase
    pub fn to_pascal_case(&self, s: &str) -> String {
        let camel = self.to_camel_case(s);
        let mut chars = camel.chars();
        if let Some(first) = chars.next() {
            first.to_uppercase().collect::<String>() + &chars.collect::<String>()
        } else {
            camel
        }
    }
}

impl<Parent> Default for RelationshipInferenceEngine<Parent>
where
    Parent: InferableModel + DeserializeOwned + Send + Sync,
{
    fn default() -> Self {
        Self::new()
    }
}

/// Utility for inferring relationship types from field types
pub struct TypeInferenceHelper;

impl TypeInferenceHelper {
    /// Infer relationship type from Rust type information
    pub fn infer_from_type_name(type_name: &str) -> Option<RelationshipType> {
        if type_name.contains("Option<") {
            // Single optional relationship
            if type_name.contains("Vec<") {
                None // Shouldn't have Option<Vec<T>>
            } else {
                Some(RelationshipType::HasOne) // Default to HasOne for Option<T>
            }
        } else if type_name.contains("Vec<") {
            Some(RelationshipType::HasMany) // Collection relationship
        } else if type_name.contains("MorphOne<") {
            Some(RelationshipType::MorphOne)
        } else if type_name.contains("MorphMany<") {
            Some(RelationshipType::MorphMany)
        } else if type_name.contains("MorphTo<") {
            Some(RelationshipType::MorphTo)
        } else {
            None // Can't infer from basic types
        }
    }

    /// Check if a field name suggests a specific relationship type
    pub fn infer_from_field_name(field_name: &str) -> Option<RelationshipType> {
        if field_name.ends_with("_id") {
            Some(RelationshipType::BelongsTo)
        } else if field_name.ends_with("_ids") {
            Some(RelationshipType::ManyToMany)
        } else if field_name.ends_with("able") || field_name.contains("morph") {
            Some(RelationshipType::MorphTo) // Default for polymorphic
        } else {
            None
        }
    }

    /// Suggest relationship type based on multiple hints
    pub fn suggest_relationship_type(
        field_name: &str,
        type_name: &str,
        is_collection: bool,
        is_optional: bool,
    ) -> RelationshipType {
        // Try field name inference first
        if let Some(rt) = Self::infer_from_field_name(field_name) {
            return rt;
        }

        // Try type name inference
        if let Some(rt) = Self::infer_from_type_name(type_name) {
            return rt;
        }

        // Fall back to collection/optional hints
        match (is_collection, is_optional) {
            (true, _) => RelationshipType::HasMany,
            (false, true) => RelationshipType::HasOne,
            (false, false) => RelationshipType::BelongsTo, // Required single relationship
        }
    }
}

/// Macro helper for generating relationship hints
#[macro_export]
macro_rules! relationship_hints {
    ($(($field:expr, $type:expr, $related:expr, $eager:expr)),* $(,)?) => {
        vec![
            $(
                $crate::relationships::inference::RelationshipHint {
                    field_name: $field.to_string(),
                    relationship_type: $type,
                    related_model: $related.to_string(),
                    custom_foreign_key: None,
                    eager_load: $eager,
                }
            ),*
        ]
    };

    ($(($field:expr, $type:expr, $related:expr, $eager:expr, $fk:expr)),* $(,)?) => {
        vec![
            $(
                $crate::relationships::inference::RelationshipHint {
                    field_name: $field.to_string(),
                    relationship_type: $type,
                    related_model: $related.to_string(),
                    custom_foreign_key: Some($fk.to_string()),
                    eager_load: $eager,
                }
            ),*
        ]
    };
}

#[cfg(test)]
mod tests {
    use super::super::metadata::RelationshipType;
    use super::*;

    #[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
    struct TestUser {
        id: Option<i64>,
        name: String,
        email: String,
    }

    impl Model for TestUser {
        type PrimaryKey = i64;

        fn table_name() -> &'static str {
            "users"
        }

        fn primary_key(&self) -> Option<Self::PrimaryKey> {
            self.id
        }

        fn set_primary_key(&mut self, key: Self::PrimaryKey) {
            self.id = Some(key);
        }

        fn to_fields(&self) -> std::collections::HashMap<String, serde_json::Value> {
            let mut fields = std::collections::HashMap::new();
            fields.insert("id".to_string(), serde_json::json!(self.id));
            fields.insert(
                "name".to_string(),
                serde_json::Value::String(self.name.clone()),
            );
            fields.insert(
                "email".to_string(),
                serde_json::Value::String(self.email.clone()),
            );
            fields
        }

        fn from_row(row: &sqlx::postgres::PgRow) -> crate::error::ModelResult<Self> {
            use sqlx::Row;
            Ok(Self {
                id: row.try_get("id").ok(),
                name: row.try_get("name").unwrap_or_default(),
                email: row.try_get("email").unwrap_or_default(),
            })
        }
    }

    impl InferableModel for TestUser {
        fn relationship_hints() -> Vec<RelationshipHint> {
            relationship_hints![
                ("posts", RelationshipType::HasMany, "Post", false),
                ("profile", RelationshipType::HasOne, "Profile", true),
                ("roles", RelationshipType::ManyToMany, "Role", false)
            ]
        }
    }

    #[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
    struct TestPost {
        id: Option<i64>,
        title: String,
        user_id: Option<i64>,
    }

    impl Model for TestPost {
        type PrimaryKey = i64;

        fn table_name() -> &'static str {
            "posts"
        }

        fn primary_key(&self) -> Option<Self::PrimaryKey> {
            self.id
        }

        fn set_primary_key(&mut self, key: Self::PrimaryKey) {
            self.id = Some(key);
        }

        fn to_fields(&self) -> std::collections::HashMap<String, serde_json::Value> {
            let mut fields = std::collections::HashMap::new();
            fields.insert("id".to_string(), serde_json::json!(self.id));
            fields.insert(
                "title".to_string(),
                serde_json::Value::String(self.title.clone()),
            );
            fields.insert("user_id".to_string(), serde_json::json!(self.user_id));
            fields
        }

        fn from_row(row: &sqlx::postgres::PgRow) -> crate::error::ModelResult<Self> {
            use sqlx::Row;
            Ok(Self {
                id: row.try_get("id").ok(),
                title: row.try_get("title").unwrap_or_default(),
                user_id: row.try_get("user_id").ok(),
            })
        }
    }

    impl InferableModel for TestPost {
        fn relationship_hints() -> Vec<RelationshipHint> {
            relationship_hints![("user", RelationshipType::BelongsTo, "User", true)]
        }
    }

    #[test]
    fn test_inference_engine_creation() {
        let mut engine = RelationshipInferenceEngine::<TestUser>::new();

        // Test that we can create and use the engine
        let relationships = engine.infer_all_relationships().unwrap();

        assert_eq!(relationships.len(), 3);
        assert_eq!(relationships[0].name, "posts");
        assert_eq!(
            relationships[0].relationship_type,
            RelationshipType::HasMany
        );
        assert_eq!(relationships[1].name, "profile");
        assert_eq!(relationships[1].relationship_type, RelationshipType::HasOne);
        assert!(relationships[1].eager_load);
    }

    #[test]
    fn test_foreign_key_inference() {
        let engine = RelationshipInferenceEngine::<TestUser>::new();

        let fk = engine.infer_foreign_key_name("user").unwrap();
        assert_eq!(fk, "user_id");

        let fk = engine.infer_foreign_key_name("posts").unwrap();
        assert_eq!(fk, "post_id");
    }

    #[test]
    fn test_pluralization() {
        let engine = RelationshipInferenceEngine::<TestUser>::new();

        assert_eq!(engine.pluralize_name("user"), "users");
        assert_eq!(engine.pluralize_name("post"), "posts");
        assert_eq!(engine.pluralize_name("category"), "categories");
        assert_eq!(engine.pluralize_name("box"), "boxes");
    }

    #[test]
    fn test_singularization() {
        let engine = RelationshipInferenceEngine::<TestUser>::new();

        assert_eq!(engine.singularize_table_name("users"), "user");
        assert_eq!(engine.singularize_table_name("posts"), "post");
        assert_eq!(engine.singularize_table_name("categories"), "category");
        assert_eq!(engine.singularize_table_name("boxes"), "box");
    }

    #[test]
    fn test_case_conversion() {
        let engine = RelationshipInferenceEngine::<TestUser>::new();

        assert_eq!(engine.to_camel_case("user_id"), "userId");
        assert_eq!(engine.to_camel_case("user"), "user");
        assert_eq!(engine.to_pascal_case("user_id"), "UserId");
        assert_eq!(engine.to_pascal_case("user"), "User");
    }

    #[test]
    fn test_type_inference_helper() {
        assert_eq!(
            TypeInferenceHelper::infer_from_type_name("Option<Post>"),
            Some(RelationshipType::HasOne)
        );
        assert_eq!(
            TypeInferenceHelper::infer_from_type_name("Vec<Post>"),
            Some(RelationshipType::HasMany)
        );
        assert_eq!(
            TypeInferenceHelper::infer_from_field_name("user_id"),
            Some(RelationshipType::BelongsTo)
        );
        assert_eq!(
            TypeInferenceHelper::infer_from_field_name("role_ids"),
            Some(RelationshipType::ManyToMany)
        );
    }

    #[test]
    fn test_relationship_type_suggestion() {
        let rt = TypeInferenceHelper::suggest_relationship_type("posts", "Vec<Post>", true, false);
        assert_eq!(rt, RelationshipType::HasMany);

        let rt = TypeInferenceHelper::suggest_relationship_type("user_id", "i64", false, false);
        assert_eq!(rt, RelationshipType::BelongsTo);

        let rt = TypeInferenceHelper::suggest_relationship_type(
            "profile",
            "Option<Profile>",
            false,
            true,
        );
        assert_eq!(rt, RelationshipType::HasOne);
    }
}