3
我想知道如何找到由Datomic中的最新事務修改/創建/刪除的實體的ID。我怎樣才能做到這一點?在Datomic中查找最後一筆交易的實體ID?
我想知道如何找到由Datomic中的最新事務修改/創建/刪除的實體的ID。我怎樣才能做到這一點?在Datomic中查找最後一筆交易的實體ID?
對於這種讀取模式(基於時間的),您需要使用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)))
您可以使用['datomic.api/history'](http://docs.datomic.com/clojure/index.html#datomic.api/歷史)爲此。 –