 
       ;; if there are errors, return the first as an exemplar
       (if (nil? maybe-err)
-        (return [denorm-rel [:ok]])
-        (return [denorm-rel [:fail maybe-err]])))))
+        (return [denorm-rel-kw [:ok]])
+        (return [denorm-rel-kw [:fail maybe-err]])))))
 
-;; (s/defn denormalize
-;;   "denormalize all relationships for a given source record
-;;    returns [[denorm-rel [status maybe-err]]*]"
-;;   [session :- Session
-;;    source-entity :- Entity
-;;    source-record :- t/RecordSchema
-;;    denorm-op :- DenormalizeOp]
-;;   (let [rels (-> source-entity :denorm-targets vals)
-;;         v (uuid/v1)
-;;         resps (->> rels
-;;                    (map #(denormalize-rel session
-;;                                           source-entity
-;;                                           target-entity
-;;                                           source-record
-;;                                           %
-;;                                           denorm-op
-;;                                           v)))]
-;;     (apply d/zip resps)))
+
+
+(s/defn denormalize
+  "denormalize all relationships for a given source record
+   returns [[denorm-rel-kw [status maybe-err]]*]"
+  [session :- Session
+   source-entity :- Entity
+   source-record :- t/RecordSchema
+   denorm-op :- DenormalizeOp]
+  (let [resps (->> source-entity
+                   :denorm-targets
+                   (map
+                    (fn [[rel-kw rel]]
+                      (denormalize-rel session
+                                       source-entity
+                                       (deref-target-entity (:target rel))
+                                       source-record
+                                       rel-kw
+                                       rel
+                                       denorm-op))))]
+    (apply d/zip resps)))

(s/defschema DenormalizeOp
  (s/enum :upsert :delete))

(defn deref-target-entity
  [kw]
  (let [var (ns-resolve (-> kw namespace symbol)
                        (-> kw name symbol))]
    @var))

(s/defn foreign-key-val
  "returns [fk fk-val] for the denorm relationship"

   target-entity :- Entity
   source-record :- t/RecordSchema
   denorm-rel :- t/DenormalizationRelationshipSchema]
  (with-context deferred-context
    (mlet [:let [[fk fk-val] (foreign-key-val source-entity source-record denorm-rel)]

          trs (m/select-buffered
               session
               target-entity
               fk
               fk-val)]

      (return deferred-context trs))))

(defn only-error
  "given a Deferred keep only errors, otherwise returning Deferred<nil>"
