Skip to main content

sea_orm_relations_kit/
lib.rs

1//! Relations for [SeaORM](https://www.sea-ql.org/SeaORM/) that it does not have out of
2//! the box, with the operations that make them practical.
3//!
4//! - **Polymorphic relations**, where a child row points at an owner that can be one of
5//!   several entities (`commentable_type` + `commentable_id`): [`Morph`] for one-to-one
6//!   and one-to-many, with the child resolving its owner through [`morph_target!`];
7//!   [`MorphToMany`] for many-to-many through a pivot; [`Morphable`] names each owner
8//!   entity.
9//! - **Pivot operations** for polymorphic and plain ([`BelongsToMany`]) many-to-many:
10//!   `attach`, `detach`, `sync`, `sync_without_detaching`, `toggle`,
11//!   `update_pivot`, ordered pivots and extra pivot columns.
12//! - **Eager loading and counts** without N+1: `load_for`, `count_for`.
13//! - **For `SeaORM`'s own relations** (anything with `Related<R>`): [`count_related`],
14//!   [`exists_related`], [`aggregate_related`] (sum, average, min, max) and one row per
15//!   owner by a column ([`latest_of_many`], [`oldest_of_many`], [`of_many`]).
16//! - **Filters** as conditions for any query: owners that have (or lack) matching
17//!   related rows, children whose owner matches a query, owners with all given keys.
18//! - **Migrations**: [`schema`] builds the polymorphic columns and their index.
19//!
20//! Batch methods put the owners' keys in one `IN (...)` list; pass a page of rows,
21//! not a whole table (Postgres allows 65 535 bind parameters per query).
22//!
23//! Polymorphic columns cannot have a foreign key, so the database cannot clean up
24//! after a deleted owner. Call `delete_for` / `detach_all` in the owner's delete hook,
25//! or `delete_orphans` from a scheduled task.
26
27mod key;
28mod morph;
29mod of_many;
30mod pivot;
31mod related;
32pub mod schema;
33mod target;
34
35pub use key::Key;
36pub use morph::{Morph, Morphable};
37pub use of_many::Pick;
38pub use pivot::{BelongsToMany, Changes, MorphToMany, Morphed, PivotOwner, PivotRelation, Plain};
39pub use related::{
40    aggregate_related, count_related, count_related_with, exists_related, latest_of_many, of_many,
41    oldest_of_many, Aggregate,
42};
43pub use target::MorphTarget;
44
45/// Used by [`morph_target!`]; not part of the public API.
46#[doc(hidden)]
47pub mod __private {
48    pub use crate::target::{find_by_keys, unknown_type};
49    pub use sea_orm::{ConnectionTrait, DbErr, EntityTrait, Value};
50}