ID Generator
Generates unique, prefixed IDs for database records
The withIdGenerator
mixin adds capabilities for generating unique, prefixed IDs for your database records. This is especially useful for systems that need human-readable IDs or where exposing sequential numeric IDs is not desirable.
Usage
API Reference
withIdGenerator()
Enhances a model with ID generation capabilities.
Parameters
model
- The base model to enhanceoptions
- Configuration options for ID generation
IdGeneratorOptions
Added Methods
generateId()
Generates a unique ID with the configured prefix.
insertWithGeneratedId()
Inserts a record with an automatically generated ID.
isGeneratedId()
Checks if an ID matches the pattern of IDs generated by this mixin.
Features
- Customizable Prefixes: Generate IDs with meaningful prefixes (e.g.,
user_
,post_
) - Automatic Generation: Optionally auto-generate IDs for all insert operations
- Custom ID Column: Specify which column should contain the generated IDs
- Unique and Random: Generated IDs are unique and use a cryptographically strong random base