Skip to content

Instantly share code, notes, and snippets.

@pesterhazy
Created January 11, 2017 09:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pesterhazy/479303224559cf7fa372c5af3c992768 to your computer and use it in GitHub Desktop.
Save pesterhazy/479303224559cf7fa372c5af3c992768 to your computer and use it in GitHub Desktop.
Datomic entity maintenance helpers
;; Helpers for looking up and updating datomic db entities from the REPL
(defn every-eid
"Returns set of ids of entites that have a given attribute"
[db attr]
(into #{} (d/q '[:find [?e ...]
:in $ ?attr
:where [?e ?attr]]
db attr)))
(defn every-entity
"Like every-eid except that it converts results into Datomic entities"
[db attr]
(into #{} (->> (d/q '[:find [?e ...]
:in $ ?attr
:where [?e ?attr]]
db attr)
(map (partial d/entity db)))))
(defn every-val
"Returns set of all values of a certain attribute"
[db attr]
(into #{} (d/q '[:find [?v ...]
:in $ ?attr
:where [_ ?attr ?v]]
db attr)))
(defn update-all
"Update all entities in db having attr by applying f. Like clojure.core/update-in
Example:
(update-all db :cat.product/name clojure.string/replace "accross" "across")
"
[db attr f & args]
(->> (every-entity db attr)
(keep (fn [e]
(let [v (get e attr)
new-v (apply f v args)]
(when (not= v new-v)
[:db/add (:db/id e) attr new-v]))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment