2017-02-24 40 views
3

我想知道如何找到由Datomic中的最新事务修改/创建/删除的实体的ID。我怎样才能做到这一点?在Datomic中查找最后一笔交易的实体ID?

+0

您可以使用['datomic.api/history'](http://docs.datomic.com/clojure/index.html#datomic.api/历史)为此。 –

回答

2

对于这种读取​​模式(基于时间的),您需要使用Log API。请注意:

  1. 可能有多个实体受上次事务影响。
  2. 实际上,交易本身是由为该交易创建的实体表示的,您可能希望将该实体过滤出结果。

这里是一个示例实现:

(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))) 
相关问题