samedi 1 juillet 2023

Name spacing random/unique IDs

I am building a web app that generates unique IDs for user-created resources, so URLs or just IDs can easily be shared. I would like to “namespace” the IDs in some way, while keeping the IDs a reasonable length and looking consistent.

There are currently three hierarchical types of resources: a file, a collection (group of related files), and a project (a group of related collections).

Currently my scheme uses a 12 character nanoID (excluding underscores and dashes) with a prefix, e.g., [fcp]-x17Y8rSk03al. Where f, c, p stand for file, collection or project.

I don’t quite like this scheme and would prefer all the IDs to look indistinguishable to the user, while still being able to figure out resource type from the ID on the backend.

Possible solutions I’ve considered:

  1. Different ID lengths for each of the 3 resources. This is easy to expand in the future if I add more resource types but it’s inconsistent.

  2. Add a numerical prefix like myapp-xxxx-(nanoID), where xxxx are just random numbers. For example, 0-4999 is a file, 5000-7999 is a collection, 8000-8999 is a project and the rest is reserved for future uses, or something like that. The second dash is optional or course.

  3. Add a random prefix like above but in base64 but I don’t want to use - or _, so base 62 I guess? This is the one I like most.

With approaches 2 and 3 I’m not sure how to split up the range into the three resource types and leave room for future additions.

All these solutions seem a bit hacky and I’m worried I’m reinventing the wheel. Is there some kind of standard/convention or a “cleaner” way to do this? Maybe generate the IDs with some scheme so that characters can be manipulated/combined in a specific way to tell me what type of resource it is? But that also sounds unnecessarily complicated.

(I’m using python and JS but ultimately my question is completely language agnostic.)




Aucun commentaire:

Enregistrer un commentaire