reinhardt-db 0.1.1

Django-style database layer for Reinhardt framework
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
//! Marker types for relationship field annotations.
//!
//! These types are used as field types to indicate relationship definitions
//! when using the `#[rel]` attribute macro.
//!
//! # Example
//!
//! ```rust,ignore
//! use reinhardt_db::orm::Model;
//! use reinhardt_macros::model;
//! use reinhardt_db::associations::ManyToManyField;
//! use uuid::Uuid;
//!
//! #[model(app_label = "users")]
//! pub struct User {
//!     #[field(primary_key = true)]
//!     pub id: Uuid,
//!
//!     // User -> User relationship (self-referential)
//!     #[rel(many_to_many, related_name = "followers")]
//!     pub following: ManyToManyField<User, User>,
//! }
//!
//! #[model(app_label = "users")]
//! pub struct Group {
//!     #[field(primary_key = true)]
//!     pub id: Uuid,
//!
//!     // Group -> User relationship
//!     #[rel(many_to_many, related_name = "groups")]
//!     pub members: ManyToManyField<Group, User>,
//! }
//! ```

use std::marker::PhantomData;

// Import DatabaseConnection and QueryRow for method signatures
use crate::orm::{DatabaseConnection, QueryRow};

/// Configuration for ManyToMany relationship operations
///
/// This struct groups together the parameters needed to identify and work with
/// a ManyToMany relationship through a junction table.
#[non_exhaustive]
#[derive(Debug, Clone)]
pub struct ManyToManyConfig<PK> {
	/// Primary key of the source instance
	pub source_pk: PK,
	/// Name of the junction table
	pub through_table: String,
	/// Column name for the source foreign key in junction table
	pub source_field: String,
	/// Column name for the target foreign key in junction table
	pub target_field: String,
}

impl<PK> ManyToManyConfig<PK> {
	/// Create a new ManyToManyConfig
	pub fn new(
		source_pk: PK,
		through_table: String,
		source_field: String,
		target_field: String,
	) -> Self {
		Self {
			source_pk,
			through_table,
			source_field,
			target_field,
		}
	}
}

/// Marker type for ManyToMany relationship fields.
///
/// This type is used as the field type for `#[rel(many_to_many, ...)]` attributes.
/// It indicates that the field represents a many-to-many relationship.
///
/// The type parameters:
/// - `Source`: The source model type (the model containing this field)
/// - `Target`: The target model type (the related model)
/// - `S`: Relationship metadata type (defaults to `()`)
///
/// The `S` parameter allows storing relationship-specific metadata at the type level,
/// such as through-table configuration, ordering, or custom relationship behavior.
///
/// The intermediate table is automatically generated based on the source and target types:
/// - Table name: `{app_label}_{source_table}_{field_name}`
/// - Source FK: `{source_table}_id`
/// - Target FK: `{target_table}_id`
///
/// # Example
///
/// ```rust,ignore
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use reinhardt_db::orm::Model;
/// use reinhardt_macros::model;
/// use reinhardt_db::associations::ManyToManyField;
/// use uuid::Uuid;
///
/// #[model(app_label = "auth", table_name = "users")]
/// pub struct User {
///     #[field(primary_key = true)]
///     pub id: Uuid,
///
///     // User -> Group relationship
///     #[rel(many_to_many, related_name = "members")]
///     pub groups: ManyToManyField<User, Group>,
///
///     // Self-referential relationship (following)
///     #[rel(many_to_many, related_name = "followers")]
///     pub following: ManyToManyField<User, User>,
/// }
///
/// // With custom metadata type:
/// struct OrderedRelation;
///
/// #[model(app_label = "project", table_name = "tasks")]
/// pub struct Task {
///     #[rel(many_to_many)]
///     pub tags: ManyToManyField<Task, Tag, OrderedRelation>,
/// }
///
/// // Usage with ManyToManyAccessor:
/// use reinhardt_db::orm::ManyToManyAccessor;
///
/// let user = User::find_by_id(&db, user_id).await?;
/// let accessor = ManyToManyAccessor::new(&user, "groups", ());
///
/// // Add relationship
/// accessor.add(&group).await?;
///
/// // Get all related
/// let groups = accessor.all().await?;
///
/// // Remove relationship
/// accessor.remove(&group).await?;
///
/// // Clear all
/// accessor.clear().await?;
///
/// # Ok(())
/// # }
/// ```
/// Marker type for ManyToMany relationship.
///
/// `Default` is implemented manually to avoid requiring `Source` and `Target` to implement `Default`.
/// This is because the derive macro would add `Source: Default, Target: Default` bounds.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
#[serde(bound = "")]
pub struct ManyToManyField<Source, Target, S = ()>(PhantomData<(Source, Target, S)>);

