【问题标题】:Find entity id of last transaction in Datomic?在 Datomic 中查找最后一笔交易的实体 ID?
【发布时间】:2017-02-24 15:03:55
【问题描述】:

我想知道如何在 Datomic 中找到被最新事务修改/创建/删除的实体的 ID。我该怎么做?

【问题讨论】:

标签: clojure datomic


【解决方案1】:

对于这种读取​​模式(基于时间),您需要使用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)))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-01
    • 2017-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-15
    • 1970-01-01
    • 2013-06-12
    相关资源
    最近更新 更多