Created
July 28, 2014 15:55
-
-
Save shargoj/b2eca7c815963d858f28 to your computer and use it in GitHub Desktop.
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
(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