impl<Source, Target, S> Default for ManyToManyField<Source, Target, S> {
	fn default() -> Self {
		Self(PhantomData)
	}
}

impl<Source, Target, S> ManyToManyField<Source, Target, S> {
	/// Creates a new ManyToManyField marker.
	///
	/// This constructor hides the internal `PhantomData` implementation,
	/// providing a clean API for users without exposing implementation details.
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_db::associations::ManyToManyField;
	///
	/// struct User;
	/// struct Tag;
	///
	/// // User -> Tag relationship with default metadata type ()
	/// let field: ManyToManyField<User, Tag> = ManyToManyField::new();
	///
	/// // Self-referential relationship
	/// let following: ManyToManyField<User, User> = ManyToManyField::new();
	///
	/// // Custom metadata type
	/// struct CustomMetadata;
	/// let field_with_metadata: ManyToManyField<User, Tag, CustomMetadata> = ManyToManyField::new();
	/// ```
	#[inline]
	pub const fn new() -> Self {
		Self(PhantomData)
	}
}

impl<Source, Target, S> ManyToManyField<Source, Target, S>
where
	Source: std::fmt::Display,
	Target: std::fmt::Display,
{
	/// Get a manager for this ManyToMany relationship
	///
	/// This is an internal helper method that creates a `ManyToManyManager`
	/// with the appropriate configuration for this field.
	///
	/// # Arguments
	///
	/// * `config` - Configuration containing source_pk, through_table, source_field, and target_field
	///
	/// # Type Parameters
	///
	/// * `PK` - Primary key type (must implement Display and Clone)
	fn get_manager<PK>(
		&self,
		config: ManyToManyConfig<PK>,
	) -> crate::prelude::many_to_many_manager::ManyToManyManager<Source, Target, PK>
	where
		PK: std::fmt::Display + Clone,
	{
		crate::prelude::many_to_many_manager::ManyToManyManager::new(
			config.source_pk,
			config.through_table,
			config.source_field,
			config.target_field,
		)
	}

	/// Add a target instance to this ManyToMany relationship
	///
	/// # Arguments
	///
	/// * `conn` - Database connection
	/// * `config` - Configuration containing source_pk, through_table, source_field, and target_field
	/// * `target_pk` - Primary key of the target instance to add
	///
	/// # Example
	///
	/// ```ignore
	/// use reinhardt_db::associations::ManyToManyConfig;
	///
	/// // Add a user to a group
	/// let config = ManyToManyConfig::new(
	///     group.id,
	///     "auth_group_members".to_string(),
	///     "group_id".to_string(),
	///     "user_id".to_string(),
	/// );
	/// group.members.add_with_db(&db, config, user.id).await?;
	/// ```
	pub async fn add_with_db<PK, TPK>(
		&self,
		conn: &DatabaseConnection,
		config: ManyToManyConfig<PK>,
		target_pk: TPK,
	) -> reinhardt_core::exception::Result<()>
	where
		PK: std::fmt::Display + Clone,
		TPK: std::fmt::Display,
	{
		self.get_manager(config).add_with_db(conn, target_pk).await
	}

	/// Remove a target instance from this ManyToMany relationship
	///
	/// # Arguments
	///
	/// * `conn` - Database connection
	/// * `config` - Configuration containing source_pk, through_table, source_field, and target_field
	/// * `target_pk` - Primary key of the target instance to remove
	pub async fn remove_with_db<PK, TPK>(
		&self,
		conn: &DatabaseConnection,
		config: ManyToManyConfig<PK>,
		target_pk: TPK,
	) -> reinhardt_core::exception::Result<()>
	where
		PK: std::fmt::Display + Clone,
		TPK: std::fmt::Display,
	{
		self.get_manager(config)
			.remove_with_db(conn, target_pk)
			.await
	}

	/// Check if a target instance exists in this ManyToMany relationship
	///
	/// # Arguments
	///
	/// * `conn` - Database connection
	/// * `config` - Configuration containing source_pk, through_table, source_field, and target_field
	/// * `target_pk` - Primary key of the target instance to check
	///
	/// # Returns
	///
	/// * `Ok(true)` if the relationship exists
	/// * `Ok(false)` if not
	pub async fn contains_with_db<PK, TPK>(
		&self,
		conn: &DatabaseConnection,
		config: ManyToManyConfig<PK>,
		target_pk: TPK,
	) -> reinhardt_core::exception::Result<bool>
	where
		PK: std::fmt::Display + Clone,
		TPK: std::fmt::Display,
	{
		self.get_manager(config)
			.contains_with_db(conn, target_pk)
			.await
	}

	/// Get all target instances in this ManyToMany relationship
	///
	/// # Arguments
	///
	/// * `conn` - Database connection
	/// * `config` - Configuration containing source_pk, through_table, source_field, and target_field
	/// * `target_table` - Name of the target table
	/// * `target_pk_field` - Name of primary key column in target table
	///
	/// # Returns
	///
	/// Vector of QueryRow representing all related target instances
	///
	/// Note: Returns `Vec<QueryRow>` instead of `Vec<Target>` for flexibility.
	/// Callers can deserialize QueryRow to their target type as needed.
	pub async fn all_with_db<PK>(
		&self,
		conn: &DatabaseConnection,
		config: ManyToManyConfig<PK>,
		target_table: &str,
		target_pk_field: &str,
	) -> reinhardt_core::exception::Result<Vec<QueryRow>>
	where
		PK: std::fmt::Display + Clone,
	{
		self.get_manager(config)
			.all_with_db(conn, target_table, target_pk_field)
			.await
	}

	/// Clear all relationships for the source instance
	///
	/// # Arguments
	///
	/// * `conn` - Database connection
	/// * `config` - Configuration containing source_pk, through_table, source_field, and target_field
	pub async fn clear_with_db<PK>(
		&self,
		conn: &DatabaseConnection,
		config: ManyToManyConfig<PK>,
	) -> reinhardt_core::exception::Result<()>
	where
		PK: std::fmt::Display + Clone,
	{
		self.get_manager(config).clear_with_db(conn).await
	}

	/// Count the number of related target instances
	///
	/// # Arguments
	///
	/// * `conn` - Database connection
	/// * `config` - Configuration containing source_pk, through_table, source_field, and target_field
	///
	/// # Returns
	///
	/// Number of related instances
	pub async fn count_with_db<PK>(
		&self,
		conn: &DatabaseConnection,
		config: ManyToManyConfig<PK>,
	) -> reinhardt_core::exception::Result<usize>
	where
		PK: std::fmt::Display + Clone,
	{
		self.get_manager(config).count_with_db(conn).await
	}
}

