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
//! Type-Safe Model Hydration - Converting database rows to typed relationship data

use serde::de::DeserializeOwned;
use sqlx::{postgres::PgRow, Column, Row};
use std::collections::HashMap;
use std::marker::PhantomData;

use super::containers::*;
use super::metadata::{RelationshipMetadata, RelationshipType};
use crate::error::{ModelError, ModelResult};
use crate::model::Model;

/// Trait for converting database rows to typed models
pub trait TypeSafeHydrator<T>: Send + Sync
where
    T: Model + DeserializeOwned + Send + Sync,
{
    /// Hydrate a single model from a database row
    fn hydrate_single(&self, row: &PgRow) -> ModelResult<T>;

    /// Hydrate multiple models from database rows
    fn hydrate_collection(&self, rows: &[PgRow]) -> ModelResult<Vec<T>>;

    /// Hydrate with specific column mapping
    fn hydrate_with_mapping(
        &self,
        row: &PgRow,
        column_mapping: &HashMap<String, String>,
    ) -> ModelResult<T>;
}

/// Generic hydrator that uses the Model trait's from_row method
#[derive(Debug)]
pub struct ModelHydrator<T> {
    _phantom: PhantomData<T>,
}

impl<T> ModelHydrator<T>
where
    T: Model + DeserializeOwned + Send + Sync,
{
    pub fn new() -> Self {
        Self {
            _phantom: PhantomData,
        }
    }
}

impl<T> Default for ModelHydrator<T>
where
    T: Model + DeserializeOwned + Send + Sync,
{
    fn default() -> Self {
        Self::new()
    }
}

impl<T> TypeSafeHydrator<T> for ModelHydrator<T>
where
    T: Model + DeserializeOwned + Send + Sync,
{
    fn hydrate_single(&self, row: &PgRow) -> ModelResult<T> {
        T::from_row(row)
    }

    fn hydrate_collection(&self, rows: &[PgRow]) -> ModelResult<Vec<T>> {
        rows.iter().map(|row| self.hydrate_single(row)).collect()
    }

    fn hydrate_with_mapping(
        &self,
        row: &PgRow,
        column_mapping: &HashMap<String, String>,
    ) -> ModelResult<T> {
        // For now, use the standard from_row method
        // In the future, we could implement column remapping here
        let _ = column_mapping; // Suppress unused warning
        self.hydrate_single(row)
    }
}

/// Advanced hydrator for complex relationship scenarios
#[derive(Debug)]
pub struct RelationshipHydrator<Parent, Related>
where
    Parent: Model + Send + Sync,
    Related: Model + DeserializeOwned + Send + Sync,
{
    /// The relationship metadata
    _metadata: RelationshipMetadata,

    /// Parent model hydrator
    parent_hydrator: ModelHydrator<Parent>,

    /// Related model hydrator
    related_hydrator: ModelHydrator<Related>,

    /// Column prefix for related models (for joins)
    related_prefix: Option<String>,
}

