I would like to be able to pass a user-defined array of fields which contains a list of all the columns that need fetching. Can korma/fields take an array of columns?
What I am essentially wanting to create is something like this:
(defn fetch [fields]
(->
(korma/select* foo)
(as-> query
(if (not-empty? fields)
(korma/fields query fields)
query))
(korma/select)))
I am assuming this is because I have specified entity-fields in defentity. I am therefore wondering, if it is possible to override the entity-fields specified in
defentity
?
I think your assumption is correct, and it's also possible to override this. If you look at the map generated by (defentity foo)
it has a :fields
key with all the fields. korma.core/entity-fields
doesn't replace what's already there, but this will:
(-> foo
(assoc :fields [:first])
(korma/select*)
(korma/as-sql))
=> "SELECT \"foo\".\"first\" FROM \"foo\""
I would like to be able to pass a user-defined array of fields which contains a list of all the columns that need fetching.
(defn fetch [& fields]
(-> (korma/select* foo)
(as-> query
(if (seq fields)
(assoc query :fields fields)
query))
(korma/select)))
I used variadic args in this example to mirror korma.core/entity-fields
(and made fetch
return the SQL string for my testing rather than execute the query):
(fetch :first :last)
=> "SELECT \"foo\".\"first\", \"foo\".\"last\" FROM \"foo\""
(fetch)
=> "SELECT \"foo\".\"id\", \"foo\".\"first\", \"foo\".\"last\" FROM \"foo\""