I have users from many external sources which I try to map to internal userId, so the table I have is:
userId, externalSourceId, externalUserId
In my code, I'm getting externalSourceId
and externalUserId
and want to get the userId
from the database, if exists, otherwise, create one and return the newly created value. I need this action to be atomic because several processes may try to do the same thing at the same time, so I wished only the first time will create a userId
.
In pseudo code it will look like that:
- u = find user with (externalSourceId, externalUserId)
- if no u:
2.1. u = create new user with (externalSourceId, externalUserId) and random userId - return u