uuidv7 and uuidv4 tools: shellscript, Go, Javascript, Postgresql, C
This is collection to use uuidV7.
UuidV7 and other uuid-based ones include, for example, the fact that the prefix is a timestamp, which makes indexing enjoyable and the timestamp does not need to be separately stored. It can be used for sorting just like SERIAL type keys.
- IETF
- Why uuidv7 is better ...
- Good bye integers, welcome uuids
- Kvelakur C-language version using SSL Rand -uuidv4
# using cmd uuidgen
uuidgen
# using kernel file
read uuid < /proc/sys/kernel/random/uuid
#uuid=$(cat /proc/sys/kernel/random/uuid)
echo $uuid
# using OpenSSL rand
uuid=$(openssl rand -hex 16)
echo ${uuid:0:8}-${uuid:8:4}-4${uuid:12:3}-${uuid:16:4}-${uuid:20:12}
Based on the JS versions published by Fabio Lim. Uuidv4 and uuidv7 are really elegant solutions. Quite easily adaptable to other languages as well.
const v4 = () => {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
const r = Math.trunc(Math.random() * 16);
const v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
const v7 = () => {
return 'tttttttt-tttt-7xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
const r = Math.trunc(Math.random() * 16);
const v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
}).replace(/^[t]{8}-[t]{4}/, function() {
const unixtimestamp = Date.now().toString(16).padStart(12, '0');
return unixtimestamp.slice(0, 8) + '-' + unixtimestamp.slice(8);
});
}
console.log(v4());
console.log(v7());
Look file uuid.js and uuidv7.js more uuid versions. Source Fabio Lim.