Skip to content

Instantly share code, notes, and snippets.

@favila
Created February 27, 2018 23:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save favila/dfca941c46ab66ae55d2935c2348d04e to your computer and use it in GitHub Desktop.
Save favila/dfca941c46ab66ae55d2935c2348d04e to your computer and use it in GitHub Desktop.
datomic query to return the tx at which an entity or its components were last touched
(require '[datomic.api :as d])
(defn last-touched-tx
"Return the tx of the oldest assertion on `entity` or any of its components."
[db entity]
(or (d/q '[:find (max ?tx) .
:in $ % ?root
:where
(component-entities ?root ?ce)
(union ?root ?ce ?e)
[?e _ _ ?tx]]
db
'[[(component-attr? [?a])
[?a :db/isComponent true]
[(datomic.api/entid $ :db.type/ref) ?reftype]
[?a :db/valueType ?reftype]]
[(rebind [?a] ?b)
[(identity ?a) ?b]]
[(union [?a ?b] ?c)
(rebind ?a ?c)]
[(union [?a ?b] ?c)
(rebind ?b ?c)]
[(component-entities [?e] ?ce)
[?e ?a ?ce]
(component-attr? ?a)]
[(component-entities [?e] ?ce)
[?e ?a ?r]
(component-attr? ?a)
(component-entities ?r ?ce)]]
(d/entid db entity))
0))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment