@petbee/prisma-extension-custom-id v0.0.2
Table of Contents
Installation
This module is distributed via npm and should be installed as one of your project's dependencies:
npm install prisma-extensions-custom-id
@prisma/client
is a peer dependency of this library, so you will need to
install it if you haven't already:
npm install @prisma/client
Usage
Extension Setup
To add custom id functionality to your Prisma client create the extension using the createCustomIdExtesion
function and pass it to client.$extends
.
The createCustomIdExtesion
function takes a config object where you can define the models you want to use custom id with.
import { PrismaClient } from '@prisma/client'
const client = new PrismaClient()
const extendedClient = client.$extends(
createCustomIdExtesion({
models: {
Comment: true,
},
})
)
By default the extension will use a id
field of type String
on the model. If you want to use a custom field
name or value you can pass a config object for the model. For example to use a customId
field where the value is null
by default and a nanoid
when the record is created you would pass the following:
const extendedClient = client.$extends(
createCustomIdExtesion({
models: {
Comment: {
field: 'customId',
createValue: (model) => {
if (model) return nanoid()
return null
},
},
},
})
)
The field
property is the name of the field to use for storing the custom id, and the createValue
property is a function that
takes a model argument and returns the value for whether the record has a custom id field. The createValue
method
must return a falsy value if the record should not have a custom id and a truthy value if it should have.
It is possible to custom ids for multiple models at once by passing a config for each model in the models
object:
const extendedClient = client.$extends(
createCustomIdExtesion({
models: {
Comment: true,
Post: true,
},
})
)
To modify the default field and type for all models you can pass a defaultConfig
:
const extendedClient = client.$extends(
createCustomIdExtesion({
models: {
Comment: true,
Post: true,
},
defaultConfig: {
field: 'customId',
createValue: (model) => {
const customPrefix = 'cus_'
if (model) return `${customPrefix}${nanoid()}`
return null
},
},
})
)
When using the default config you can also override the default config for a specific model by passing a config object for that model:
const extendedClient = client.$extends(
createCustomIdExtesion({
models: {
Comment: true,
Post: {
field: 'id',
createValue: (model) => {
if (model) return nanoid()
return null
},
},
},
defaultConfig: {
field: 'customId',
createValue: (model) => {
const customPrefix = 'cus_'
if (model) return `${customPrefix}${nanoid()}`
return null
},
},
})
)
Prisma Schema Setup
The Prisma schema must be updated to include the new id field for each model you want to use custom id with.
For models configured to use the default field and type you must add the id
field to your Prisma schema manually.
Using the Comment model configured in Extension Setup you would need add the following to the
Prisma schema:
model Comment {
id String @default("custom-id")
[other fields]
}
If the Comment model was configured to use a customId
field where the value is null by default and a Int
when
the record has as custom id you would need to add the following to your Prisma schema:
model Comment {
customId Int?
[other fields]
}
Behaviour
The main behaviour of the extension is to replace crate operations with update operations that set the custom id field to the generated value.
LICENSE
Apache 2.0
2 years ago