Search code examples
clojurekorma

How to select default fields in korma?


Assume you have already done a defdb. I have a table "items" which has two fields, "id" (PK) and "item". I want to set up an entity so that whenever I select it, I only get the entries in "item". Judging by the documentation I thought korma/entity-fields was the way to do this.

(require '[korma.core :as korma])

(korma/defentity items
  (korma/entity-fields :item))

(korma/select items)
;; Wanted: [{:item "foo"}]
;; Received: [{:id 1, :item "foo"}]

I'm using korma 0.3.0-beta7. How can I make korma/select do what I want?


Solution

  • I don't think you can do this from the defentity--the way select works without passing a list of fields explicitly is just select *.

    How about a helper-macro based on korma/select:

    (defmacro select-without-id
      [ent & body]
      `(let [query# (-> (korma/select* ~ent)
                    (korma/fields (vec (:fields ~ent)))
                     ~@body)]
         (korma/exec query#)))