Skip to content

Instantly share code, notes, and snippets.

@robert-stuttaford
Last active December 19, 2015 07:09
Show Gist options
  • Save robert-stuttaford/5916891 to your computer and use it in GitHub Desktop.
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!
(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