Last active
August 29, 2015 14:11
-
-
Save sonwh98/f445abb0da0a79422d8f to your computer and use it in GitHub Desktop.
inserting :db/id key recursively into a map to be transacted to datomic
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 with-db-id [m] | |
(let [m-with-db-id (into m {:db/id (temp-id)}) | |
result (for [[k v] m-with-db-id :let [type-v (type v)]] | |
(if (or (= type-v clojure.lang.PersistentArrayMap) | |
(= type-v clojure.lang.PersistentHashMap)) | |
(let [v-with-db-id (into v {:db/id (temp-id)})] | |
{k (with-db-id v-with-db-id)}) | |
(if (or (= type-v clojure.lang.IteratorSeq) | |
(= type-v clojure.lang.PersistentHashSet)) | |
{k (map #(with-db-id %) v)} | |
{k v})))] | |
(reduce #(into %1 %2) result))) | |
(def m {:first-name "Sonny" :last-name "To" | |
:school {:name "Upenn" | |
:address {:city "Philadelphia" :state "PA" :zip "19104"}} | |
}) | |
(def n (with-db-id m)) | |
#n is m transformed with :db/id needed to transact to datomic | |
{:last-name "To", | |
:school {:address {:city "philadelphia", :state "PA", :zip "19104", :db/id #db/id[:db.part/user -1000016]}, | |
:name "Upenn", | |
:db/id #db/id[:db.part/user -1000016]}, | |
:first-name "Sonny", | |
:db/id #db/id[:db.part/user -1000016]} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment