In analytics, we often need to ask questions regarding an entity’s time, for example:
- when is the entity first seen in the system?
- when is the entity last touched?
In old-school sql, we might setup columns such as created_at or updated_at with trigger to ensure the values are accurate or an ORM might manage that for us. However, Datomic has a native history API that can help us answer the questions.
(defn created-at [db eid]
(let [hist (d/history db)]
(ffirst (d/q {:query '[:find (min ?inst)
:in $ ?e
:where [?e _ _ ?tx true]
[?tx :db/txInstant ?inst]]
:args [hist eid]}))))
(defn updated-at [db eid]
(ffirst (d/q {:query '[:find (max ?inst)
:in $ ?e
:where [?e _ _ ?tx]
[?tx :db/txInstant ?inst]]
:args [db eid]})))))
Is there anyway I can expose them as a column in Analytics?