Fork me on GitHub
Vincent Cantin08:07:02

I found a convenient way to quote the transaction function correctly:

(backtick/template (fn [ctx eid]
                     (let [db (crux.api/db ctx)
                           entity (crux.api/entity db eid)]
                       [[:crux.tx/put (~`custom-fn entity)]])))


Oh cool, I've not seen backtick before, but this same general idea came up on the Crux Zulip the other day in regards conveniently constructing queries without all the awkward quote symbols - I'll repost this tip there!

Vincent Cantin08:07:11

I can also read on Zulip


Oh, that’s an awesome library.

Vincent Cantin08:07:17

maybe the most simple way to do function transaction is to directly associate a quoted qualified symbol to :crux.db/fn (I did not try yet but it should work)

{:crux.db/id :product/foobar-tx-fn
 :crux.db/fn `foobar-tx-fn}


we don't currently require any symbols provided to transaction functions, because they could be anywhere in the body - and you might not want namespaced-qualified symbols requiring!


things like requiring-resolve can be useful here - ((requiring-resolve 'your.ns/your-fn) ...)

👍 1

there're a few namespaces that might currently work accidentally, but best not to rely on those 🙂


also, personal preference, but worth adding you can also use vanilla Clojure backtick here, if you haven't got too much gensym'ing to do

Vincent Cantin09:07:51

(clojure.core/let [user/db (crux.api/db user/ctx) ....

Vincent Cantin09:07:09

I am not sure that it would do what we want.


yep - you'd need to gensym the local variables using #:

`(fn [ctx# eid#]
   (let [db# (crux/db ctx#)
         entity# (crux/entity db# eid#)]

Vincent Cantin09:07:42

but then it's hard to read from the DB because a no-collision number is appended to the symbols

Vincent Cantin09:07:24

this version stays readable after evaluation

`(fn [~'ctx ~'eid]
   (let [~'db (crux/db ~'ctx)
         ~'entity (crux/entity ~'db ~'eid)]

Vincent Cantin09:07:28

IMHO, backtick/template help reducing the typing


ah, ok - I can't say I've often needed to read the functions back out 🙂