How do correctly create a uuid for a new database entry?

There's a lot of different ways and definitely no right or wrong, just depends on use case.

There are different versions of 'standardized' UUID's, v4 seems to have evolved to be the option adopted by most frameworks (though they're described as standards there still seems to be different ways of creating them, it's just the format that's standard!)

The first thing I did on Wappler last year was a port from Laravel which used v4 UUID so this was something I was forced to tackle quite early on.

For PHP you can create a custom formatter, there's some info in this post:

On Node I think you can just import a package to manage it for you, I've not tried it, and there are other packages around:

You could probably also create a custom formatter similar to the PHP route, there are JS code snippets like this that could be used:

This post also shows how the version standards have evolved, it's about 10 years old but has been updated as new options for generating 'randomness' have been developed, or gone in and out of fashion. A package should be maintained and updated, if you use your own formatter you may need to update the code every so often.

For most apps all these options are potentially overkill and unnecessary but they're useful if you have specific requirements, and could be essential if you had a massive app with database sharding etc (one day...!)

2 Likes