This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-01-30
Channels
- # announcements (3)
- # babashka (5)
- # beginners (68)
- # calva (34)
- # cider (6)
- # cljsrn (3)
- # clojure (48)
- # clojure-australia (2)
- # clojure-europe (1)
- # clojure-nl (4)
- # clojure-spec (28)
- # clojurescript (10)
- # conjure (20)
- # datahike (1)
- # deps-new (1)
- # depstar (27)
- # events (2)
- # fulcro (61)
- # graalvm (32)
- # membrane (18)
- # off-topic (20)
- # practicalli (10)
- # reagent (2)
- # shadow-cljs (8)
- # slack-help (20)
- # spacemacs (9)
- # sql (5)
- # vim (10)
- # xtdb (8)
Hello people of Crux, I've been trying the database out. Am i supposed to update an entity using :crux.tx/put ?
Because if i have an entity let's say
{:crux.db/id id
:foo :goo
:bar :baz}
And then i do the transaction
[:crux.tx/put {:crux.db/id id
:foo :doo}]
The bar attribute will be lost.Nevermind found the answer here https://juxt.pro/blog/a-bitemporal-tale#_letting_data_in
(defn entity-update
[entity-id new-attrs valid-time]
(let [entity-prev-value (crux/entity (crux/db system) entity-id)]
(crux/submit-tx system
[[:crux.tx/put
(merge entity-prev-value new-attrs)
valid-time]])))
In case you hadn't made the connection, transaction functions can be used to consistently update individual attributes: https://opencrux.com/blog/crux-transaction-functions.html
@silencioseu put
is used as an append-only operation for the entire entity. Your second transaction is effectively saying "now the entity has a schema of [:crux.db/id :foo]
, dropping the :bar
attribute from the previous implicit schema. If you want to maintain :bar
you will need to do:
[:crux.tx/put {:crux.db/id id
:bar :baz
:foo :doo}]
@steven427 Yes, thanks alot. I think i got it confused with Datahike (i've only used a little). and i think the write transactions there don't overwrite the whole entity but just the specified attributes
Make sense. I haven't used DataHike but my understanding is that it's a triple-store, so any update would be over a single attr.