Hacker News new | ask | show | jobs
by vbsteven 1844 days ago
I’m currently prototyping a little database+api+cli todo app and I want identifiers that can be abbreviated in the same way as partial git commit hashes can be used on the command line. What should I use?

I was thinking of generating random character strings and simply retry when the db throws duplicate key error on insert. No sharding is necessary and I’d like to have efficient foreign keys. Any thoughts?

3 comments

You could try NanoID[0]? Seems available in many languages.

[0] https://blog.bibekkakati.me/nanoid-alternative-to-uuid

You could use a serial int and just hex-encode when interacting with the CLI? You could then use range queries to match short hashes by zeroing out the remaining bytes and using >=
Check out linear congruential generators or other pseudorandom number generators. Then map the resulting number to letters.