In Datomic, querying field history with no retractions?

Viewed 83

I'd like to get the history of values for a particular field in Datomic. My intuition is to use (d/history) like

(d/q '[:find ?entity ?field-val ?date ?tx
       :in $
       :where 
          [?entity :namespace/field ?field-val ?tx]
          [?tx :db/txInstant ?date]]
      (d/history (db/get-db)))

However, this query will duplicate most values because it lists every retraction as well as every value update (every db/add and db/retract).

I thought maybe I could query the datoms with the transaction, then check the operations. But I can't find a way to query the datoms.

  • (d/pull db '[*] tx-id) doesn't include datoms.
  • search engine results were not helpful for keywords like "query datomic transaction datoms"
  • searching for datomic transaction schema is not fruitful

I can use tx-range, but that seems unweildly.

Any better approaches?

1 Answers

I was looking in the wrong place. History queries offer an extra hidden positional value described in the history docs.

So any where clause can include ?entity ?attribute ?value ?transaction ?operation.

  • ?operation is true for :db/add and false for :db/retract

So, the query I want looks like

(d/q '[:find ?entity ?field-val ?date ?tx
       :in $
       :where 
          [?entity :namespace/field ?field-val ?tx true] ;;ADDED TRUE
          [?tx :db/txInstant ?date]]
      (d/history (db/get-db)))
Related