Fork me on GitHub
Nikolas Pafitis19:01:57

Hello people of Crux, I've been trying the database out. Am i supposed to update an entity using :crux.tx/put ?

Nikolas Pafitis19:01:31

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.

Nikolas Pafitis20:01:05

Nevermind found the answer here

(defn entity-update
  [entity-id new-attrs valid-time]
  (let [entity-prev-value (crux/entity (crux/db system) entity-id)]
    (crux/submit-tx system
        (merge entity-prev-value new-attrs)


In case you hadn't made the connection, transaction functions can be used to consistently update individual attributes:

Steven Deobald20:01:10

@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}]

Nikolas Pafitis20:01:20

@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

Steven Deobald20:01:43

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.