Hey, there
My question regards auto-incrementation in a specific case, that goes beyond normal usage.
I am building an app where users aggregate their banking transactions.
My schema connects a table User
(one) with a table Transaction
(many).
For each user
, the related transactions
should increment like follow T-001, T-002, … T-N, etc.
This implies that for 2 different users
, the first transaction
of each will be indexed T-001.
And the second transaction
of each will be indexed T-002.
And that’s totally fine. Increment indexes don’t have to be unique (and they should not be).
This is why I can’t apply the auto-increment feature in Transaction
table.
So I need to find another approach.
My first approach consisted of storing an index
value in the User
table. This index
value would be auto-incremented with a custom function that would be triggered before creating a transaction
.
The problem is that transactions are created in parallel, and so custom functions will be executed in parallel.
Because of that, some functions will access the same index
value at the same time.
As a consequence, some transactions
within the same user
will be affected to the same index value.
Hence, this way doesn’t work.
Another solution would consist of locking the index
value during User
queries and updates, to ensure table calls are executed sequentially, thus garanteeing that 2 calls never access the same index
value at the same time.
But 8base doesn’t allow such a granular control on database writing and quering — which makes totally sense!
A final solution would be to create an external API that would return on demand an auto-incremented index for each given user.id
. But it’s quite over-engineered.
So I need your help.
Any idea about how to create and manage indexes that are auto-incremented for each transaction
per user
?
Hope I made the request clear.
Thanks
Andréas