impl<Parent, Related> RelationshipHydrator<Parent, Related>
where
    Parent: Model + DeserializeOwned + Send + Sync,
    Related: Model + DeserializeOwned + Send + Sync,
{
    pub fn new(metadata: RelationshipMetadata) -> Self {
        Self {
            _metadata: metadata,
            parent_hydrator: ModelHydrator::new(),
            related_hydrator: ModelHydrator::new(),
            related_prefix: None,
        }
    }

    /// Set a column prefix for related models (useful for JOINs)
    pub fn with_related_prefix(mut self, prefix: String) -> Self {
        self.related_prefix = Some(prefix);
        self
    }

    /// Hydrate a parent model with its relationships
    pub fn hydrate_with_relationships(
        &self,
        parent_row: &PgRow,
        related_rows: &[PgRow],
    ) -> ModelResult<(Parent, Vec<Related>)> {
        let parent = self.parent_hydrator.hydrate_single(parent_row)?;
        let related = self.related_hydrator.hydrate_collection(related_rows)?;

        Ok((parent, related))
    }

    /// Hydrate from a joined query result
    pub fn hydrate_joined(&self, row: &PgRow) -> ModelResult<(Parent, Option<Related>)> {
        let parent = self.parent_hydrator.hydrate_single(row)?;

        // Try to hydrate the related model
        let related = if let Some(prefix) = &self.related_prefix {
            // Check if related columns are present and not null
            let has_related_data = row.columns().iter().any(|col| {
                col.name().starts_with(prefix)
                    && !matches!(row.try_get::<Option<String>, _>(col.name()), Ok(None))
            });

            if has_related_data {
                Some(self.related_hydrator.hydrate_single(row)?)
            } else {
                None
            }
        } else {
            // Try to hydrate without prefix
            match self.related_hydrator.hydrate_single(row) {
                Ok(related) => Some(related),
                Err(_) => None, // Ignore hydration errors for optional relationships
            }
        };

        Ok((parent, related))
    }

    /// Group related models by parent key for eager loading
    pub fn group_by_parent_key(
        &self,
        related_rows: &[PgRow],
        foreign_key_column: &str,
    ) -> ModelResult<HashMap<String, Vec<Related>>> {
        let mut grouped: HashMap<String, Vec<Related>> = HashMap::new();

        for row in related_rows {
            // Extract the foreign key value
            let foreign_key_value: String = row.try_get(foreign_key_column).map_err(|e| {
                ModelError::Database(format!(
                    "Failed to get foreign key '{}': {}",
                    foreign_key_column, e
                ))
            })?;

            // Hydrate the related model
            let related = self.related_hydrator.hydrate_single(row)?;

            // Add to the group
            grouped.entry(foreign_key_value).or_default().push(related);
        }

        Ok(grouped)
    }
}

/// Type-safe relationship loader that replaces the JSON-based system
pub struct TypeSafeRelationshipLoader<Parent, Related>
where
    Parent: Model + Send + Sync,
    Related: Model + DeserializeOwned + Send + Sync,
{
    /// The hydrator for this relationship
    hydrator: RelationshipHydrator<Parent, Related>,
}

impl<Parent, Related> TypeSafeRelationshipLoader<Parent, Related>
where
    Parent: Model + DeserializeOwned + Send + Sync,
    Related: Model + DeserializeOwned + Send + Sync,
{
    pub fn new(metadata: RelationshipMetadata) -> Self {
        Self {
            hydrator: RelationshipHydrator::new(metadata),
        }
    }

    /// Load and hydrate relationships for a collection of parent models
    pub fn hydrate_relationships(
        &self,
        parents: &mut [Parent],
        related_rows: &[PgRow],
        foreign_key_column: &str,
    ) -> ModelResult<()> {
        // Group related models by parent key
        let grouped_related = self
            .hydrator
            .group_by_parent_key(related_rows, foreign_key_column)?;

        // For each parent, find and attach its related models
        for parent in parents {
            if let Some(parent_key) = parent.primary_key() {
                let parent_key_str = parent_key.to_string();

                if let Some(related_models) = grouped_related.get(&parent_key_str) {
                    // Here we would attach the relationships to the parent model
                    // This requires the parent model to have relationship fields
                    // For now, we'll just validate that hydration works
                    let _ = related_models; // Suppress unused warning
                }
            }
        }

        Ok(())
    }
}

/// Specialized hydrators for different relationship types
pub mod specialized_hydrators {
    use super::*;

    /// Hydrator for HasOne relationships
    pub struct HasOneHydrator<Parent, Related>
    where
        Parent: Model + DeserializeOwned + Send + Sync + Clone,
        Related: Model + DeserializeOwned + Send + Sync + Clone,
    {
        _loader: TypeSafeRelationshipLoader<Parent, Related>,
    }

