Fork me on GitHub
#datomic
<
2023-12-27
>
itaied09:12:04

I have read the docs about transactions, and there's still something I cant wrap my head around. How can I make changes to existing entities based on a query? Or, retract multiple entities based on a query? For example, "increase by 10% the salary of all employees whose in the company for more than 3 years", and "retract all the products which are out of stock"

Sam Ferrell09:12:56

> in particular they allow the data added in a transaction to be derived from the current value of the database.

itaied09:12:55

Hi, the example queries the entity and then execute an update on that entity, which is not atomic (assume a race condition where the query verifies that the salary hasn't been increased yet). How can I execute a transaction like

update employees 
set salary = salary*1.1,
increased_at = getdate()
where experience > 3 and
increased_at < dateadd(year, -1, getdate());
it doesn't feel right to add dedicated functions for validations, I'm sure I'm missing something here

itaied12:12:09

I have looked into that, but it works for a single entity. I guess I can create multiple cas in a single tx, but it still means that I have to separate the query and the mutation

tatut15:12:56

so you create a transaction function which queries the current employees’ salaries and experience and emits new salaries for those who have enough experience

itaied15:12:37

how would you write it? I'm struggling with understand where does the tx start (datomic execution) and where my code does

tatut15:12:44

it is just a pure function that gets the current db and returns tx data

caleb.macdonaldblack22:12:42

With a transaction function, the transactor executes the function with the current db value, and applies the tx-data returned from that function immediately after. The transactor is single threaded, so nothing else is applied to the database during this time. Basically, changes in transaction functions are atomic.

🙌 1
Aviv11:12:25

Hey, Is there a triggering mechanism similar to PostgreSQL? I want to invoke a webhook based on certain insertions.

magnars16:12:31

You can subscribe to the transactor log too e do this.

Aviv16:12:27

any idea where can I find some docs about it? couldn’t find

Aviv12:12:37

thanks! i’ll check that