Skip to content

Instantly share code, notes, and snippets.

@shargoj
Created July 28, 2014 15:55
Show Gist options
  • Save shargoj/b2eca7c815963d858f28 to your computer and use it in GitHub Desktop.
Save shargoj/b2eca7c815963d858f28 to your computer and use it in GitHub Desktop.
(defn sp->transactions-helper
[db sp deletions-atom]
(let [{sname :name :as spec} (get-spec sp)
eid (or (-> sp :db-ref :eid) (get-eid db sp spec))
db-val (db/entity db (get-eid db sp spec))]
(with-meta
(hash-map
(for [{iname :name [cardinality type] :type :as item} (:items spec)]
(let [datomic-key (keyword (datomic-ns spec) (name iname))
is-recursive (= :rec (recursiveness item))
v (iname sp)
db-v (datomic-key db-val)]
(when (and (nil? v) (some? db-v))
(swap! deletions-atom cons [:db/retract eid datomic-key db-v]))
;; TODO: set-difference for :is-many's
[datomic-key
(if-not is-recursive v
(case cardinality
(:is-a :is-an) (sp->transactions-helper db v deletions-atom)
:is-many (map #(sp->transactions-helper db % deletions-atom) v)))])))
{:eid eid :data sp :spec spec})))
(defn sp->transactions
[db sp]
(let [deletions (atom '())
datomic-data (sp->transactions-helper db sp deletions)]
(with-meta
(cons datomic-data @deletions)
(meta datomic-data))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment