I've got an entity, and I want to update it. What's the common pattern for handling this? Do you diff the old entity and the new one, so you can figure out which datoms to retract? Do you just retract the entire entity and replace it?
Yes, you need to know which entity to update, so you need to get the :db/id
and then you can just transact new facts to an entity like so (d/transact conn [{:db/id 123 :email "new-email@foo.bar"}])
. The old values are automatically retracted and moved to the historical index if you have it enabled so can query that as well if you need to.
That's really helpful, I assumed that transact
was more of a merge
-like operation. I'll play around at the repl, but this certainly makes updates easy.
Yeah, there are some handy features in there I had to discover myself again when I used it in a customer project, e.g. reverse lookups on transactions.
If you want to retract some keys and assert others, then you may have to retract the individual facts though, right? i.e. you're updating entity 1 to have :some/key1 retracted and :some/key2 asserted.
Yes, that is how you normally do it.