This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-02-23
Channels
- # announcements (1)
- # babashka (68)
- # babashka-sci-dev (12)
- # beginners (36)
- # biff (22)
- # calva (20)
- # clerk (1)
- # clj-on-windows (7)
- # clojure (27)
- # clojure-conj (8)
- # clojure-denmark (2)
- # clojure-europe (141)
- # clojure-france (1)
- # clojure-italy (4)
- # clojure-nl (1)
- # clojure-uk (2)
- # clojurescript (7)
- # conjure (8)
- # core-async (111)
- # cursive (3)
- # datahike (4)
- # datalevin (18)
- # events (9)
- # gratitude (4)
- # guix (2)
- # helix (3)
- # hyperfiddle (62)
- # introduce-yourself (2)
- # kaocha (4)
- # london-clojurians (3)
- # lsp (7)
- # malli (34)
- # membrane (1)
- # nbb (9)
- # polylith (4)
- # portal (6)
- # reagent (4)
- # releases (2)
- # remote-jobs (4)
- # shadow-cljs (30)
- # sql (2)
- # tools-deps (58)
- # xtdb (9)
How would you handle biff/submit-tx
when doing upsert
with the db schema?
For instance I just want to update the email for a user with this schema:
(def user
{:user/id :uuid
:user/email :string
:user/joined-at inst?
:user [:map {:closed true}
[:xt/id :user/id]
:user/email
:user/joined-at]})
But when I do this
(biff/submit-tx sys [{:db/doc-type :user
:db/op :upsert
:xt/id #uuid "..."
:user/email ""}])
I get a schema error since joined-at
is missing.
Should I create separate schema for the transaction?
Or lookup and merge the date (which defeats the purpose of :upsert
or?)I also don't want to set joined-at
as optional for the db, since I want to make sure it's always there in the db.
I'd also like to know the answer to this
> Or lookup and merge the date (which defeats :upsert or?)
This is what I do now (didn't even know
nevermind, I'm an idiot, I use :update:upsert
is a valid xtdb op)
I guess writing a separate schema for the transaction would make things safer in the long run, to avoid users overwriting arbitrary props
Oh, https://biffweb.com/docs/reference/transactions/ and it's the same as merge if the document exists
is this for a user that definitely is already in the database? if so you should use :update instead of :upsert.
Right, that's what I was just thinking; because :update will fail if the user doesn't exist
In that I need to pass all the keys that are required in the db schema. But I want only to update user/email
for example with an update
update (and merge) will look up the existing doc for you and use the current values
:upsert doesn't do that?
it does that if the document exists, but if not it'll create a new doc, in which case you need to provide all the required keys
you can use the :db/default operation to set keys only if it's a new doc/if the keys aren't set yet
According to OP, the document exists but xtdb still complains that the schema is wrong Under the transaction docs, there's this note: > Note: You must have installed the :biff/ensure-unique transaction function for this to work. See com.biffweb/tx-fns (installed by default in new projects). Is this a prerequisite in order to do a proper :update/:merge (seems relevant but maybe it's not)?
if the doc exists and it already has all the required keys, that error shouldn't be triggered by upsert. the upsert in the code snippet is also not in the right format--with upsert you never pass in the :xt/id, since the whole point of upsert is to create or update a document based on an arbitrary key-value pair(s). e.g. it's good for creating a user with a given email address so that if they hit the signup button twice in a row or something, you don't accidentally create two separate user docs. see the transaction docs (linked above) for an example of upsert (on my phone currently so a little hard to type it out :) )
the :biff/ensure-unique transaction fn is installed by biff automatically
(aside, maybe I should deprecate the merge operation and just have upsert--they're quite different implementation wise but I guess semantically they're the same. difference is that merge only works for updating/creating documents based on :xt/id, while upsert works for arbitrary keys)
Here's my results/notes for getting started with unit testing biff/submit-tx
https://gist.github.com/floscr/4af4fa5cb79d4538d558b0f685ffdf97
looks great 👍