在数据组查询中使用数据库函数

gra*_*ton 6 datomic

我正在尝试通过REST API在Datomic中进行"外连接".来自https://github.com/Datomic/day-of-datomic/blob/master/tutorial/social_news.clj我已经采取了最后的例子:

(defn maybe
  "Returns the set of attr for e, or nil if e does not possess
   any values for attr."
  [db e attr]
  (seq (map :a (d/datoms db :eavt e attr))))

;; find all users 
(q '[:find ?e ?upvote
     :where
     [?e :user/email]
     [(user/maybe $ ?e :user/upVotes) ?upvote]]
   (db conn))
Run Code Online (Sandbox Code Playgroud)

我将maybe函数插入到我的数据库中,因此可以查询:

[:find ?n ?v :in $ :where [?e ?a ?v] [?a :db/ident :db/fn] [?e :db/ident ?n]]
Run Code Online (Sandbox Code Playgroud)

回报

:maybe  #db/fn{:code "(seq (map :a (d/datoms db :eavt e attr)))", :params [db e attr], :requires [], :imports [], :lang :clojure}
Run Code Online (Sandbox Code Playgroud)

但是,我无法确定如何在查询中调用该函数.我有:data/user一些关于某些事务的属性,我希望得到它存在的值.这是我正在尝试运行的查询; 我想:maybe成为上面定义的数据库函数.

[:find ?attr ?v ?when ?who :where
 [17592186045423 ?a ?v ?tx true]
 [?a :db/ident ?attr]
 [(:maybe $ ?tx :data/user) ?who]
 [?tx :db/txInstant ?when]]
Run Code Online (Sandbox Code Playgroud)

我很确定我错过了一些非常明显的东西,但我现在已经坚持了一天.谢谢你的帮助!

Bri*_*ian 4

您需要使用 d/invoke。所以你的例子看起来像这样:

[:find ?attr ?v ?when ?who :where
 [17592186045423 ?a ?v ?tx true]
 [?a :db/ident ?attr]
 [(d/invoke $ :maybe ?tx :data/user) ?who]
 [?tx :db/txInstant ?when]]
Run Code Online (Sandbox Code Playgroud)