Last active
September 24, 2020 14:24
-
-
Save lgessler/a53a2081bb9829bf17f69da4720068d4 to your computer and use it in GitHub Desktop.
Macro for crux tx-fns
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
(defmacro deftx [name node tx-fn] | |
"Defines a transaction function on the given node." | |
(let [kwd-name (keyword (str *ns*) (str name))] | |
`(do | |
(crux/submit-tx ~node [[:crux.tx/put {:crux.db/id ~kwd-name | |
:crux.db/fn (quote ~tx-fn)}]]) | |
(defn ~name [& ~'args] | |
(crux/submit-tx ~node (log/spy [(into [:crux.tx/fn ~kwd-name] ~'args)])))))) | |
;; without macro | |
(crux/submit-tx node [[:crux.tx/put {:crux.db/id :increment-age | |
;; note that the function body is quoted. | |
;; and function calls are fully qualified | |
:crux.db/fn '(fn [ctx eid] | |
(let [db (crux.api/db ctx) | |
entity (crux.api/entity db eid)] | |
[[:crux.tx/put (update entity :age inc)]]))}]]) | |
(crux/submit-tx node [[:crux.tx/fn :increment-age :ivan]]) | |
;; with macro | |
(deftx increment-age node | |
'(fn [ctx eid] | |
(let [db (crux.api/db ctx) | |
entity (crux.api/entity db eid)] | |
[[:crux.tx/put (update entity :age inc)]]))) | |
(increment-age :ivan) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment