Fork me on GitHub
#datahike
<
2021-06-08
>
datran03:06:46

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?

kkuehne07:06:30

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 "[email protected]"}]) . 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.

👍 5
datran13:06:18

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.

kkuehne18:06:39

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.

Joshua Suskalo20:06:44

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.

whilo18:06:04

Yes, that is how you normally do it.