A utility package for generating CUID2 columns in Drizzle ORM
npm install drizzle-cuid2drizzle-cuid2 is a utility package designed to generate CUIDs (Collision-resistant Unique Identifiers) for use with the Drizzle ORM. This library uses the @paralleldrive/cuid2 package to provide unique and efficient IDs for your database entries.
- Installation
- Usage
- Postgres example
- MySQL example
- SQLite example
- Customizing CUID2 Length
- Customizing Prefix
- Issues
- License
- Contributing
- Changelog
Installing with npm:
``bash`
npm install drizzle-cuid2
Installing with yarn:
`bash`
yarn add drizzle-cuid2
Installing with pnpm:
`bash`
pnpm add drizzle-cuid2
Installing with bun:
`bash`
bun add drizzle-cuid2
NOTE: This package requires the drizzle-orm package to be installed in your project.
To use drizzle-cuid2, you will need to import the cuid2 function from the package and use it to define your table columns. The cuid2 function returns a new column definition that can be used with the drizzle-orm package to define your tables.
This package supports the following database types:
`ts
import { pgTable } from 'drizzle-orm/pg-core';
import { cuid2 } from 'drizzle-cuid2/postgres';
export const users = pgTable('doctors', {
id: cuid2('id').defaultRandom().primaryKey(),
// other columns...
});
export const posts = pgTable('posts', {
id: cuid2('id').defaultRandom().primaryKey(),
userId: cuid2('user_id')
.notNull()
.references(() => users.id),
// other columns...
});
`
or
`ts
import { pgTable } from 'drizzle-orm/pg-core';
import { pgCuid2 } from 'drizzle-cuid2';
export const users = pgTable('doctors', {
id: pgCuid2('id').defaultRandom().primaryKey(),
// other columns...
});
export const posts = pgTable('posts', {
id: pgCuid2('id').defaultRandom().primaryKey(),
userId: pgCuid2('user_id')
.notNull()
.references(() => users.id),
// other columns...
});
`
`ts
import { mysqlTable } from 'drizzle-orm/mysql-core';
import { cuid2 } from 'drizzle-cuid2/mysql';
export const users = mysqlTable('doctors', {
id: cuid2('id').defaultRandom().primaryKey(),
// other columns...
});
export const posts = mysqlTable('posts', {
id: cuid2('id').defaultRandom().primaryKey(),
userId: cuid2('user_id')
.notNull()
.references(() => users.id),
// other columns...
});
`
or
`ts
import { mysqlTable } from 'drizzle-orm/mysql-core';
import { mysqlCuid2 } from 'drizzle-cuid2';
export const users = mysqlTable('doctors', {
id: mysqlCuid2('id').defaultRandom().primaryKey(),
// other columns...
});
export const posts = mysqlTable('posts', {
id: mysqlCuid2('id').defaultRandom().primaryKey(),
userId: mysqlCuid2('user_id')
.notNull()
.references(() => users.id),
// other columns...
});
`
`ts
import { sqliteTable } from 'drizzle-orm/sqlite-core';
import { cuid2 } from 'drizzle-cuid2/sqlite';
export const users = sqliteTable('doctors', {
id: cuid2('id').defaultRandom().primaryKey(),
// other columns...
});
export const posts = sqliteTable('posts', {
id: cuid2('id').defaultRandom().primaryKey(),
userId: cuid2('user_id')
.notNull()
.references(() => users.id),
// other columns...
});
`
or
`ts
import { sqliteTable } from 'drizzle-orm/sqlite-core';
import { sqliteCuid2 } from 'drizzle-cuid2';
export const users = sqliteTable('doctors', {
id: sqliteCuid2('id').defaultRandom().primaryKey(),
// other columns...
});
export const posts = sqliteTable('posts', {
id: sqliteCuid2('id').defaultRandom().primaryKey(),
userId: sqliteCuid2('user_id')
.notNull()
.references(() => users.id),
// other columns...
});
`
You can customize the length of the generated CUID2 values using the setLength method. The default length is 24 characters.
`ts
import { pgTable } from 'drizzle-orm/pg-core';
import { cuid2 } from 'drizzle-cuid2/postgres';
export const users = pgTable('doctors', {
// Generate CUID2s with a length of 32 characters
id: cuid2('id').setLength(32).defaultRandom().primaryKey(),
// other columns...
});
`
You can add a prefix to the generated CUID2 values using the setPrefix method. This is useful for namespacing IDs by entity type.
There is not built in delimiter, so make sure to include one if you would like separation between prefix and id.
`ts
import { pgTable } from 'drizzle-orm/pg-core';
import { cuid2 } from 'drizzle-cuid2/postgres';
export const users = pgTable('users', {
// Generate CUID2s with a 'usr_' prefix
id: cuid2('id').setPrefix('usr_').defaultRandom().primaryKey(),
// other columns...
});
export const posts = pgTable('posts', {
// Generate CUID2s with a 'post' prefix, this will have no delimiter. e.g. postn04n62wsow10n3l4huidgkle
id: cuid2('id').setPrefix('post').defaultRandom().primaryKey(),
// other columns...
});
`
You can also combine setPrefix with setLength:
`ts
import { pgTable } from 'drizzle-orm/pg-core';
import { cuid2 } from 'drizzle-cuid2/postgres';
export const users = pgTable('users', {
// Generate CUID2s with a 'usr_' prefix and custom length of 16
id: cuid2('id').setPrefix('usr_').setLength(16).defaultRandom().primaryKey(),
// other columns...
});
``
If you find a bug or have a feature request, please report them in this repository's issues section.
This package is licensed under the MIT license. See the LICENSE file for more information.
If you would like to contribute to this project, please refer to the CONTRIBUTING file for more information.
Please refer to the CHANGELOG file for a complete list of changes.