A Prisma extension that automatically adds prefixed IDs to your models. This package allows you to configure custom prefixes for your models and even customize how the IDs are generated.
npm install prisma-prefixed-ids
import { type Prisma, PrismaClient } from "@prisma/client";
import { extendPrismaClient } from 'prisma-prefixed-ids';
type ModelName = Prisma.ModelName;
// NOTE: is your Prisma.ModelName is not available in your setup,
// simply use the following instead:
// type ModelName = string;
// Create your Prisma client
const prisma = new PrismaClient();
// Define your model prefixes
const prefixes: Partial<Record<ModelName, string>> = {
Organization: 'org',
User: 'usr',
// Add more model prefixes as needed
};
// Extend the client with prefixed IDs
const extendedPrisma = extendPrismaClient(prisma, {
prefixes,
});
// Use the extended client
const organization = await extendedPrisma.organization.create({
data: {
name: 'My Organization',
// id will be automatically generated with prefix 'org_'
},
});
console.log(organization.id); // e.g., 'org_abc123...'
You can customize how IDs are generated by providing your own ID generator function:
import { extendPrismaClient } from 'prisma-prefixed-ids';
import { customAlphabet } from 'nanoid';
// Create a custom ID generator
const customIdGenerator = (prefix: string) => {
const nanoid = customAlphabet('1234567890abcdef', 10);
return `${prefix}_${nanoid()}`;
};
const extendedPrisma = extendPrismaClient(prisma, {
prefixes: {
Organization: 'org',
User: 'usr',
},
idGenerator: customIdGenerator,
});
The extension accepts the following configuration:
prefixes
: A record mapping model names to their prefixes (required)idGenerator
: A function that generates IDs (optional, defaults to using nanoid)
The prefixes
configuration is a simple object where:
- Keys are your Prisma model names (case sensitive)
- Values are the prefixes you want to use (without the underscore, which is added automatically)
Example:
const prefixes = {
Organization: 'org',
User: 'usr',
Post: 'post',
Comment: 'cmt',
};
The idGenerator
function should:
- Accept a prefix as its only parameter
- Return a string that will be used as the ID
The default generator uses nanoid with a 24-character length and alphanumeric characters.
This package uses nanoid for ID generation instead of UUID v4 for several reasons:
-
Better Collision Resistance: While UUID v4 has a 122-bit random component, nanoid with 24 characters (using 36 possible characters) provides approximately 128 bits of entropy, making it even more collision-resistant than UUID v4.
-
Smaller Size: A UUID v4 is 36 characters long (including hyphens), while a nanoid with 24 characters is more compact. When combined with a prefix (e.g.,
usr_
), the total length is still shorter than a UUID v4. -
URL-Safe: nanoid uses URL-safe characters by default, making it suitable for use in URLs without encoding.
-
Customizable: nanoid allows you to customize the alphabet and length, giving you more control over the ID format.
-
Better Performance: nanoid is optimized for performance and generates IDs faster than UUID v4.
For example, with a 24-character nanoid:
- The chance of a collision is approximately 1 in 2^128 (same as UUID v4)
- The ID length is 24 characters + prefix length (e.g.,
usr_abc123...
) - The alphabet includes 36 characters (0-9, a-z), making it both readable and compact
MIT