Field transformations
As databases typically store only basic types, such as integers and strings, it's not always convenient to keep the same simplicity on Data Access Object (DAO) level.
For example, you might need to parse JSON from a VARCHAR
column, or retrieve values from a cache based on data from the database. In such cases, the preferred approach is to use column transformations.
Example: Defining an Unsigned Integer field
Suppose that you want to define an unsigned integer field on an entity, but Exposed doesn't have such column type yet. You can achieve this by using the following implementation:
The transform
function accept two lambdas that convert values to and from the original column type. In this case, you make sure to store only UInt
instances in the uint
field.
Although it is still possible to insert or update values with negative integers via DAO, this approach assures a cleaner business logic.