|
|
|
|
|
by stith
4242 days ago
|
|
I had a similar issue with an app I'm writing now. I wanted short IDs so my URLs wouldn't be fugly, but with a low chance of collisions. The solution I went with (in javascript) is: // Make a "pretty unique" ID for this session.
// Since RethinkDB doesn't have a way for us to guarantee a _short_
// random unique value (short of trying the insert and regenerating if it
// doesn't save), we'll just have to rely on the unlikeliness of a collision
// with both this time-based ID and the title-based slug.
// I'm sure this will never ever cause any problems ʘ‿ʘ
var alphabet = "0123456789abcdefghijklmnopqrstuvwxyz";
var id = new Date().getTime().toString().match(/.{1,2}/g).map(function(val){return alphabet[val % alphabet.length];}).join('');
var slugPart = slug((this.title || "").substring(0,60).toLowerCase());
this.url_slug = id + "/" + slugPart;
That is, get a current timestamp (in milliseconds), and use every group of 2 digits to pull a letter out of an alphabet string. Then append "/title-of-the-thing-made-url-safe". This results in strings that look like "ee7zrm9/something-goes-here", which is then used as the primary key for the document. It's not perfect by any means, but it gets the job done, and I thing appending the title makes collisions extremely rare. |
|