/// Marker type for ForeignKey relationship fields.
///
/// This type is used as the field type for `#[rel(foreign_key, ...)]` attributes.
/// It represents a many-to-one relationship where multiple instances of the
/// containing model can reference a single instance of `T`.
///
/// The type parameter:
/// - `T`: The target model type (the related model)
///
/// When using this type, the macro automatically:
/// - Generates a `{field_name}_id` column for the foreign key value
/// - Infers the target model from `T` (no need for `to = Model` attribute)
/// - Generates lazy load accessor method `{field_name}(&self, conn)` -> `Result<Option<T>>`
///
/// # Example
///
/// ```rust,ignore
/// use reinhardt_db::associations::ForeignKeyField;
/// use reinhardt_macros::model;
///
/// #[model(app_label = "blog", table_name = "posts")]
/// pub struct Post {
///     #[field(primary_key = true)]
///     pub id: i64,
///
///     // Generates `author_id` column automatically
///     #[rel(foreign_key, related_name = "posts")]
///     pub author: ForeignKeyField<User>,
/// }
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
#[serde(bound = "")]
pub struct ForeignKeyField<T>(PhantomData<T>);

impl<T> Default for ForeignKeyField<T> {
	fn default() -> Self {
		Self(PhantomData)
	}
}

impl<T> ForeignKeyField<T> {
	/// Creates a new ForeignKeyField marker.
	///
	/// This constructor hides the internal `PhantomData` implementation.
	#[inline]
	pub const fn new() -> Self {
		Self(PhantomData)
	}
}

/// Marker type for OneToOne relationship fields.
///
/// This type is used as the field type for `#[rel(one_to_one, ...)]` attributes.
/// It represents a one-to-one relationship where each instance of the
/// containing model references exactly one instance of `T`.
///
/// The type parameter:
/// - `T`: The target model type (the related model)
///
/// When using this type, the macro automatically:
/// - Generates a `{field_name}_id` column for the foreign key value
/// - Infers the target model from `T` (no need for `to = Model` attribute)
/// - Generates lazy load accessor method `{field_name}(&self, conn)` -> `Result<Option<T>>`
/// - Adds UNIQUE constraint to the generated column
///
/// # Example
///
/// ```rust,ignore
/// use reinhardt_db::associations::OneToOneField;
/// use reinhardt_macros::model;
///
/// #[model(app_label = "auth", table_name = "profiles")]
/// pub struct Profile {
///     #[field(primary_key = true)]
///     pub id: i64,
///
///     // Generates `user_id` column with UNIQUE constraint
///     #[rel(one_to_one, related_name = "profile")]
///     pub user: OneToOneField<User>,
/// }
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
#[serde(bound = "")]
pub struct OneToOneField<T>(PhantomData<T>);

