npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

prisma-slug

v1.0.0-beta.1

Published

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

Downloads

161

Readme

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'

const db = new PrismaClient()

db.use(PrismaSlug())

export default db

In your Prisma schema, define a slug field on models that have a name, and when new records are saved your slug field will be automatically populated.

model User {
  id    Int     @id @default(autogenerated())
  name  String  @unique
  slug  String  @unique
}

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(params) {
      return params.args.data.name ?? params.args.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.

Configuring Slugify Options

To configure slugify, define PrismaSlug's slugify() function and pass in the options like so:

import slugify from 'slugify'

db.use(
  PrismaSlug({
    slugify(value) {
      return slugify(value, { trim: false })
    },
  })
)

Unique Slugs

Customize the slugify() function to keep generating slugs until it finds one that's unique:

import { camelCase } from 'camel-case'
import slugify from 'slugify'

db.use(
  PrismaSlug({
    async slugify(source, params) {
      const method = camelCase(params.model)
      const collection = db[method]
      let slug = slugify(source)
      let attempt = 0

      while ((await collection.count({ where: { slug } })) > 0) {
        attempt += 1
        slug = `${slug}-${attempt}`
      }

      return slug
    },
  })
)

Development

This project uses Yarn Plug'n'Play and Zero-Installs, meaning you don't need to run yarn install to begin developing. However, depending on your editor you may need to install an editor SDK.

To run tests:

yarn test

To run lint checks:

yarn lint

To run type checks:

yarn types

You can build the library by running:

yarn build

And tear it down:

yarn clean

Upon committing, your code will be automatically formatted and your commit message checked to make sure it follows the conventional commits standard. All pull requests have tests, lint checks, formatting checks, type checks, and package security checks run against them.