sea-orm-sluggable
Unique, readable URL slugs for SeaORM entities, generated on save.
Describe the slug once, and every insert gets one: "Rust for PHP developers" becomes
rust-for-php-developers, the next one rust-for-php-developers-1.
Features
- Automatic — one trait and one line in the save hook, no manual slug code
- Unique —
title,title-1,title-2, …, optionally only within a scope (per category, per tenant) - Any language — every script is transliterated to ASCII; official national rules per feature (
uktoday), or your own - Safe for URLs — slugs stay stable on update by default; reserved words (
new,edit) never become slugs - Flexible — several source columns, separator, length, custom suffixes, conditional skipping
- Tested — against a real database (in-memory SQLite), every API doc example compiled
Installation
[]
= "0.1"
Quick start
Describe the slug:
use ;
Fill it in the save hook:
That's it:
let post = ActiveModel
.insert
.await?;
assert_eq!;
let found = .await?;
When the slug is generated
new
.on_create // default: on
.on_update // default: off, see below
.prevent_overwrite // default: off; keep a slug an editor typed in
.skip_when
on_update is off by default: changing a slug breaks every link to the old URL. When it is
on, the slug is rebuilt when a source column is set on the model and gives another slug:
saving the same title again keeps hello-1 as it is. A partial update that sets only some
of several source columns leaves the slug alone, and a row never collides with its own
slug.
Several source columns
new
// "Ada", "Lovelace" -> "ada-lovelace"; empty and NULL columns are skipped
Unique within a scope
new
.scope
// "hello" can exist once per category
With sea-orm-soft-delete, scope to rows outside the trash, so a trashed post frees its slug:
.scope
Back it with a unique index
The slug is checked, then saved: two requests saving "Hello" at the same moment can both
see hello as free. Add a unique index on the slug (with the scope columns, and
WHERE deleted_at IS NULL for soft deletes) so the database refuses the second one, and
retry that save:
manager
.create_index
.await?;
Options
use SlugOptions;
new.options
Reserved words
A post titled "New" must not take the slug new and shadow your /posts/new route. List the
words your routes use, and such a slug gets a suffix instead (new-1). The comparison ignores
case.
Without a model
let slug = slugify;
let slug = .await?;
Languages
The default transliteration, AnyAscii, handles every script but knows no national rules.
For those, enable a language feature:
= { = "0.1", = ["uk"] }
use ;
let options = default.transliterator;
// "Юрій Яковенко" -> "yurii-yakovenko" (AnyAscii alone: "yuriy-yakovenko")
| Feature | Language | Rules |
|---|---|---|
uk |
Ukrainian | Cabinet of Ministers resolution No. 55 (2010) |
Your own language
Implement Transliterator and pass it to SlugOptions::transliterator:
use Transliterator;
;
Adding a language to the package
- Copy
src/languages/uk.rstosrc/languages/<code>.rsand replace the table. - Add tests built from the official examples of your national standard.
- Add the feature to
Cargo.tomland a#[cfg(feature = "<code>")]export insrc/languages/mod.rs. - Add a row to the table above and open a pull request.
Errors
fill_slug returns SlugError::Empty when the source has no letters or digits (for example
"!!!"), and SlugError::Db when the uniqueness query fails. Inside before_save the ?
turns both into DbErr.
Contributing
Issues and pull requests are welcome, language tables especially. The tests need no database server:
License
Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.