    impl<Parent, Related> HasOneHydrator<Parent, Related>
    where
        Parent: Model + DeserializeOwned + Send + Sync + Clone,
        Related: Model + DeserializeOwned + Send + Sync + Clone,
    {
        pub fn new(metadata: RelationshipMetadata) -> Self {
            Self {
                _loader: TypeSafeRelationshipLoader::new(metadata),
            }
        }

        /// Hydrate HasOne relationship into a TypeSafeRelationship container
        pub fn hydrate_has_one(
            &self,
            parent: &Parent,
            related_rows: &[PgRow],
            foreign_key_column: &str,
        ) -> ModelResult<HasOne<Related>> {
            let parent_key = parent
                .primary_key()
                .ok_or_else(|| {
                    ModelError::Configuration("Parent model has no primary key".to_string())
                })?
                .to_string();

            // Find the related model for this parent
            let related_model = related_rows
                .iter()
                .find(|row| match row.try_get::<String, _>(foreign_key_column) {
                    Ok(fk) => fk == parent_key,
                    Err(_) => false,
                })
                .map(|row| ModelHydrator::<Related>::new().hydrate_single(row))
                .transpose()?;

            let mut relationship = HasOne::new(RelationshipMetadata::new(
                RelationshipType::HasOne,
                "related".to_string(),
                Related::table_name().to_string(),
                std::any::type_name::<Related>().to_string(),
                super::super::metadata::ForeignKeyConfig::simple(
                    foreign_key_column.to_string(),
                    Related::table_name().to_string(),
                ),
            ));

            relationship.set_loaded(related_model);
            Ok(relationship)
        }
    }

    /// Hydrator for HasMany relationships
    pub struct HasManyHydrator<Parent, Related>
    where
        Parent: Model + DeserializeOwned + Send + Sync + Clone,
        Related: Model + DeserializeOwned + Send + Sync + Clone,
    {
        _loader: TypeSafeRelationshipLoader<Parent, Related>,
    }

    impl<Parent, Related> HasManyHydrator<Parent, Related>
    where
        Parent: Model + DeserializeOwned + Send + Sync + Clone,
        Related: Model + DeserializeOwned + Send + Sync + Clone,
    {
        pub fn new(metadata: RelationshipMetadata) -> Self {
            Self {
                _loader: TypeSafeRelationshipLoader::new(metadata),
            }
        }

        /// Hydrate HasMany relationship into a TypeSafeRelationship container
        pub fn hydrate_has_many(
            &self,
            parent: &Parent,
            related_rows: &[PgRow],
            foreign_key_column: &str,
        ) -> ModelResult<HasMany<Related>> {
            let parent_key = parent
                .primary_key()
                .ok_or_else(|| {
                    ModelError::Configuration("Parent model has no primary key".to_string())
                })?
                .to_string();

            // Find all related models for this parent
            let related_models: Result<Vec<Related>, ModelError> = related_rows
                .iter()
                .filter(|row| match row.try_get::<String, _>(foreign_key_column) {
                    Ok(fk) => fk == parent_key,
                    Err(_) => false,
                })
                .map(|row| ModelHydrator::<Related>::new().hydrate_single(row))
                .collect();

            let mut relationship = HasMany::new(RelationshipMetadata::new(
                RelationshipType::HasMany,
                "related".to_string(),
                Related::table_name().to_string(),
                std::any::type_name::<Related>().to_string(),
                super::super::metadata::ForeignKeyConfig::simple(
                    foreign_key_column.to_string(),
                    Related::table_name().to_string(),
                ),
            ));

            relationship.set_loaded(related_models?);
            Ok(relationship)
        }
    }

    /// Hydrator for BelongsTo relationships
    pub struct BelongsToHydrator<Child, Parent>
    where
        Child: Model + DeserializeOwned + Send + Sync + Clone,
        Parent: Model + DeserializeOwned + Send + Sync + Clone,
    {
        _loader: TypeSafeRelationshipLoader<Child, Parent>,
    }

