Fork me on GitHub
#xtdb
<
2023-05-15
>
hifumi12307:05:58

I am attempting to understand transaction functions, but the example provided in the language reference seems to not work on my end. What am I doing wrong?

(defonce node (xt/start-node {}))

(xt/submit-tx node [[::xt/put
                     {:xt/id :increment-age
                      ;; note that the function body is quoted.
                      ;; and function calls are fully qualified
                      :xt/fn '(fn [ctx eid]
                                (let [db (xt/db ctx)
                                      entity (xt/entity db eid)]
                                  [[::xt/put (update entity :age inc)]]))}]])
(xt/submit-tx node [[::xt/put {:xt/id :ivan, :age 40}]])
(xt/submit-tx node [[::xt/fn :increment-age :ivan]])

(xt/entity (xt/db node) :ivan)
;; => {:age 40, :xt/id :ivan}
I am expecting the age to have been incremented to 41, but it is still 40. For reference, I am using [com.xtdb/xtdb-core "1.23.2"]

tatut07:05:37

you may need to use xtdb.api/db as the form is quoted

tatut07:05:43

in tx fn code that is

tatut07:05:39

and in the entity call as well

hifumi12307:05:56

oh wow that explains everything. Thanks! :D

2