Fork me on GitHub

Say, anybody got a "recursively touch entity e to depth n" function lying around? I saw this but I need to handle the {:db/valueType :db.type/ref :db/cardinality :db.cardinality/many} case.


Well, this is what I came up with, not the prettiest or most efficient, but it works well enough for debugging purposes.

(defn touch-n
  "Recursively touch an entity to n levels deep (default 2). Return a nested map."
   (touch-n 2 e))
  ([n e]
   (if (or (zero? n) (not (entity? e)))
     ;; We've reached our recursion limit, or we're at a leaf node
     ;; Else we're an an entity, touch it an recur
     (reduce (fn [m [k v]]
               (assoc m k (if (set? v)
                            ;; Cardinality many
                            (into #{} (map (partial touch-n (dec n)) v))
                            ;; Cardinality one
                            (touch-n (dec n) v))))
             (d/touch e)))))


(where I've already got (defn entity? [thing] (instance? datomic.Entity thing)))