Skip to content

ID Generator

The ID Generator plugin provides unique identifier generation with multiple strategies for different use cases.

Four adapters are available:

  • TimestampIdGenerator generates microsecond-based IDs (development/testing).
  • UuidV4Generator generates random UUIDs (distributed systems, no ordering).
  • UuidV7Generator generates timestamp-prefixed UUIDs (recommended for production).
  • UlidGenerator generates compact, URL-safe identifiers (public APIs).

Why Use It

Entities require unique identifiers. The ID Generator provides different strategies optimized for various scenarios—chronological ordering for database performance, compact format for URLs, or pure randomness for distributed systems.

Usage

Generate IDs in services and handlers:

$id = $this->idGenerator->generate();
$book = new Book(id: $id, ...);

Reconstruct IDs from stored values:

$id = $this->idGenerator->from('019aab5a-19a5-701c-8a23-d5379acbf015');

Generator Comparison

Generator Format Length Ordered Use Case
TimestampId Integer 16 digits Yes Dev/test
UuidV4 UUID 36 chars No Distributed, no ordering
UuidV7 UUID 36 chars Yes Production default
Ulid Base32 26 chars Yes Public APIs, compact URLs

UUID v7 is recommended for production as it provides chronological ordering (better database index performance) with collision resistance.

Testing

Any generator can be used in tests. For deterministic IDs, the from() method reconstructs IDs from known values:

$id = $this->idGenerator->from('test-id-123');

See Also