تولیدکننده CUID
تولید IDهای یکتای مقاوم در برابر تداخل (CUID v1)
CUIDهای تولیدشده
CUID v1 فرمت قدیمی است. برای پروژههای جدید از CUID2 استفاده کنید.
What is CUID?
CUID (Collision-Resistant Unique Identifier) is an open-source algorithm for generating unique IDs that work well in distributed systems without a central coordinator. Unlike a simple UUID, a CUID is designed to be horizontally scalable — multiple servers or browser tabs can each generate IDs independently with minimal risk of collision.
Every CUID starts with the lowercase letter c, making the format immediately recognisable at a glance. The remaining characters are all lowercase alphanumeric (base36), so a CUID is URL-safe and can be used directly as a URL path segment or database primary key without additional encoding.
The original CUID specification (v1) was created by Eric Elliott and popularised via the cuid npm package. It is now superseded by CUID v2, which provides cryptographic security. This page — and the generator above — produces CUID v1 IDs, the classic format still widely encountered in production codebases.
CUID Structure
A CUID v1 is roughly 25 characters long and is composed of five concatenated segments, each carrying a different type of entropy:
The segments are simply concatenated — there are no separators. The total length varies slightly depending on the current timestamp value but stays at approximately 25 characters.
How CUID Prevents Collisions
Collision resistance comes from layering independent sources of entropy so that even in worst-case scenarios (thousands of IDs generated per millisecond across many machines) the probability of two identical IDs remains vanishingly small.
CUID vs UUID v4
Both CUID and UUID v4 are widely used for client-side ID generation. They take different approaches to the same problem:
| Feature | CUID v1 | UUID v4 |
|---|---|---|
| Format | c + base36 (~25 chars) | hex groups (36 chars with dashes) |
| Sortable | Roughly (timestamp prefix) | No |
| URL-safe | Yes (alphanumeric only) | Mostly (dashes are fine in URLs) |
| Collision resistance | High — timestamp + counter + fingerprint + random | High — 122 bits of random |
| Predictability | Partially (timestamp visible) | None (pure random) |
| Length | ~25 characters | 36 characters |
| Requires coordination | No | No |
UUID v4 is the safer choice for security-sensitive scenarios because it reveals no timing information. CUID has the edge when you want IDs that are vaguely sortable, shorter, and free of hyphens — handy for use in URLs, filenames, or logs where you want to quickly identify when a record was created.
CUID v1 vs CUID2
The CUID specification has been revised significantly. Understanding the differences helps you pick the right version for your project:
| Aspect | CUID v1 | CUID v2 |
|---|---|---|
| Algorithm | Deterministic components | SHA-3 based, fully opaque |
| Cryptographic | No | Yes |
| Timestamp visible | Yes | No |
| Format | Starts with "c" | Starts with "c" (configurable) |
| npm package | @paralleldrive/cuid (deprecated) | @paralleldrive/cuid2 |
| Length | ~25 chars | 24 chars (default, configurable) |
For new projects, CUID v2 is the recommended choice. Its SHA-3 based construction means the output is opaque — no timestamp, counter, or fingerprint can be reverse-engineered from the ID. Use CUID v1 only when you need backward compatibility with an existing dataset or want a dependency-free implementation.
Use Cases
@id strategy for String primary keys — @default(cuid()) — making it one of the most widely deployed ID formats in the JavaScript ecosystem.Code Examples
Install the official CUID v2 package (recommended) or write a minimal v1 implementation with no dependencies:
// npm install @paralleldrive/cuid2 (recommended — CUID v2)
import { createId } from '@paralleldrive/cuid2'
const id = createId()
// → 'tz4a98xxat96iws9zmbrgj3a'
// Custom length
import { init } from '@paralleldrive/cuid2'
const createShortId = init({ length: 10 })
createShortId() // → 'zxp1l6mf4c'If you prefer a dependency-free Node.js implementation of the v1 algorithm:
// Pure Node.js — CUID v1 style (no dependencies)
let counter = 0
function pad(str, size) {
return str.padStart(size, '0').slice(-size)
}
function fingerprint() {
const os = require('os')
const source = [process.pid, os.hostname().length].join('')
let hash = 0
for (const c of source) {
hash = ((hash << 5) - hash) + c.charCodeAt(0)
hash |= 0
}
return pad(Math.abs(hash).toString(36), 4)
}
function cuid() {
const timestamp = Date.now().toString(36)
const cnt = pad((counter++ & 0xffff).toString(36), 4)
const fp = fingerprint()
const rnd = pad(Math.floor(Math.random() * 0xffffffff).toString(36), 4)
+ pad(Math.floor(Math.random() * 0xffffffff).toString(36), 4)
return 'c' + timestamp + cnt + fp + rnd
}
console.log(cuid()) // → 'clrc4gkwz001ag2hs3k7f9m2q'Using CUID as a database primary key with Prisma and PostgreSQL:
-- Use CUID as a primary key in PostgreSQL
CREATE TABLE users (
id TEXT PRIMARY KEY DEFAULT gen_cuid(),
name TEXT NOT NULL,
created_at TIMESTAMPTZ DEFAULT now()
);
-- Prisma schema (auto-generates CUID by default)
model User {
id String @id @default(cuid())
name String
createdAt DateTime @default(now())
}