This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-04-29
Channels
- # announcements (1)
- # babashka (15)
- # beginners (37)
- # calva (94)
- # cider (3)
- # clj-kondo (17)
- # cljsrn (2)
- # clojure (45)
- # clojure-europe (39)
- # clojure-germany (1)
- # clojure-norway (2)
- # clojurescript (16)
- # component (18)
- # conjure (1)
- # cursive (13)
- # datalevin (3)
- # datomic (12)
- # docker (2)
- # duct (5)
- # eastwood (2)
- # emacs (4)
- # events (8)
- # fulcro (8)
- # inf-clojure (5)
- # kaocha (8)
- # lsp (24)
- # malli (11)
- # meander (3)
- # off-topic (19)
- # polylith (11)
- # remote-jobs (4)
- # sci (61)
- # shadow-cljs (9)
- # spacemacs (34)
- # sql (10)
- # tools-deps (27)
- # xtdb (10)
Hi, been trying to search but is it possible to put a document and not overwrite existing fields? Is this through transaction functions as well?
gotcha, thanks!
you can also use :xtdb.api/match
for this:
(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))))
new-entity (merge old-entity new-entity)
being the relevant part (and ACID guarantees)
Ran into this and wanted to say thank you, we dropped the merge
parte cause it would not work in the case of keys you want to remove (say optional keys that were present but were taken out). The rest looks awesome 😄
(FWIW for that use case we really like using taoensso.encore/dis-assoc-some
)
I have been using merge
when I need to update a document, but I am not really familiar with [::xt/match entity-id old-entity]
. Can someone let me know what this snippet does here?
Or if I just omit it and say the line like (xt/submit-tx xtdb-node [[::xt/put new-entity]])
, what would happen differently?