drizzle-cuid2
v2.2.0
Published
A utility package for generating CUID2 columns in Drizzle ORM
Readme
drizzle-cuid2
drizzle-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.
Table of Contents
Installation
Installing with npm:
npm install drizzle-cuid2Installing with yarn:
yarn add drizzle-cuid2Installing with pnpm:
pnpm add drizzle-cuid2Installing with bun:
bun add drizzle-cuid2NOTE: This package requires the drizzle-orm package to be installed in your project.
Usage
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:
Postgres example:
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
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...
});MySQL example:
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
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...
});SQLite example:
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
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...
});Customizing CUID2 Length
You can customize the length of the generated CUID2 values using the setLength method. The default length is 24 characters.
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...
});Customizing Prefix
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.
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:
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...
});Issues
If you find a bug or have a feature request, please report them in this repository's issues section.
License
This package is licensed under the MIT license. See the LICENSE file for more information.
Contributing
If you would like to contribute to this project, please refer to the CONTRIBUTING file for more information.
Changelog
Please refer to the CHANGELOG file for a complete list of changes.
