Global ID
Adds global ID functionality for GraphQL-friendly identifiers
The withGlobalId
mixin provides global ID functionality for models, enabling easy integration with GraphQL APIs and systems that require globally unique identifiers. This mixin follows the pattern of prefixing local IDs with a type name (e.g., User_123
).
Usage
API Reference
withGlobalId()
Enhances a model with global ID functionality.
Parameters
model
- The base model to enhanceidColumnName
- Name of the ID columntype
- Type prefix for global IDs (defaults to table name in uppercase)
Returns
Enhanced model with global ID methods.
Added Methods
getGlobalId()
Generates a global ID from a local ID by adding the type prefix.
getLocalId()
Extracts the local ID from a global ID by removing the type prefix.
parseGlobalId()
Parses a global ID, returning just the ID portion or null if invalid.
findByGlobalId()
Finds a record by its global ID.
getByGlobalId()
Gets a record by its global ID, throwing an error if not found.
findByGlobalIds()
Finds multiple records by their global IDs.
Utility Functions
The mixin also exports these standalone utility functions:
decodeFromGlobalId()
Decodes a global ID into its type and ID components.
decodeTypeFromGlobalId()
Extracts just the type portion from a global ID.