Skip to content

Instantly share code, notes, and snippets.

@rauhs
Last active Jan 4, 2019
Embed
What would you like to do?
(defn updated-entity
"Updates the given entity if the db has changed since the entity was last pulled."
[entity]
(let [prev-db (d/entity-db entity)
curr-db @conn]
(if (identical? prev-db curr-db)
entity
(d/entity curr-db (:db/id entity)))))
(defn qes
"Query should return entity ids.
Then returns these as entities entities"
[q db & sources]
(->> (apply d/q q db sources)
(mapv #(d/entity db (first %)))))
(defn datoms
([idx c1]
(d/datoms @conn idx c1))
([idx c1 c2]
(d/datoms @conn idx c1 c2))
([idx c1 c2 c3]
(d/datoms @conn idx c1 c2 c3)))
(defn e-by-av
"Returns the entity given attribute and value."
[a v]
(some-> (datoms :avet a v) first (nth 0) entity))
#_(e-by-av :object/server-version? false)
(defn e-by-a
"Returns the entity given attribute
Probably for DEV only"
[a]
(some-> (datoms :avet a) first (nth 0) entity))
#_ (e-by-a :object/id)
(defn es-by-a
"Eagerly returns all entities with the given attribute."
[a]
(let [db @conn]
(mapv (fn [[e]]
(d/entity db e))
(datoms :avet a))))
#_(es-by-a :object/id)
(defn es-by-av
"Eagerly returns all entities."
[a v]
(let [db @conn]
(mapv
(fn [[e]]
(devprod
(d/touch (d/entity db e))
(d/entity db e)))
(d/datoms db :avet a v))))
#_(es-by-av :notification/hidden? false)
#_(es-by-av :object/server-version? false)
(defn added-entity
"Given the result of transaction returns the added entity.
Or if multiple entity, the one with the tempid of -1."
[tx-report]
(let [db (:db-after tx-report)
tempids (dissoc (:tempids tx-report) :db/current-tx)]
(d/entity db
(if (== 1 (count tempids))
(first (vals tempids))
(get tempids -1)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment