Datomic peer query with Loom
by Dustin Getz,
Datomic peer model and Datomic Cloud classpath functions mean we can do things like this.
is a general purpose graph library for Clojure that doesn't know anything of Datomic, it predates Datomic by two years.
(defn ref? [$ k] (= :db.type/ref (:db/valueType (d/entity $ k)))) (defn one? [$ k] (= :db.cardinality/one (:db/cardinality (d/entity $ k)))) (defn many? [$ k] (= :db.cardinality/many (:db/cardinality (d/entity $ k)))) (defn component? [$ k] (:db/isComponent (d/entity $ k))) (defn datomic-entity-successors "Excludes component because component is considered part of its parent" [$ e] (->> (-> (d/pull $ ['*] e) (dissoc :db/id)) (mapcat (fn [[k v]] (if (and (ref? $ k) (not (component? $ k))) (cond (one? $ k) [(:db/id v)] (many? $ k) (mapv :db/id v))))))) (defn clone-entities "Clone a Datomic entity and any reachable entities by walking the entity graph breadth-first with Loom." [$ es] (let [traverse #(loom/bf-traverse (partial datomic-entity-successors $) %) reachable-es (->> es (mapcat traverse) set)] (->> (seq reachable-es) (d/pull-many $ ['*]) (mapv (partial alter-ids (tempids reachable-es))))))