Skip to content

Custom type conversions

Jacek Hełka edited this page Nov 9, 2024 · 1 revision

Our models contains sometimes values, that are not directly convertible to database-compatible values. To handle such values, DbFun allows to definecustom conversions.

Consider one-case union, adding some business value to primitive type, e.g.:

type EmailAddress = EmailAddress of string

The QueryConfig.AddParamaterConverter method allows to convert value used as a parameter:

let config = config.AddParameterConverter (fun (EmailAddress s) -> s)

When reading from database, string column value can be converted back to EmailAddress using QueryConfig.AddRowConverter:

let config = config.AddRowConverter(fun s -> EmailAddress s)

or shorter:

let config = config.AddRowConverter(EmailAddress)

The same mechanism can also be used to convert more complex structures, like records or collections.

Clone this wiki locally