Fork me on GitHub
#datomic
<
2022-08-08
>
Nedeljko Radovanovic08:08:24

Hi people, is there a way to retract users from database by doing only one transact? [#:db{:id 17592186045418} #:db{:id 17592186045421} #:db{:id 17592186045423}] I have their db ids but do i need to loop over this vector or can I do it in one transact?

cl_j08:08:31

you can put them in a vector and send in one tractions:

(d/transact conn {:tx-data [[:db/retractEntity 1]
                              [:db/retractEntity 2]
                            [:db/retractEntity 3]]})

Nedeljko Radovanovic08:08:56

hmm thank you

(d/transact
      conn
      {:tx-data [[:db/retract [:db/id 1]]
                 [:db/add "datomic.tx" :db/doc "remove old user"]]})
I tried this approach and it didnt work for me, didnt know about retractEntity, thank you, I will try it

souenzzo11:08:55

:db/retract retracts specific attributes, like [:db/retract 42 :user/name]:db/retractEntity is equivalent to get all the keys (keys (d/pull db [*] 42)) => [:user/name :user/id ....] and retract them. • I recommend you to write tx-my-operation, like (defn tx-retract-user [.... ]), that returns just the array of the operation, [[:db/retract [:db/id 1]] [:db/add "datomic.tx" :db/doc "remove old user"]] in your example • tx-data is always composable via (concat tx-retract-user-1 tx-retract-user-2) => tx-retract-user-1-and-2. Then you can use (d/transact conn {:tx-data tx-retract-user-1-and-2})

souenzzo11:08:46

Avoid to create dead-simple functions

(defn tx-retract-user [id]
[[:db/retractEntity id]
 [:db/add "datomic.tx" :db/doc "remove old user"]])
Just write these tx-my-operation if there is some complexity on it.

Nedeljko Radovanovic17:08:18

Thank you for your response, I will try it. ☺️

pppaul19:08:12

@U2J4FRT2T i don't follow, you say to avoid writing simple functions, but your examples are simple functions.

pppaul19:08:41

do you mean to not write functions, but just write out a bunch of txs in a let, then concat them?

Nedeljko Radovanovic19:08:43

I think he wanted to say is that if i have complexity in code to make a separate functions to make it more simple, to split code for more simple look, please tell me if I am wrong

Nedeljko Radovanovic19:08:35

To make those “operations” functions for operations that i require, and not to write it all together in one

Nedeljko Radovanovic19:08:20

@U2J4FRT2T please tell if i got it wrong

pppaul19:08:11

ok, i think if you are reusing the logic in a lot of places that makes sense. i find a lot of my txs are not reused, though.

souenzzo19:08:55

my point is just don't write a function called tx-retract-x that do just [[:db/retract x]] if you want to retract x, write [[:db/retract x]] Write functions like tx-user-exit, that will: retract the user entity, mark its address as disabled, send one bye message for each active friend...

2