use crate::model::Model;
pub(crate) mod access;
pub(crate) mod aggregates;
mod any;
mod belongs_to;
mod belongs_to_many;
pub(crate) mod core;
mod define;
pub(crate) mod eager;
pub(crate) mod existence;
mod has;
pub(crate) mod keys;
pub(crate) mod lazy;
pub(crate) mod morph;
mod morph_map;
mod one_of_many;
mod path;
mod pivot;
pub(crate) mod registry;
mod requalify;
mod store;
mod through;
pub(crate) mod touch;
pub use access::AccessFilter;
pub use any::AnyRecord;
pub use belongs_to::BelongsTo;
pub use belongs_to_many::{BelongsToMany, MorphToMany, joining_table};
pub use define::{KeyColumn, Relationships};
pub use has::{HasMany, HasOne, MorphMany, MorphOne};
pub use lazy::{Associate, Dissociate, LazyLoading, LazyLoadingViolation, load_missing_chain};
pub use morph::{MorphRef, MorphTargets, MorphTo, MorphToOps, MorphTypes, MorphWith};
pub use morph_map::MorphMap;
pub use path::{ConstrainPath, RelationPath, RelationPathTo};
pub use pivot::{Pivot, PivotKind, RelatedId, SyncChanges};
pub use registry::{DynRelation, RelationKind, RelationMeta, RelationRegistry};
pub use store::{RelationValue, Relations};
pub use through::{
Cardinality, HasManyThrough, HasOneThrough, Many, One, PendingThrough, ThroughLeg, ThroughOf,
};
pub use touch::{TouchList, is_ignoring_touch, without_touching, without_touching_on};
pub(crate) mod private {
use std::borrow::Cow;
use std::sync::Arc;
use crate::model::Model;
use crate::relations::registry::ErasedRelation;
pub trait Erase<P: Model> {
fn erase(self) -> Arc<dyn ErasedRelation<P>>;
fn rename(&mut self, name: Cow<'static, str>);
}
}
#[diagnostic::on_unimplemented(
message = "`{Self}` is not a relation of `{P}`",
label = "a relation method returns a relation",
note = "relation methods return a relation built from `self`: `self.belongs_to(..)` (`BelongsTo<{P}, R>`), `self.has_many(..)` (`HasMany<{P}, R>`), `self.belongs_to_many(..)`, `self.morph_to(..)`, ..."
)]
pub trait Relation<P: Model>: Clone + Send + Sync + 'static + private::Erase<P> {
type Value: RelationValue;
fn name(&self) -> &str;
fn kind(&self) -> RelationKind;
fn meta(&self) -> RelationMeta;
}
pub trait RelatedTo<P: Model>: Relation<P> {
type Related: Model;
}
#[doc(hidden)]
pub mod derive_support {
use std::borrow::Cow;
use crate::model::Model;
use crate::relations::{DynRelation, Relation};
pub fn named<P: Model, R: Relation<P>>(mut relation: R, name: &'static str) -> R {
crate::relations::private::Erase::rename(&mut relation, Cow::Borrowed(name));
relation
}
pub fn erased<P: Model, R: Relation<P>>(relation: R) -> DynRelation<P> {
DynRelation::new(relation)
}
pub trait NoRelations: Model {
fn __navree_relations(name: &str) -> Option<DynRelation<Self>> {
let _ = name;
None
}
fn __navree_relation_names() -> &'static [&'static str] {
&[]
}
}
impl<M: Model> NoRelations for M {}
#[diagnostic::on_unimplemented(
message = "`{Self}` has relations, so it must implement `Default`",
note = "relation definitions are read from a default instance: add `#[derive(Default)]` (or implement `Default`) for `{Self}`"
)]
pub trait ModelWithRelations: Model + Default {}
impl<M: Model + Default> ModelWithRelations for M {}
pub fn template<M: ModelWithRelations>() -> M {
M::default()
}
pub fn defined<M: Model>(name: &str) -> Option<DynRelation<M>> {
crate::relations::registry::defined::<M>(name)
}
pub fn defined_names<M: Model>() -> Vec<Cow<'static, str>> {
crate::relations::registry::defined_names::<M>()
}
}