impl<T> Default for OneToOneField<T> {
	fn default() -> Self {
		Self(PhantomData)
	}
}

impl<T> OneToOneField<T> {
	/// Creates a new OneToOneField marker.
	///
	/// This constructor hides the internal `PhantomData` implementation.
	#[inline]
	pub const fn new() -> Self {
		Self(PhantomData)
	}
}

/// Marker type for OneToMany relationship fields (reverse of ForeignKey).
///
/// This type is used as the field type for `#[rel(one_to_many, ...)]` attributes.
/// It represents the reverse side of a ForeignKey relationship.
///
/// The type parameters:
/// - `T`: The related model type
/// - `S`: Relationship metadata type (defaults to `()`)
///
/// # Example
///
/// ```rust,ignore
/// // On User model
/// #[rel(one_to_many, foreign_key = "author_id")]
/// pub posts: OneToManyField<Post>,
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct OneToManyField<T, S = ()>(PhantomData<(T, S)>);

impl<T, S> OneToManyField<T, S> {
	/// Creates a new OneToManyField marker.
	///
	/// This constructor hides the internal `PhantomData` implementation.
	#[inline]
	pub const fn new() -> Self {
		Self(PhantomData)
	}
}

/// Marker type for PolymorphicManyToMany relationship fields.
///
/// This type is used as the field type for `#[rel(polymorphic_many_to_many, ...)]` attributes.
/// It indicates a polymorphic many-to-many relationship.
///
/// The type parameters:
/// - `K`: The key type (usually `i64` or `Uuid`)
/// - `S`: Relationship metadata type (defaults to `()`)
///
/// # Example
///
/// ```rust,ignore
/// #[rel(polymorphic_many_to_many, name = "taggable")]
/// pub tags: PolymorphicManyToManyField<Uuid>,
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct PolymorphicManyToManyField<K, S = ()>(PhantomData<(K, S)>);

impl<K, S> PolymorphicManyToManyField<K, S> {
	/// Creates a new PolymorphicManyToManyField marker.
	///
	/// This constructor hides the internal `PhantomData` implementation.
	#[inline]
	pub const fn new() -> Self {
		Self(PhantomData)
	}
}

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

	#[test]
	fn test_many_to_many_field_creation() {
		struct User;
		struct Group;
		// User -> Group relationship
		let _field: ManyToManyField<User, Group> = ManyToManyField::new();
	}

	#[test]
	fn test_many_to_many_field_self_referential() {
		struct User;
		// User -> User (self-referential, e.g., following)
		let _field: ManyToManyField<User, User> = ManyToManyField::new();
	}

	#[test]
	fn test_one_to_many_field_creation() {
		struct Post;
		let _field: OneToManyField<Post> = OneToManyField::new();
	}

	#[test]
	fn test_polymorphic_many_to_many_field_creation() {
		let _field: PolymorphicManyToManyField<i64> = PolymorphicManyToManyField::new();
	}

	#[test]
	fn test_default_impl() {
		#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
		struct Article;
		#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
		struct Tag;
		// Article -> Tag relationship
		let field1: ManyToManyField<Article, Tag> = ManyToManyField::default();
		let field2: ManyToManyField<Article, Tag> = ManyToManyField::new();
		assert_eq!(field1, field2);
	}

	#[test]
	fn test_many_to_many_field_with_metadata() {
		struct User;
		struct Tag;
		struct OrderedRelation;

		// Default metadata type ()
		let _field1: ManyToManyField<User, Tag> = ManyToManyField::new();

		// Custom metadata type
		let _field2: ManyToManyField<User, Tag, OrderedRelation> = ManyToManyField::new();
	}

	#[test]
	fn test_foreign_key_field_creation() {
		struct User;
		let _field: ForeignKeyField<User> = ForeignKeyField::new();
	}

	#[test]
	fn test_foreign_key_field_default() {
		#[derive(Debug, PartialEq)]
		struct User;
		let field1: ForeignKeyField<User> = ForeignKeyField::default();
		let field2: ForeignKeyField<User> = ForeignKeyField::new();
		assert_eq!(field1, field2);
	}

	#[test]
	fn test_one_to_one_field_creation() {
		struct User;
		let _field: OneToOneField<User> = OneToOneField::new();
	}

	#[test]
	fn test_one_to_one_field_default() {
		#[derive(Debug, PartialEq)]
		struct User;
		let field1: OneToOneField<User> = OneToOneField::default();
		let field2: OneToOneField<User> = OneToOneField::new();
		assert_eq!(field1, field2);
	}
}