Skip to content

Instantly share code, notes, and snippets.

@tifletcher
Last active July 27, 2016 21:51
Show Gist options
  • Save tifletcher/ec68eb48b766fb25b066655c41e36596 to your computer and use it in GitHub Desktop.
Save tifletcher/ec68eb48b766fb25b066655c41e36596 to your computer and use it in GitHub Desktop.

Because sometimes faking it is enough.

function genGUID() {
// http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
return v.toString(16);
});
}
-- postgres, at least
CREATE FUNCTION generate_uuid() RETURNS uuid AS $$
SELECT md5(random()::text || clock_timestamp()::text)::uuid
$$ LANGUAGE SQL;
-- or, simpler:
create extension if not exists pgcrypto;
select gen_random_uuid() from foo;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment