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-extension-cuid2

v1.1.1

Published

Enables the use of CUID2 as the default ID type for Prisma models

Downloads

1,677

Readme

prisma-extension-cuid2

Enables using cuid2 as a default value for id fields in Prisma.

Limitations

This package does not attempt to modify the Prisma engine to allow for the use of @default(cuid2()) but instead utilizes PrismaClient.$extends to add some middleware that will update the id fields to CUID2 values before they are saved to the database.

Installation

Install the package using your package manager of choice:

npm install --save prisma-extension-cuid2
# or
yarn add prisma-extension-cuid2
# or
pnpm add prisma-extension-cuid2

Define your Prisma schema as usual using @default(cuid()) for the id fields:

model SingleId {
  id String @id @default(cuid())
}

model DualId {
  id1 String @id @default(cuid())
  id2 String @default(cuid())

  @@unique([id2])
}

Then when initializing your PrismaClient, extend it with the cuid2 middleware and provide the fields you want to use CUID2 for:

import { PrismaClient } from '@prisma/client'
import cuid2Extension from 'prisma-extension-cuid2'

const prisma = new PrismaClient().$extend(cuid2Extension({
  fields: ['SingleId:id', 'DualId:id1', 'DualId:id2']
}))

export default prisma

By default if you don't specify the fields or includeFields options, the extension will use the *:id pattern to apply the extension which can cause issues, see the options section for more information.

Options

fields (recommended)

Specify the fields to apply the extension to. This option takes in an array of ModelName:FieldName strings. This is the recommended way to use the extension, as it provides the most safety and control.

cuid2Extension({
  fields: ['SingleId:id', 'DualId:id1', 'DualId:id2']
})

includeFields and excludeFields

If your schema is large and has a fairly standard format for models, you can use the includeFields and excludeFields options instead of specifying each field individually. These options take in an array of ModelName:FieldName strings, with includeFields supporting wildcard model names and excludeFields supporting wildcard field names.

DANGER: Due to how Prisma generates code, this extension does not have a way to know which fields are on any given model. The extension will attempt to set the include fields on every model that matches regardless of whether the field exists. This will cause runtime errors if you are not careful.

// Changing the default field name from `id` to `cuid`
cuid2Extension({
  includeFields: ['*:cuid']
})

// Excluing a specific field from the extension
cuid2Extension({
  excludeFields: ['SingleId:id']
})

// Excluding a model from the extension, like a join table
cuid2Extension({
  excludeFields: ['AuthorsToPosts:*']
})

cuid2Options

If you want to customize the CUID2 generation, you can pass in options that will be used when initializing the CUID2 generator. The options are passed directly to the cuid2 package.

cuid2Extension({
  cuid2Options: {
    fingerprint: process.env.DEVICE_ID
  }
})

See the CUID2 documentation for more information on the available options.