包详细信息

drizzle-cuid2

Coeeter3.4kMIT2.1.0

A utility package for generating CUID2 columns in Drizzle ORM

drizzle, cuid2, cuid

自述文件

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-cuid2

Installing with yarn:

yarn add drizzle-cuid2

Installing with pnpm:

pnpm add drizzle-cuid2

Installing with bun:

bun add drizzle-cuid2

NOTE: 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...
});

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.

更新日志

drizzle-cuid2

2.1.0

Minor Changes

  • 9be37dc: Refactor MySql, Pg, and SQLite builders to support variable CUID2 lengths and add corresponding tests.

2.1.0

Minor Changes

  • Added support for customizing CUID2 length using the setLength method
  • Updated SQL type definitions to use the configured length

2.0.0

Major Changes

  • 10a0287: Made column names optional to make the package compatible with drizzle-orm's latest version and changed length of columns from 32 to 24

1.0.5

Patch Changes

  • aead402: Added documentation on how to use this package
  • aead402: Cleaned up package.json

1.0.4

Patch Changes

  • c95d2d8: Updated peerdependency of drizzle

1.0.3

Patch Changes

  • 9a413f6: Fixed global import error

1.0.2

Patch Changes

  • 6aa7575: Added global import for cuid functions

1.0.1

Patch Changes

  • bfb47dd: Fixed import error for postgres

1.0.0

  • Initial release of package