我想知道如何找到Datomic中最新交易修改/创建/删除的实体的ID。我怎么能这样做?
答案 0 :(得分:2)
对于这种阅读模式(基于时间),您将要使用Log API。请注意:
以下是一个示例实现:
(defn affected-entities
"Given a Datomic connection, returns the set of entity ids that were affected
by the last transaction (in e position), excluding the entity representing the
transaction itself."
[conn]
(let [db (d/db conn)]
(->>
(d/q '[:find [?e ...] :in ?log ?t1 ?t2 :where
[(tx-ids ?log ?t1 ?t2) [?tx ...]] ;; binds the last tx
[(tx-data ?log ?tx) [[?e]]]]
(d/log conn) (d/basis-t db) (d/next-t db))
;; filtering out the transaction entity
(remove (fn [eid]
(->> eid d/part (d/ident db) (= :db.part/tx))))
set)))