Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dogenpunk/3911fb00238c618f9a105974198f8cad to your computer and use it in GitHub Desktop.
Save dogenpunk/3911fb00238c618f9a105974198f8cad to your computer and use it in GitHub Desktop.
Inspect a datomic entity's history
;; Show history of an entity
;;
;; useful for interactively inspecting what happened to a datomic entity in its lifetime
;;
;; use `entity-history` to get a list of transactions that have touched the entity (assertions, retractions)
;;
;; use `explain-tx` to find out what else was transacted in the txs
(defn entity-history
"Takes an entity and shows all the transactions that touched this entity.
Pairs well with clojure.pprint/print-table"
[db eid]
(->> eid
(d/q
'[:find ?e ?a ?v ?tx ?added
:in $ ?e
:where
[?e ?a ?v ?tx ?added]]
(datomic.api/history db))
(map #(->> %
(map vector [:e :a :v :tx :added])
(into {})))
(sort-by :tx)))
(defn pretty-datom [d] (into {} (map (fn [x] [x (x d)]) [:e :a :v :tx :added])))
(defn resolve-attr [db {:keys [a] :as datom}]
(assoc datom
:a
(->> a (d/entity db) :db/ident)))
(defn explain-tx
"Takes a tx id and returns a prettified list of all datoms contained in the
transaction. N.b. takes a conn, not a db.
Pairs well with clojure.pprint/print-table"
[conn tx]
(->> (d/tx-range (d/log conn) tx nil)
first
:data
(map pretty-datom)
(map (partial resolve-attr (d/db conn)))
(sort-by :e)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment