prisma-extension-cuid2
TypeScript icon, indicating that this package has built-in type declarations

1.1.1 • Public • Published

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.

Readme

Keywords

Package Sidebar

Install

npm i prisma-extension-cuid2

Weekly Downloads

27

Version

1.1.1

License

MIT

Unpacked Size

51.6 kB

Total Files

37

Last publish

Collaborators

  • nrdobie