@adonisjs/lucid-slugify

中文文档
4.0.0发布于 2026/06/25MITTypeScriptES Module构建来源已验证近 30 天下载 9638

Generate and persist unique slugs in your database via Lucid models

原始使用文档
文档目录22 节
  1. Introduction
  2. Installation and usage
  3. Uniqueness of slug
  4. dbIncrement
  5. shortId
  6. No strategy
  7. Updating slugs
  8. Null values and slug generation
  9. Available options
  10. Generating slugs from multiple fields
  11. Customizing the transformer
  12. Using custom strategies
  13. Self creating slugs
  14. API reference
  15. Entry points
  16. slugify(config)
  17. Slugifier
  18. SlugifyStrategyContract
  19. errors
  20. Contributing
  21. Code of Conduct
  22. License

@adonisjs/lucid-slugify

Use Lucid models to create URL-safe unique slugs and persist them in the database.


gh-workflow-image npm-image license-image

Introduction

Generating slugs is easy, but keeping them unique is hard. This package abstracts the hard parts and gives you a simple API to create and persist unique slugs to the database.

Lucid slugify exports the @slugify decorator, which you can use on the model fields to mark them as slugs and define the source fields from which the slug should be generated. Under the hood, the decorator registers beforeCreate and beforeUpdate hooks to compute the slug and persist it to the database.

In the following example, we mark the slug field as the slug and compute its value using the title field. We also use the dbIncrement strategy to keep slugs unique.

import { PostSchema } from '#database/schema'
import { slugify } from '@adonisjs/lucid-slugify'

export default class Post extends PostSchema {
  @slugify({
    strategy: 'dbIncrement',
    fields: ['title'],
  })
  declare slug: string
}

Installation and usage

You can install the @adonisjs/lucid-slugify package from the npm packages registry. Ensure your application uses @adonisjs/core@7 and @adonisjs/lucid@22.

npm i @adonisjs/lucid-slugify
yarn add @adonisjs/lucid-slugify
pnpm add @adonisjs/lucid-slugify

Once done, you can mark a field as a slug using the @slugify decorator. Make sure to specify the source field(s) from which the slug should be generated.

import { PostSchema } from '#database/schema'

// 👇 Import decorator
import { slugify } from '@adonisjs/lucid-slugify'

export default class Post extends PostSchema {
  // 👇 Use it on a column
  @slugify({
    fields: ['title'],
  })
  declare slug: string
}

The decorator computes the slug only when the column does not already have a value. If you assign a value to the slug column manually, your value is left untouched.

Uniqueness of slug

In the previous example, if two posts are created with the same title, they will have the same slug value.

This won't be a problem if you are the only author of your blog since you can always rename titles or might never write two articles with the same title.

However, if it's a community blog or forum, the chances of creating two or more posts with the same title are quite high.

You can use one of the following strategies to prevent duplicate slugs even when the titles are the same.

dbIncrement

The dbIncrement strategy performs a select query to find similar slugs and appends a counter when a duplicate slug is found. Given you have a database table with the following slugs:

  • Creating a post with slug=hello-world will result in hello-world-6.
  • Similarly, creating a post with slug=introduction-to-social-auth will result in introduction-to-social-auth-5.
+----+-----------------------------+-------------------------------+
| id | title                       | slug                          |
+----+-----------------------------+-------------------------------+
| 1  | Hello world                 | hello-world                   |
| 2  | Hello world                 | hello-world-5                 |
| 3  | Hello world                 | hello10world                  |
| 4  | Hello world                 | hello-10-world                |
| 5  | Introduction to social auth | introduction-to-social-auth   |
| 6  | Introduction to social auth | introduction-to-social-auth-4 |
| 7  | Hello world                 | hello-world-2                 |
| 8  | Hello world fanny           | hello-world-fanny             |
| 9  | Hello world                 | post-hello-world              |
| 10 | Hello world                 | post-11am-hello-world11       |
| 11 | Hello world                 | post-11am-hello-world         |
| 12 | Introduction to social auth | introdUction-to-Social-auTH-1 |
+----+-----------------------------+-------------------------------+

This strategy uses the same database connection as the model being persisted and works with PostgreSQL, MySQL (including 5.7), SQLite, and SQL Server. Using it with any other dialect throws the E_UNSUPPORTED_DB_DIALECT exception.

shortId

The shortId strategy appends a 10-character short id to the slug to make it unique. This strategy does not perform any additional database queries.

