clojurekorma

Can I specify default field aliases in Korma?


I have a MySQL field called thing_id, but I want to reference it as :thing-id in my code. I can define an entity like this:

(defentity thing
  (entity-fields :id [:thing_id :thing-id]))

so that when I fetch things:

(select thing)

The MySQL field that contains an underscore is transformed:

[{:id 1 :thing-id 2}]

But I can't select with the aliased:

(select thing (where (= :thing-id 2)))

gives

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException
Unknown column 'thing.thing-id' in 'where clause'

I can fix it in each where call:

(select thing (where (= :thing_id 2)))

But I was hoping that the alias works both way. It doesn't appear to. Is there a way to set an alias that can be used in a select?


Solution

  • Bit too late, but here it goes...

    Use a naming strategy, more here. So your spec should look like:

    ...
    (:require [camel-snake-kebab.core :refer [->kebab-case ->snake_case]])
    ...
    
    (def db-spec
      {:classname "com.mysql.jdbc.Driver"
       :subprotocol "mysql"
       :delimiters "`"
       :unsafe true
       :subname db-subname
       :user db-user
       :password db-password
       :naming {:keys ->kebab-case
                :fields ->snake_case}})