    impl<Child, Parent> BelongsToHydrator<Child, Parent>
    where
        Child: Model + DeserializeOwned + Send + Sync + Clone,
        Parent: Model + DeserializeOwned + Send + Sync + Clone,
    {
        pub fn new(metadata: RelationshipMetadata) -> Self {
            Self {
                _loader: TypeSafeRelationshipLoader::new(metadata),
            }
        }

        /// Hydrate BelongsTo relationship into a TypeSafeRelationship container
        pub fn hydrate_belongs_to(
            &self,
            child: &Child,
            parent_rows: &[PgRow],
            foreign_key_column: &str,
        ) -> ModelResult<BelongsTo<Parent>> {
            // Get the foreign key value from the child model
            let child_fields = child.to_fields();
            let foreign_key_value = child_fields
                .get(foreign_key_column)
                .and_then(|v| v.as_str())
                .ok_or_else(|| {
                    ModelError::Configuration(format!(
                        "Child model missing foreign key '{}'",
                        foreign_key_column
                    ))
                })?;

            // Find the parent model
            let parent_model = parent_rows
                .iter()
                .find(|row| {
                    match row.try_get::<String, _>("id") {
                        // Assuming parent PK is 'id'
                        Ok(id) => id == foreign_key_value,
                        Err(_) => false,
                    }
                })
                .map(|row| ModelHydrator::<Parent>::new().hydrate_single(row))
                .transpose()?;

            let mut relationship = BelongsTo::new(RelationshipMetadata::new(
                RelationshipType::BelongsTo,
                "parent".to_string(),
                Parent::table_name().to_string(),
                std::any::type_name::<Parent>().to_string(),
                super::super::metadata::ForeignKeyConfig::simple(
                    foreign_key_column.to_string(),
                    Parent::table_name().to_string(),
                ),
            ));

            relationship.set_loaded(parent_model);
            Ok(relationship)
        }
    }
}

/// Utility functions for type-safe hydration
pub mod hydration_utils {
    use super::*;

    /// Extract column names from a database row
    pub fn extract_column_names(row: &PgRow) -> Vec<String> {
        row.columns()
            .iter()
            .map(|col| col.name().to_string())
            .collect()
    }

    /// Check if a row has all required columns for a model
    pub fn has_required_columns<T: Model>(row: &PgRow, required_columns: &[&str]) -> bool {
        let column_names: std::collections::HashSet<_> =
            row.columns().iter().map(|col| col.name()).collect();

        required_columns
            .iter()
            .all(|col| column_names.contains(col))
    }

    /// Convert a row to a JSON-like HashMap for debugging
    pub fn row_to_debug_map(row: &PgRow) -> HashMap<String, String> {
        let mut map = HashMap::new();

        for (i, column) in row.columns().iter().enumerate() {
            let column_name = column.name();

            // Try to get the value as different types for debugging
            let value_str = if let Ok(value) = row.try_get::<Option<String>, _>(i) {
                format!("{:?}", value)
            } else if let Ok(value) = row.try_get::<Option<i64>, _>(i) {
                format!("{:?}", value)
            } else if let Ok(value) = row.try_get::<Option<bool>, _>(i) {
                format!("{:?}", value)
            } else {
                "<unknown_type>".to_string()
            };

            map.insert(column_name.to_string(), value_str);
        }

        map
    }
}

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

    // Use the same test models from containers.rs
    #[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(),
            })
        }
    }

    #[test]
    fn test_model_hydrator_creation() {
        let hydrator = ModelHydrator::<TestUser>::new();

        // Test that the hydrator can be created
        // Full testing would require actual database rows
        let _ = hydrator; // Suppress unused warning
    }

    #[test]
    fn test_relationship_hydrator_creation() {
        let metadata = RelationshipMetadata::new(
            RelationshipType::HasMany,
            "posts".to_string(),
            "posts".to_string(),
            "TestPost".to_string(),
            ForeignKeyConfig::simple("user_id".to_string(), "posts".to_string()),
        );

        let hydrator = RelationshipHydrator::<TestUser, TestUser>::new(metadata);
        let _ = hydrator.with_related_prefix("post_".to_string());

        // Test creation succeeds
        assert!(true);
    }

    #[test]
    fn test_specialized_hydrator_creation() {
        let metadata = RelationshipMetadata::new(
            RelationshipType::HasOne,
            "profile".to_string(),
            "profiles".to_string(),
            "Profile".to_string(),
            ForeignKeyConfig::simple("user_id".to_string(), "profiles".to_string()),
        );

        let _hydrator = specialized_hydrators::HasOneHydrator::<TestUser, TestUser>::new(metadata);

        // Test creation succeeds
        assert!(true);
    }

    #[test]
    fn test_hydration_utils() {
        let required_columns = vec!["id", "name", "email"];

        // Test utility functions exist
        let _ = hydration_utils::extract_column_names;
        let _ = hydration_utils::has_required_columns::<TestUser>;
        let _ = hydration_utils::row_to_debug_map;
        let _ = required_columns; // Suppress unused warning

        assert!(true);
    }
}