zcaudate
zcaudate

Reputation: 14258

how to get the datetime of the last transaction in a datomic db?

I want to find the most recent transaction made to a connection. The following does not seem to give the correct date:

(require '[datomic.api :as datomic])

(-> conn datomic/db datomic/basis-t datomic/t->tx (java.util.Date.))

Upvotes: 3

Views: 1702

Answers (3)

nikolayandr
nikolayandr

Reputation: 190

Should be much easier

 (d/q
     '[:find (max 1 ?tx)
       :where
       [?tx :db/txInstant]]
     db)

Upvotes: 1

zcaudate
zcaudate

Reputation: 14258

I figured it out:

(defn last-transaction-time [db]
  (let [t (-> db datomic/basis-t)]
    [t (ffirst (datomic/q '[:find ?t
                            :in $ ?tx
                            :where [?tx :db/txInstant ?t]]
                          db
                          (datomic/t->tx t)))]))

Upvotes: 4

pwnyexpress
pwnyexpress

Reputation: 1016

You would probably want to let the result of your thread function up to the datomic/t->tx. Then use that to query for the transaction entity (implicitly created entity for each transaction). Each transaction entity has a :db/txInstant attribute that is implicitly added during a transaction. The value of that attribute is what you would want to pass to the java.util.Date. static method.

Upvotes: 1

Related Questions