export default class Post extends PostSchema {
  @slugify({
    strategy: 'shortId',
    fields: ['title'],
  })
  declare slug: string
}
+----+-------------+------------------------+
| id | title       | slug                   |
+----+-------------+------------------------+
| 1  | Hello world | hello-world-yRPZZIWGgC |
+----+-------------+------------------------+

No strategy

When you do not specify a strategy, the slug is generated and persisted as-is, without any uniqueness guarantees. This is the default behavior and is suitable when the source fields are already unique (for example, a SKU or an email address).

Updating slugs

By default, slugs are not updated when you update a model instance, and this is how it should be when slugs are used to look up a record, as changing a slug will result in a broken URL.

However, if slugs are not primarily used to look up records, you may want to update them.

You can enable updates by using the allowUpdates flag.

export default class Post extends PostSchema {
  @slugify({
    strategy: 'dbIncrement',
    fields: ['title'],
    allowUpdates: true, // 👈
  })
  declare slug: string
}

When updates are enabled, the slug is recomputed only when one of the source fields has been modified. If you mutate the slug column manually, your value takes precedence and the slug is not recomputed.

You may also pass a function to allowUpdates to decide at runtime whether the slug should be updated for a given model instance.

@slugify({
  fields: ['title'],
  allowUpdates: (post) => post.status === 'draft',
})
declare slug: string

Null values and slug generation

The slugify decorator does not generate slugs when the value of one or more source fields is undefined or null.

Available options

Following is the list of available options accepted by the @slugify decorator.

{
"fields":

An array of source fields to use for generating the slug. The value of multiple fields is concatenated using the config.separator property. This is the only required option.

"strategy":

The strategy to use for keeping slugs unique. It can be one of the pre-existing strategy names ("dbIncrement" or "shortId") or a factory function that returns a custom strategy implementation. Defaults to no uniqueness handling.

"allowUpdates":

A boolean to enable updates, or a function that receives the model instance and returns a boolean. Updates are disabled by default.

"maxLength":

The maximum length for the generated slug. The final slug value can be slightly over the defined maxLength in the following scenarios.

No max length is applied by default.

  • When completeWords is set to true.
  • When using the dbIncrement strategy. The counter value is appended after trimming the value for the maxLength.
"completeWords":

A boolean that forces to complete the words when applying the maxLength property. Completing words will generate a slug larger than the maxLength. So, keep some buffer between the maxLength property and the database storage size.

Complete words are disabled by default.

"separator":

The separator to use for creating the slug. A dash - is used by default.

"transformer":

A custom function to convert non-string source values to a string before they are slugified. It receives the model instance, the field name, and the field value, and must return a string.

By default, booleans are converted to "1" or "0", Date values are converted using date.toJSON(), and everything else is converted using String(value).

}

Generating slugs from multiple fields

When you specify more than one field, their values are concatenated using the separator (in the order they are defined) and the result is slugified.

@slugify({
  fields: ['seriesName', 'title'],
  separator: '-',
})
declare slug: string

Customizing the transformer

The transformer is handy when a source field holds a rich data type that should not be stringified using the default rules.

@slugify({
  fields: ['title', 'publishedAt'],
  transformer: (post, field, value) => {
    if (field === 'publishedAt' && value instanceof DateTime) {
      return value.toFormat('yyyy-MM-dd')
    }

    return String(value)
  },
})
declare slug: string

Using custom strategies

Custom strategies can be used if you want to handle the uniqueness of slugs yourself. A strategy must implement the SlugifyStrategyContract interface.

import { LucidRow } from '@adonisjs/lucid/types/model'
import { SlugifyStrategyContract } from '@adonisjs/lucid-slugify/types'

export class MyCustomStrategy implements SlugifyStrategyContract {
  maxLengthBuffer: number = 0

  async makeSlugUnique(modelInstance: LucidRow, field: string, value: string) {
    return value
  }
}

The makeSlugUnique method receives the following arguments.

  • modelInstance: Reference to the model instance that will be persisted in the database.
  • field: The name of the field for which the unique slug will be created.
  • value: The base value to convert to a unique value.

Once you have created the strategy, you can use it with the @slugify decorator by registering it as a factory function. The factory receives the model class, the property name, and the resolved decorator config, allowing you to build the strategy with the same configuration.

export default class Post extends PostSchema {
  @slugify({
    strategy: (model, propertyName, config) => {
      return new MyCustomStrategy()
    },
    fields: ['title'],
  })
  declare slug: string
}

The built-in strategy classes are also exported, so you can reuse or extend them from a factory function.

import { DbIncrementStrategy, ShortIdStrategy } from '@adonisjs/lucid-slugify/strategies'

