clojure - 在 Datomic 中查找最后一笔交易的实体 ID?

标签 clojure datomic

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

最佳答案

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

关于clojure - 在 Datomic 中查找最后一笔交易的实体 ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42441862/

相关文章:

clojure - 在 Clojure 中返回表达式序列中非最后一个表达式的值的惯用方法

clojure - 在结果元组中检索实体 ID 时,Datomic 未返回正确的 "min"结果

clojure - 如何在 Datomic 中收回属性?

clojure - 使用 Datomic 返回 Map 而不是 Vector 时出错

clojure - 有没有更好的方法来映射除第一项之外的所有项?

pointers - Clojure 有变量引用吗?

clojure - not 子句在 Datomic 中如何工作?

clojure - 如何使用枚举/引用的实体 id 执行 Datomic 事务?

java - 如何使用 clojure.java.jdbc 从 postgresql 查询 JSON 数据类型?

exception - 使用 Clojure 处理异常