9/4/2024
I've got something cool to share from my current job. At NaranjaX, we've been cooking up a project called "Kausanna," aimed at tackling chargeback processing for credit cards. It's been an interesting journey, and along the way, we made a decision that I think could spark some neat discussions: opting for namespaced ULIDs over the traditional UUIDs.
First off, let's talk about why we even thought about ditching UUIDs. While they've been a staple for generating unique identifiers, they're not without their quirks. Here’s the lowdown:
Enter ULIDs – our chosen heroes. They’re not just shorter and easier on the eyes; they offer the superpower of lexicographical sorting. Imagine the convenience in your databases!
Have in mind that projects can have different requirements and constraints. Make sure to evaluate if this approach fits your needs.
Our twist was to create "namespaced ULIDs" for the Kausanna project. The gist? We generate ULIDs with a pinch of context, assigning prefixes based on their use case. Picture this: a new product ID starts with "prod*" and an API key with "key*". It’s not just about uniqueness but also about adding a layer of identifiable context to the ID.
Here's a snippet to show how we're spinning this magic:
1import { ulid } from "ulidx";
2
3const namespaces = {
4 product: "prod",
5 apiKey: "key",
6};
7
8export function namespacedUlid(namespace: keyof typeof namespaces) {
9 const mappedNamespace = namespaces[namespace];
10 if (!namespace)
11 throw new Error(`Namespace "${namespace}" is not mapped for generation.`);
12 return `${mappedNamespace}_${ulid()}`;
13}
14
15namespacedUlid("apiKey");
This approach ensures that every ID generated is not just unique but also immediately tells you something about the entity it represents. It’s like giving your IDs a brief intro about themselves before they dive into your database parties. 🎉
Implementing namespaced ULIDs has been a game-changer for us. It's streamlined our processes, made our data more readable, and added an efficient sorting mechanism. For any of you embarking on projects where ID generation becomes central, considering a leap from UUID to ULID, especially with a namespaced twist, might just be the workflow upgrade you’re looking for.
Let's keep the code flowing and the IDs meaningful. Until next time, happy coding!
© 2024 - Made with ❤️ in Argentina 🇦🇷