The Uuid class provides universally unique identifiers (UUIDs) with support for UUID v4 (random) and UUID v7 (time-ordered) generation.
Import:
Source: value/uuid.ts
Constructor
new Uuid(value)
Create a UUID from an existing value.
Parameters
| Parameter | Type | Description |
|---|---|---|
value | Uuid | string | ArrayBuffer | Uint8Array | Value to create UUID from. |
Examples
Static Methods
Uuid.v4()
Generate a random UUID v4.
Returns
Uuid - Random UUID v4
Example
Uuid.v7()
Generate a time-ordered UUID v7.
Returns
Uuid - Time-ordered UUID v7
Tip
Example
Instance Methods
.toString()
Convert to string representation.
Returns
string - UUID string in standard format (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)
Example
.toJSON()
Serialize for JSON.
Returns
string - UUID string
.toUint8Array()
Get the binary representation as Uint8Array.
Returns
Uint8Array - 16-byte binary representation
Example
.toBuffer()
Get the binary representation as ArrayBuffer.
Returns
ArrayBufferLike - Binary representation
.equals(other)
Check if two UUIDs are equal.
Returns
boolean - True if equal
Complete Examples
Session Management
Time-Ordered Records
Unique Identifiers
API Keys
Distributed System IDs
File Upload Tracking
Parsing and Validation
Batch ID Generation
UUID v4 vs UUID v7
UUID v4 (Random)
Pros: Truly random, no predictability
Cons: Not sortable, no time information
Use when: You need unpredictable, secure IDs
UUID v7 (Time-ordered)
Pros: Sortable, includes timestamp, better for database indexes
Cons: Slightly predictable (timestamp component)
Use when: You need sortable IDs or time-series data
Best Practices
1. Choose the Right Version
2. Use with RecordId
3. Validate User Input
4. Store as UUID Type
See Also
RecordId - Record identifiers
Data Types Overview - All custom data types
Query Builders - Using Uuid in queries
SurrealQL UUID - Database UUID type