Elegant: Accessors & Mutators
Overview
Elegant provides powerful mechanisms to transform attribute values as they're read from or written to your database. These features enable you to:
Automatically encrypt sensitive data during storage and decrypt it on retrieval
Convert database strings into structured data types like arrays or objects
Format values consistently across your application
Implement custom business logic for attribute handling
Understanding Accessors
Accessors modify attribute values when you retrieve them from a model instance. They act as intermediaries between the raw database value and the value your application code receives. To create an accessor, define a protected method on your model class. The method name should use camelCase formatting to represent the underlying database column name.
Example: Creating a Name Accessor
When you access user.name, Elegant automatically invokes this accessor, applying the appropriate title prefix based on the user's gender attribute.
Accessing Transformed Values:
The accessor executes transparently whenever you reference the attribute. Your application code works with the transformed value while the raw data remains unchanged in the database.
Understanding Mutators
Mutators work in the opposite direction—they transform attribute values before saving them to the database. This ensures data consistency and can enforce formatting rules at the model level.
Example: Creating a First Name Mutator
In this example, the firstName method receives a configuration object containing a mutator function. You call mutator with a transformation function that takes the current value and modifies it. This function executes whenever the firstName attribute is accessed or modified.
How It Works:
The transformation function receives the attribute's value as its parameter
Your logic determines what transformation to apply
The mutated value becomes what your application sees
In this case, any user with the first name "Williams" will display as "Bill"
This pattern provides a clean, functional approach to attribute transformation that integrates seamlessly with Elegant's TypeScript-based architecture.
Combining Accessors and Mutators
You can define both transformations for the same attribute, creating a complete bidirectional transformation pipeline:
This approach keeps related transformation logic organized in one place. The mutator stores values in lowercase, while the accessor capitalizes them on retrieval, ensuring consistent data storage and presentation formatting.