Column transformation
Column transformations allow you to define custom transformations between a database column's types and your application's data types. This can be particularly useful when you need to store data in one format but work with it in a different format within your application.
Basic usage
To apply custom transformations to a table column, use the .transform() function.
The transformation consists of two operations:
The
wrap()function converts a value read from the database into the type used by your application.The
unwrap()function converts an application value into the type used by the database for storage.
In the following example, the Meals table stores meal times as LocalTime values but exposes them in the application as Meal values:
Within the .transform() function:
The
wrap()function transforms the storedLocalTimevalues intoMealenums. It checks the hour of the stored time and returns the corresponding meal type.The
unwrap()function transformsMealenums back intoLocalTimevalues for storage in the database.
Reusing a transformation
For transformations that are used by multiple columns or require more complexity, you can define a custom ColumnTransformer and pass it as an argument to the .transform() function:
Null transform
The .nullTransform() function applies a transformation that allows a non-nullable database column to be represented as a nullable column in Kotlin.
This transformation does not change the column's definition in the database and remains non-nullable. Instead, it allows the transformation to map particular non-null database values to null in the application. This can be useful for converting an empty string from a non-nullable text column, empty lists, or negative IDs, to null.
When an
00:00value is read from the database, it is converted tonull.When
Meal.BREAKFAST,Meal.LUNCH, orMeal.DINNERis written, the value is converted to the correspondingLocalTime.When
nullis written, the value is converted back to00:00.
Non-null transform
The .nonNullTransform() function allows a nullable database column to be represented as a non-nullable Exposed column.
This is useful when the database allows null, but your application has a meaningful non-null representation for that value. The transformation is responsible for converting null database values into an appropriate non-null application value and for converting that value back when writing to the database.
In the following example, an UNSCHEDULED value is added to the Meal enum to represent a missing meal time:
When
nullis read from the database, the value is converted toMeal.UNSCHEDULED.When
Meal.BREAKFAST,Meal.LUNCH, orMeal.DINNERis written, the value is converted to the correspondingLocalTime.When
Meal.UNSCHEDULEDis written, the value is converted back tonull.
The database column remains nullable, while the application uses a non-nullable Column<Meal>.