The similar questions has already been asked, but my question is quite different with some research done over it.
On signing up to a website, I provide
FirstName: Ajay, LastName: Kumar
Now the backend API need to create a unique-Id from these two texts. Why?
Yes, I have used the autoincrement Integer column for storing the user-id, but this is the Unique_id which will be shown in the URL of the user’s profile page, the way most of the websites(fb, twitter, quora) do. I think it helps in SEO for searching the people’s profile.
Approaches in my mind:
1. FirstName + LastName + mysql-auto-generated-id
API cannot know the autogenerated ID before the creation of the user.
2. Concat FirstName & LastName and us it as unique id, if the DB fails
and returns id violation error, then start appending digits starting from 0 until an valid Id is found.
It increases the database roundtrips.
3. FirstName + LastName + [random integer numbers]
It also increse the database roundtrip.
4. Use UUID concatenated to FirstName + LastName.
Since UUID is 128-bit, that’s quite long and I need to substring it. This could again cause duplicate IDs
5. concatenating current timestamp with FirstName + LastName.
But that value is also quite long.
The most efficient way I have is to use UUID and substring 4-5 starting characters from it. If the unique-id is already taken, try with another UUID. I think this is best in case of decreasing the database roundtrips as well.
I am curious to know how websites handle this(other than this database recursive calls until a valid Unique-id is found)? There is proper digit assingnment in unique-id(e.g in Quora). E.g tim-cook-1
, time-cook-2
.
6
Answers
You can just get the count+1 of the record from data base having
Firstname as ‘Arjun’ and Lastname as ‘Kumar’ then just add to your user name
It will create user name in this pattern
Arjun-Kumar-1
Arjun-Kumar-2
Arjun-Kumar-3
…
UUID is a good tool to use. You can also try the Crypto module while working with node.js
I assume that you’re using MySQL. Here’s a possible solution dealing with concurrent requests:
(first_name, last_name, count)
when you get a new user registration request:
select for update
on this tableI cannot see the problem here. Save the object, then create the URL from the name and the already existing id and return it to the client.
You don’t even have to store that unique id separately, because you can build it easily any time from the other three fields.
You can use the following generator and select how long your unique string must be that you append to the firstname-surname-[uniquenumber]
https://alex7kom.github.io/nano-nanoid-cc/?alphabet=0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ&size=8&speed=1000&speedUnit=second
If you make a unique constraint on this firstname-surname-[uniquenumber] field in the database, you can detect a collision and then just regenerate a uniquenumber again.
Based on the given name The below Function generates unique id.
Create a table mentioned, execute Function and try.
Create table and Function Script