Skip to main content

Crate sea_orm_taggable

Crate sea_orm_taggable 

Source
Expand description

Tags for any SeaORM entity.

One tags table serves every taggable entity through the polymorphic taggables pivot. Mark an entity, and each of its rows gets a tags handle:

use sea_orm::{DatabaseConnection, DbErr, EntityTrait, QueryFilter};
use sea_orm_taggable::{HasTags, Morphable, TagFilters, Taggable};

impl Morphable for posts::Entity {
    const MORPH_TYPE: &'static str = "post";
}
impl Taggable for posts::Entity {}

async fn example(db: &DatabaseConnection, post: &posts::Model) -> Result<(), DbErr> {
    post.tags().attach(db, ["Rust", "SeaORM"], None).await?;
    post.tags().attach(db, ["Beginner"], Some("level")).await?;

    let about_rust = posts::Entity::find()
        .filter(posts::Entity::with_any_tags(["rust"], None))
        .all(db)
        .await?;
    Ok(())
}
  • Tags are created from names on first use; a slug is made from the name, unique within the tag’s type.
  • Types group tags ("topic", "level"); None is the plain group.
  • Each row keeps its tags in its own order; tags also have a global position within their type (tag::Model::move_to_start and friends).
  • Load the tags of a page of rows in two queries with load_tags.

The pivot cannot have a foreign key to the tagged rows, so call Tags::detach_all in the row’s delete hook (for soft-deleted rows, when they are deleted for good), or delete_orphans from a scheduled task.

Modules§

schema
The migration for the tags and taggables tables.
tag
The tags table: a tag, its slug, an optional type and its position in the list.
taggable
The taggables pivot table: which tag is on which row, and in which position.

Structs§

Changes
What a sync or toggle changed.
Tags
The tags of one row, from HasTags::tags.

Enums§

Key
A primary key value, normalized.
TagInput
A tag given by name (or slug), or a tag already loaded.

Constants§

TAGS
The polymorphic many-to-many relation behind the tags, for queries this crate does not cover; see sea-orm-relations-kit.

Traits§

HasTags
Gives every model of a Taggable entity a tags handle.
Morphable
An entity that can own polymorphic children, with the name stored in their *_type column.
TagFilters
Filters on a Taggable entity’s tags, as conditions for .filter(...).
Taggable
An entity whose rows can be tagged. Its Morphable::MORPH_TYPE is stored in taggables.taggable_type, and its primary key must be an integer.

Functions§

count_tags
How many tags each row has, in one query.
delete_orphans
Removes the pivot rows of O rows that no longer exist. Returns how many went.
load_tags
The tags of many rows, in two queries, each list in the row’s own order.