Skip to content
/ uuid Public

uuidv7 and uuidv4 tools: shellscript, Go, Javascript, Postgresql, C

License

Notifications You must be signed in to change notification settings

kshji/uuid

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Uuid - uuidv7

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.

Ksh / Bash shellscript uuidv7

Bash / Ksh 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}

Postgresql uuidv7

SqliLite uuidv7

Go uuidv7

Javascript uuidv7

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.

About

uuidv7 and uuidv4 tools: shellscript, Go, Javascript, Postgresql, C

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published