This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-04-03
Channels
- # announcements (5)
- # aws (3)
- # babashka (52)
- # babashka-sci-dev (23)
- # beginners (51)
- # calva (191)
- # clj-commons (18)
- # clj-kondo (11)
- # cljdoc (39)
- # cljsrn (3)
- # clojure (24)
- # clojure-czech (3)
- # clojure-dev (2)
- # clojure-europe (15)
- # clojuredesign-podcast (2)
- # clojurescript (8)
- # conjure (2)
- # core-typed (151)
- # cursive (15)
- # data-science (3)
- # datalevin (4)
- # datomic (8)
- # figwheel-main (21)
- # fulcro (37)
- # gratitude (3)
- # honeysql (1)
- # hyperfiddle (2)
- # introduce-yourself (1)
- # malli (3)
- # membrane (54)
- # off-topic (21)
- # other-languages (4)
- # portal (18)
- # re-frame (12)
- # reagent (7)
- # releases (2)
- # sci (64)
- # scittle (1)
- # spacemacs (14)
- # sql (2)
- # vim (4)
- # xtdb (6)
Heyy, any ready recipes for unique constraint transaction functions? Or how to use match for that? E.g. I have a user where I want both email and username to be unique
for the match version - I use this helper that I lifted from emccue:
(defn match-update-entity
"Takes xtdb node, an xt/id and a map. Uses match to merge the new entity with the existing xtdb document atomically.
Recurs until successful with no limit.
shout out emccue:
"
[xtdb-node entity-id new-entity]
(let [[[old-entity]] (seq (xt/q (xt/db xtdb-node)
'{:find [(pull ?eid [*])]
:where [[?eid :xt/id id]]
:in [id]}
entity-id))
new-entity (merge old-entity new-entity)
tx (xt/submit-tx xtdb-node [[::xt/match entity-id old-entity] [::xt/put new-entity]])
most-recent-tx (xt/await-tx xtdb-node tx)]
(if (xt/tx-committed? xtdb-node tx)
most-recent-tx
(recur xtdb-node entity-id new-entity))))
👍 2
(require '[xtdb.api :as xt])
(def check-unique
'(fn [ctx attr value]
(let [db (xtdb.api/db ctx)
res (xtdb.api/q db
[:find (quote ?e)
:in (quote ?value)
:where [(quote ?e) attr (quote ?value)]]
value)]
(if (empty? res)
[]
false))))
(defn install-fns! [node]
(xt/submit-tx node [[::xt/put {:xt/id :check-unique
:xt/fn check-unique}]]))
(with-open [node (xt/start-node {})]
(install-fns! node)
[(xt/tx-committed? node
(xt/await-tx node
(xt/submit-tx node
[[::xt/fn :check-unique :user/name "Niki"]
[::xt/put {:xt/id "1" :user/name "Niki"}]])))
(xt/tx-committed? node
(xt/await-tx node
(xt/submit-tx node
[[::xt/fn :check-unique :user/name "Niki"]
[::xt/put {:xt/id "1" :user/name "Niki"}]])))])
🙏 2