Created
January 11, 2017 09:50
-
-
Save pesterhazy/479303224559cf7fa372c5af3c992768 to your computer and use it in GitHub Desktop.
Datomic entity maintenance helpers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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