Self creating slugs

The default implementation used by Lucid slugify for creating slugs works great with English words. However, if you are using Non-Latin alphabets, replace the implementation for creating slugs with a custom one.

You can override the static slugify method on the Slugifier class. The following code has to be executed only once (for example, inside a service provider).

import { Slugifier } from '@adonisjs/lucid-slugify'

/**
 * Make sure to install the "transliteration" package
 */
import { slugify } from 'transliteration'

Slugifier.slugify = function (value, options) {
  return slugify(value, {
    separator: options.separator,
    lowercase: options.lower,
  })
}

API reference

Entry points

Import pathExports
@adonisjs/lucid-slugifyslugify decorator, Slugifier class, errors
@adonisjs/lucid-slugify/strategiesDbIncrementStrategy, ShortIdStrategy
@adonisjs/lucid-slugify/typesSlugifyConfig, SlugifierConfig, SlugifyStrategyContract

slugify(config)

A property decorator that marks a Lucid model column as a slug. It registers beforeCreate and beforeUpdate hooks on the model to compute and persist the slug.

import { slugify } from '@adonisjs/lucid-slugify'

@slugify({ fields: ['title'], strategy: 'dbIncrement' })
declare slug: string

See Available options for the complete list of accepted config properties.

Slugifier

The class that powers slug generation. You usually do not interact with it directly — the @slugify decorator creates and manages a Slugifier instance per slug field for you.

The one member relevant to end-users is the static slugify method, which converts a string into a URL-safe slug. Override it to plug in a custom slugification routine (see Self creating slugs).

Slugifier.slugify(value: string, options: { separator: string; lower: boolean }): string

SlugifyStrategyContract

The interface that every strategy (built-in or custom) implements.

interface SlugifyStrategyContract {
  /**
   * The number of characters the strategy may add to the slug to make it
   * unique. The slugifier reserves this many characters when applying the
   * `maxLength` option, so the final value stays within the column size.
   */
  maxLengthBuffer: number

  /**
   * Convert the computed slug value into a unique value that is safe to
   * persist to the database.
   */
  makeSlugUnique(modelInstance: LucidRow, field: string, value: string): Promise<string> | string
}

errors

The package exports its exceptions under the errors namespace.

import { errors } from '@adonisjs/lucid-slugify'

errors.E_UNSUPPORTED_DB_DIALECT
ExceptionRaised when
E_UNSUPPORTED_DB_DIALECTThe dbIncrement strategy is used with a database dialect it does not support.

Contributing

One of the primary goals of AdonisJS is to have a vibrant community of users and contributors who believe in the principles of the framework.

We encourage you to read the contribution guide before contributing to the framework.

Code of Conduct

To ensure that the AdonisJS community is welcoming to all, please review and abide by the Code of Conduct.

License

AdonisJS Lucid slugify is open-sourced software licensed under the MIT license.

版本与发布记录

@adonisjs/lucid-slugify 共发布 18 个版本,首次发布于 2018/02/24;过去 12 个月发布了 1 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
4.0.0latest2026/06/254 个月前
3.0.02025/03/091.7 年前
3.0.0-beta.0beta2025/03/091.7 年前
3.0.0-0next2024/03/212 年前
2.2.12022/08/144 年前
2.2.02022/05/224 年前

其他发布通道:legacy → 1.0.3,next → 3.0.0-0,beta → 3.0.0-beta.0。

关于 @adonisjs/lucid-slugify 的常见问题

@adonisjs/lucid-slugify 是什么?

Generate and persist unique slugs in your database via Lucid models

如何安装 @adonisjs/lucid-slugify?

在项目目录运行 npm install @adonisjs/lucid-slugify,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@adonisjs/core(^7.0.0-next.21 || ^7.0.0)、@adonisjs/lucid(^22.0.0-next.1 || ^22.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@adonisjs/lucid-slugify 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@adonisjs/lucid-slugify 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@adonisjs/lucid-slugify 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=24.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@adonisjs/lucid-slugify 可以商用吗?

@adonisjs/lucid-slugify 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@adonisjs/lucid-slugify 的下载量有多少?

根据 npm 官方下载统计,@adonisjs/lucid-slugify 在 2026/10/05 – 2026/11/03 这 30 天内被下载 9,638 次,日均约 321 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@adonisjs/lucid-slugify 还在维护吗?

最新版本 4.0.0 发布于 2026/06/25(4 个月前);至今共发布 18 个版本,其中过去 12 个月发布了 1 个;首次发布于 2018/02/24。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。