korma/fields 可以取一个列数组吗?

Can korma/fields take an array of columns?

我希望能够传递一个用户定义的字段数组,其中包含需要获取的所有列的列表。 korma/fields 可以采用列数组吗?

我本质上想要创建的是这样的东西:

(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?

我认为你的假设是正确的,也可以覆盖它。如果您查看由 (defentity foo) 生成的地图,它有一个包含所有字段的 :fields 键。 korma.core/entity-fields 不会 替换 已经存在的内容,但这将:

(-> 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)))

我在这个例子中使用可变参数来镜像 korma.core/entity-fields(并使 fetch return 成为 SQL 字符串用于我的测试而不是执行查询):

(fetch :first :last)
=> "SELECT \"foo\".\"first\", \"foo\".\"last\" FROM \"foo\""
(fetch)
=> "SELECT \"foo\".\"id\", \"foo\".\"first\", \"foo\".\"last\" FROM \"foo\""