Created
June 16, 2015 19:15
-
-
Save lynaghk/52ec255ea22ba9f1efef to your computer and use it in GitHub Desktop.
datascript entity export
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
;;Export datoms, replacing eids w/ tempids so that the datoms can be imported into a second datascript/datomic db. | |
(let [db (d/db conn) | |
eids (->> (d/q '{:find [?eid] | |
:in [$ %] | |
:where [(exportable ?eid)]} | |
db export-rules) | |
(map first) | |
set) | |
temp-ids (zipmap eids (range -1000000 -1)) | |
ref-attrs (reduce-kv (fn [res attr schema] | |
(if (= :db.type/ref (:db/valueType schema)) | |
(conj res attr) | |
res)) | |
#{} | |
(:schema db))] | |
(->> eids | |
(mapcat (partial d/datoms db :eavt)) | |
(map (fn [{:keys [e a v]}] | |
[(temp-ids e) a (if (ref-attrs a) (temp-ids v) v)])))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment