Skip to content

Instantly share code, notes, and snippets.

@lynaghk
Created June 16, 2015 19:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lynaghk/52ec255ea22ba9f1efef to your computer and use it in GitHub Desktop.
Save lynaghk/52ec255ea22ba9f1efef to your computer and use it in GitHub Desktop.
datascript entity export
;;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