Created
June 28, 2018 17:29
-
-
Save souenzzo/d8e6afe21e990530f58fab5c8c3abc8c 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
(require '[datomic.api :as d]) | |
(defn undo-transact | |
[db tx] | |
(let [ref? (set (d/q '[:find [?ident ...] :where [?e :db/ident ?ident] [?e :db/valueType :db.type/ref]] db)) | |
ident (into {} (d/q '[:find ?id ?ident :where [?id :db/ident ?ident]] db)) | |
hist (d/history db) | |
data-from-tx (d/q '[:find ?e ?a ?v ?tx ?op | |
:in $ ?tx | |
:where | |
[?i :db/ident ?a] | |
[?e ?i ?v ?tx ?op]] | |
hist tx) | |
tx-data (for [[e a v tx op] data-from-tx] | |
[e a (if (ref? a) (ident v v) v) tx op]) | |
tx-data (keep (fn [[e a v tx op]] | |
(when-not (= e tx) | |
[(if op :db/retract :db/add) e a v])) tx-data)] | |
(into [[:db/add (d/tempid :db.part/tx) :audit/undo-tx tx]] tx-data))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment