0

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?

user234562
  • 631
  • 9
  • 20
  • What do you need the note id in the client side for? Why is it important to have an id on "unsaved" notes in the client? Can't you just have a pile of notes in the client which are "not saved yet" and when you are online again you save the notes on the server and get then a valid id (from an auto increment column)? – Progman Feb 02 '19 at 10:04
  • I need an ID because when you select a note it will show the data in the UI of that note through the ID. Because I am going to use an array with the ID as index. Its a good question though, I could have them have a temporary 'offline' ID until it goes online. – user234562 Feb 02 '19 at 13:12
  • You usually use a UUID to generate a ID in a distributed environment where you cannot ask someone for the next id. Keep in mind that you still generate an auto increment Id for the row in the table of your database, the UUID is just to identify the object in the client. – Progman Feb 02 '19 at 13:18
  • So If I understand correctly, I will create a UUID for the note which I use in the application to select the note in the UI. and I use the increment ID of the DB to store them uniquely. This way it doesn't matter if the UUID already exists in the DB, the only check I have to do is if the UUID is unique per user? – user234562 Feb 02 '19 at 13:33
  • You can consider UUIDs to be unique. You don't need to handle the case or put the effort in to handle two UUIDs being the same, the probability for that is too low. Check the API of your programming language on how to create a new random UUID and use it. – Progman Feb 02 '19 at 13:36
  • Thanks for your replies :D I use Javascript, and for now, I use the snippet above in the original post – user234562 Feb 02 '19 at 13:39
  • See https://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript for creating UUIDs in javascript. – Progman Feb 02 '19 at 13:43

0 Answers0