### Exposed 0.56.0 Help

# 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:

object TableWithUnsignedInteger : IntIdTable() { val uint = integer("uint") } class EntityWithUInt : IntEntity() { var uint: UInt by TableWithUnsignedInteger.uint.transform({ it.toInt() }, { it.toUInt() }) companion object : IntEntityClass<EntityWithUInt>() }

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.

Note that these transformations will occur with every access to a field. Therefore, to maintain performance efficiency, it is advisable to avoid heavy transformations in this context.

Last modified: 30 October 2024