This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-08-26
Channels
- # adventofcode (2)
- # announcements (7)
- # babashka (20)
- # beginners (77)
- # brompton (6)
- # calva (4)
- # clj-kondo (28)
- # clj-together (1)
- # cljdoc (2)
- # cljfx (10)
- # cljsrn (1)
- # clojure (77)
- # clojure-europe (33)
- # clojure-gamedev (12)
- # clojure-uk (11)
- # clojurescript (95)
- # clojureverse-ops (4)
- # core-async (4)
- # core-logic (1)
- # cryogen (2)
- # cursive (14)
- # data-science (3)
- # datomic (47)
- # duct (1)
- # emacs (7)
- # fulcro (51)
- # gratitude (8)
- # helix (14)
- # hoplon (4)
- # improve-getting-started (60)
- # jobs (1)
- # jobs-discuss (4)
- # joker (11)
- # lsp (99)
- # meander (62)
- # membrane (5)
- # news-and-articles (3)
- # off-topic (64)
- # pathom (3)
- # polylith (11)
- # practicalli (7)
- # react (1)
- # reagent (8)
- # reveal (15)
- # shadow-cljs (78)
- # specter (7)
- # sql (16)
- # tools-build (1)
- # tools-deps (29)
- # workspaces (1)
- # xtdb (17)
If I wanted to get all the transactions associated with an entity, what would that look like?
(defn all-transactions-for-entity [connection entity]
(d/q {:query '[:find (pull ?transaction [*])
:in $ ?e
:where [?e _ _ ?transaction]]
:args [(d/db connection) (:db/id entity)]}))
(defn insert-and-return [db entity]
(let [temp-id (str (UUID/randomUUID))
entity-to-insert (update entity :db/id (fn [id]
(if (nil? id)
temp-id
id)))
transact-result (transact db
{:tx-data [entity-to-insert]})]
(d/pull (:db-after transact-result)
'[*]
(or (:db/id entity) ((:tempids transact-result) temp-id)))))
(def e (insert-and-return connection {:payment-request/price-cents 123}))
=> #'l.datomic/e
e
=> {:db/id 83562883711071, :payment-request/price-cents 123}
(def e2 (insert-and-return connection (assoc e :payment-request/price-cents 2345234)))
=> #'l.datomic/e2
e2
=> {:db/id 83562883711071, :payment-request/price-cents 2345234}
if you pass the db
, you can make a complicated report/query by asking several questions of the same db value
(by making several function that all take a db
, the call them all with the same db arg)
I think what you mean is “a datom with a matching E was in a TX” If so, then use a history db instead of a normal db and your query should work. The db you use has only currently-asserted datoms in it.
(defn all-transactions-for-entity [db entity]
(d/q {:query '[:find (pull ?transaction [*])
:in $ ?e
:where [?e _ _ ?transaction]]
:args [db (:db/id entity)]}))
(all-transactions-for-entity (d/history (d/db connection)) e2)
Execution error (IllegalStateException) at datomic.core.pull/pull* (pull.clj:364).
Can't pull from history
ah, I forgot about that. You will need to pass two dbs, and make a decision about the moment-in-time value of the tx you pull
(defn all-transactions-for-entity [db entity]
(d/q {:query '[:find (pull ?transaction [*])
:in $ $h ?e
:where [$h ?e _ _ ?transaction]]
:args [db (d/history db) (:db/id entity)]}))
You were querying all datoms in a history db (i.e., including retractions) and collecting their TX; then you were projecting the tx entities into maps at the moment-in-time of db
pull projects an entity at a particular point in time (db), and a history db includes all points in time
okay - so what if i wanted to see the current value of the entity at the time of each one of these transactions
okay small extension to that - what if i wanted to get all entities affected by a transaction
> what if i wanted to get all entities affected by a transaction
d/tx-range
is the index to go.
For queries, this might work as well. (see concerns below↓)
[:find ?e
:in $ ?tx
:where [?e _ _ ?tx]]
That will be a full scan of the entire index. I’m not sure datomic will even let you issue that query
Hi. Is there any simple way to count the total number of datoms in a database?
on-prem
it does not. The transactor reports metrics “Datoms” and “IndexDatoms”. https://docs.datomic.com/on-prem/operation/monitoring.html