Generate your first ID with uniku in under 5 minutes.
This guide will walk you through generating your first ID, validating it, and converting it to bytes. We’ll use UUID v7 as it’s the most common use case for database primary keys.
1
Import the generator
Import the uuidv7 function from the dedicated entry point:
import { uuidv7 } from 'uniku/uuid/v7'
Each ID format has its own entry point (e.g., uniku/uuid/v4, uniku/ulid, uniku/nanoid). This ensures optimal tree-shaking.
2
Generate your first ID
Call the function to generate a UUID v7 string:
const id = uuidv7()console.log(id)// => "018e5e5c-7c8a-7000-8000-000000000000"
UUID v7 IDs are time-ordered and lexicographically sortable:
import { uuidv7 } from 'uniku/uuid/v7'// PostgreSQL example with binary storageconst id = uuidv7()const bytes = uuidv7.toBytes(id)await db.execute( 'INSERT INTO users (id, name) VALUES ($1, $2)', [bytes, 'Alice'])// Retrieve and convert backconst result = await db.query('SELECT id FROM users WHERE name = $1', ['Alice'])const retrievedId = uuidv7.fromBytes(result.rows[0].id)console.log(retrievedId) // Same as original ID
When storing UUIDs as binary in PostgreSQL, use the BYTEA type, not the UUID type. The UUID type expects hyphenated strings.