Autogenerated unique short IDs
What is the best way in Ash to generate short IDs e.g. invite codes that are unique and will keep generating till it gets a unique one? Actions?
14 Replies
Yep! You can do it in a before action hook and check until you get a unique one 🙂
Check manually or does ash have checking built in?
There is nothing that will repeatedly check and retry built in
👌🏼 So i write an action that generates then tryies to query for it, if it finds it -> generate a new one, else return
Well, you’d just include that as a change on a create action.
yeah
🔥 thanks yeah thats what I was plannin
Just putting the code here for anyone else :magic_sparkles:
🥳
An optimisation would be checking for count rather than an actual query but idk if you can do that
Not quite yet, but I'm working on
Api.aggregate(query, :count, ...)
to do exactly thatThis is a tangent but you might find this interesting. https://morioh.com/p/52d5efc4cba3
ExULID: Unique Lexicographically Sortable Identifier (ULID) in Elixir
In this tutorial, We'll learn ExULID: Universally Unique Lexicographically Sortable Identifier (ULID) in Elixir. Implemented according to ulid/spec.
They are a bit long for what I need xD thanks for sending though
I know. The js ulid library I use lets you control the length and other aspects of the format. This elixir one is a bit deficient.