Liking cljdoc? Tell your friends :D

Coercion

HoneyEQL converts values as they cross the boundary between the application and the database.

There are two coercion directions:

application
    ↓
  :to-db
    ↓
database

database
    ↓
 :from-db
    ↓
application

HoneyEQL provides default coercion based on the database column type.

While retrieving data from the database, HoneyEQL coerces returned values to the corresponding JVM types described below.

TypePostgresMySQL
java.lang.Longinteger, int, int2 int4, smallint, smallserial, serial, serial2, serial4, bigint,int8,bigserial,serial8SMALLINT, MEDIUMINT, INT, TINYINT UNSIGNED, SMALLINT UNSIGNED, MEDIUMINT UNSIGNED, YEAR, INT UNSIGNED, BIGINT
java.math.BigDecimalreal, float4, float8, double precision,numeric,decimalREAL, FLOAT, DOUBLE, DECIMAL, NUMERIC
java.lang.Stringbit, bit varying, char, character varying, varchar, citext, bpchar, macaddr8, text, moneyCHAR, VARCHAR, TINYTEXT, TEXT, MEDIUMTEXT, LONGTEXT, ENUM, SET, BINARY, VARBINARY, TINYBLOB, BLOB, LONGBLOB, BIT
java.lang.BooleanbooleanTINYINT(1), BIT(1)
java.util.UUIDuuid--
java.time.LocalDatedateDATE
java.time.LocalTimetime, time without time zoneTIME
java.time.OffsetTimetimetz, time with time zone--
java.time.LocalDateTimetimestamp, timestamp without time zoneDATETIME, TIMESTAMP
java.time.OffsetDateTimetimestamptz, timestamp with time zone--

Custom Attribute Coercion

An application may override the default coercion for a specific HoneyEQL attribute.

Configure attribute coercions when initializing the database adapter:

(def db-adapter
  (heql-db/initialize
   db-spec
   {:attr/coercions
    {:certificate/item-type
     {:to-db name
      :from-db keyword}}}))

In this example, the application represents :certificate/item-type as a keyword while the database stores it as text.

Writing:

(mutation/insert!
 db-adapter
 #:certificate{:item-type :ssl})

passes the value through:

:ssl
  ↓ :to-db
"ssl"
  ↓
database

Reading:

(heql/query-single
 db-adapter
 {[:certificate/id 1]
  [:certificate/item-type]})

passes the database value through:

"ssl"
  ↓ :from-db
:ssl

and returns:

#:certificate{:item-type :ssl}

The same :to-db coercion is also used when an attribute value participates in a query condition.

For example:

(heql/query
 db-adapter
 {[:certificate/item-type :ssl]
  [:certificate/id]})

coerces :ssl using the configured :to-db function before producing the SQL parameter.

This keeps the attribute's application representation consistent across both mutations and queries.

Coercion Precedence

For each attribute and direction:

custom attribute coercion configured?
        │
        ├── yes → use custom coercion
        │
        └── no  → use HoneyEQL default coercion

Custom coercion is attribute-specific.

Configuring coercion for:

:certificate/item-type

does not change the behavior of other text columns.

The :to-db and :from-db functions are independent. An application may override only one direction when that is sufficient.

Can you improve this documentation?Edit on GitHub

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
←Move to previous article
→Move to next article
Ctrl+/Jump to the search field
× close