SeaORM Taggable
Tags for any SeaORM entity: attach, detach and sync by name, tag types, ordering, slugs and filters like with_any_tags.
One tags table serves posts, videos and anything else: post.tags().attach(db, ["Rust", "SeaORM"], None) creates the tags it needs, and posts::Entity::with_any_tags(["rust"], None)
finds the posts. Works with any SeaORM app,
including Loco.
Features
- Any entity: one trait per entity; posts and videos with the same id keep their own tags
- Tags from names: attaching a name creates the tag; names match ignoring case, or by slug
- Slugs from SeaORM Sluggable, unique within a tag type
- Types: group tags (
"topic","level"); plain tags are their own group - Order: each row keeps its tags in its own order; tags have a position within their type
- Filters: with any, all or none of the given tags, untagged
- Eager loading: the tags of a whole page in two queries
- Tested on SQLite and Postgres
Installation
cargo add sea-orm-taggable
The tags and taggables tables come from sea_orm_taggable::schema in a migration.
Quick start
use sea_orm_taggable::{HasTags, Morphable, TagFilters, Taggable};
impl Morphable for posts::Entity {
const MORPH_TYPE: &'static str = "post";
}
impl Taggable for posts::Entity {}
post.tags().attach(db, ["Rust", "SeaORM"], None).await?;
post.tags().attach(db, ["Beginner"], Some("level")).await?;
post.tags().sync(db, ["Rust", "Loco"]).await?;
let tags = post.tags().get(db).await?;
let about_rust = posts::Entity::find()
.filter(posts::Entity::with_any_tags(["rust"], None))
.all(db)
.await?;
let tags_per_post = sea_orm_taggable::load_tags(db, &about_rust).await?;