Reputation: 841
I am working on a self-education project building a URL shortener in Node. I was going to use shortid, but that's been deprecated so I switched to nanoid. My concern is the eventual possibility of a collision with a generated ID and an existing ID in the DB (via Knex). The concern isn't exactly "crypto-grade", more of a functional issue with the app crashing due to a new short URL ID already existing in the DB. I have come up with the following hypothetical solution (not my actual code!). Does it seem like the right (best, efficient, beautiful, etc.) way of doing this?
var shortId = nanoid();
while (knex.('urls').where('shortID',shortId).first() != NULL) {
shortId = nanoid();
}
Upvotes: 0
Views: 902
Reputation: 164
You can create an ID based on the timestamps, this is how MongoDB uses its id indexing mechanism.
new Date().valueOf() //1606597014945
You can play with it and add letters, you can shrink it to another letter mechanism. Hope it helped you :)
Upvotes: 0