Options
All
  • Public
  • Public/Protected
  • All
Menu

prisma-slug

prisma-slug

A slugification middleware for Prisma. It generates slugs for your models by using other model attributes with logic that you can define. It's bundled with the excellent slugify package and comes with reasonable defaults to let you define your Prisma schema without worrying about how you're going to generate URL-safe slugs.

Getting Started

Install the library:

yarn add prisma-slug

Then, include it in the file you use to instantiate your Prisma client:

import { PrismaClient } from '@prisma/client'
import { PrismaSlug } from 'prisma-slug'

export const db = new PrismaClient()

db.use(PrismaSlug())

Usage

The PrismaSlug() function outputs a middleware that will convert model fields called name into a URL-safe slug and persist that to a field called slug on the same model. You can customize how this works by passing options into the function. For example, if you wanted to convert title fields as well as name, you'd configure the middleware like so:

db.use(
PrismaSlug({
source(data) {
return data.name ?? data.title
},
})
)

You can also configure the function used to generate the slug:

import slug from 'slug'

db.use(
PrismaSlug({
slugify: slug,
})
)

Both the source() and slugify() functions can return promises as well.

For a full list of options, view the documentation at https://tubbo.github.io/prisma-slug/modules.html#PrismaSlugOptions.

Generated using TypeDoc