I am building a notes app where I use a MySQL database to store the notes. The app should be able to store notes offline (PWA).
I have one table where all the notes for all users will be stored. Now I am wondering what the best way to create the ID of the notes is?
Option 1: auto increment MySQL column.
The downside of this approach is that if someone creates a note offline there could be duplicate ID's in the column when the app syncs the notes. This could be prevented to give this duplicate note a new ID and sync it with the user.
Option 2: Use a UID function like the one below.
The downside of this is that there is a very small possibility that the UID will be duplicate with on that is already stored in the DB. Same thing as option 1 I can then create a new ID which is unique server side, and sync it with the user's app.
let S4 = function () {
return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
};
let id = (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
example output: c62a85d6-4e93-e443-8533-56a895d7e013
What do you guys think is the best approach for this? do you have any suggestions or totally different ways of doing this?