Last active
December 19, 2015 07:09
-
-
Save robert-stuttaford/5916891 to your computer and use it in GitHub Desktop.
Using Datomic's d/with to query a database as though some transaction had been committed, before actually committing it!
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
(def conn (d/conn some-uri)) | |
(def some-id 12345) | |
(defn get-v [db] (ffirst (d/q '[:find ?v :in $ ?e :where [?e :some/attribute ?v]] db some-id))) | |
(get-v (d/db conn)) | |
;; => :some-value | |
(def tx [[:db/retract some-id :some/attribute :some-value] | |
[:db/add some-id :some/attribute :some-other-value]]) | |
;; try the tx with d/with | |
(def updated-db (-> (d/db conn) (d/with tx) :db-after)) | |
(get-v updated-db) | |
;; => :some-other-value | |
;; db hasn't changed! | |
(get-v (d/db conn)) | |
;; => :some-value | |
;; commit | |
(d/transact conn tx) | |
;; now it has! | |
(get-v (d/db conn)) | |
;; => :some-other-value |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment