segmentio / ksuid

K-Sortable Globally Unique IDs
https://segment.com/blog/a-brief-history-of-the-uuid/
MIT License
4.82k stars 174 forks source link

Is it possible to generate KSUID from SQL? #43

Closed derkan closed 3 years ago

derkan commented 4 years ago

Hi, To set DEFAULT value for an PK of table, I wonder if has anyone tried to generate a valid ksuid value by using SQL.

For example for UUIDv4, we can generate default value if uuid-ossp extension is not installed for PostrgreSQL as:

`CREATE TABLE IF NOT EXISTS table (
   id VARCHAR(36) DEFAULT md5(random()::text || clock_timestamp()::text)::uuid  NOT NULL,
....

Of course it'll be better to generate from application but, I it makes simpler to add default value for manual interactions with tables.

Has anyone tried to develop an SQL function to geneate ksuid?

daleobrien commented 3 years ago

It's certainly possible, here's what I have done https://github.com/daleobrien/sql-ksuid

derkan commented 3 years ago

thank you, now the missing part is found! 👏