Created
February 27, 2018 23:41
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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