AshObjectIds

https://hex.pm/packages/ash_object_ids An Ash implementation of the "Stripe IDs" (human readable, copyable, IDs with a prefix for the model they reference): https://dev.to/stripe/designing-apis-for-humans-object-ids-3o5a TLDR:
defmodule App.Blog.Comment do
use Ash.Resource,
domain: App.Blog,
data_layer: Ash.DataLayer.AshPostgres,
extensions: [AshObjectIds]

alias App.Blog.Post

object_id do
prefix "c"
end

attributes do
uuid_primary_key :id
attribute :text, :string, public?: true, allow_nil?: false
end

relationships do
belongs_to :post, Post, attribute_type: Post.ObjectId
end
end
defmodule App.Blog.Comment do
use Ash.Resource,
domain: App.Blog,
data_layer: Ash.DataLayer.AshPostgres,
extensions: [AshObjectIds]

alias App.Blog.Post

object_id do
prefix "c"
end

attributes do
uuid_primary_key :id
attribute :text, :string, public?: true, allow_nil?: false
end

relationships do
belongs_to :post, Post, attribute_type: Post.ObjectId
end
end
Externally, these are prefixed, base58-encoded UUIDs.
15 Replies
ZachDaniel
ZachDaniel•2mo ago
Nice! Don't forget to add a link to the project 😄
Randall
RandallOP•2mo ago
Haha, you're right, forgot the most important part. Added it. The only thing I wasn't sure about with this library was the use of persisters to generate the type module for each resource
Meeq
Meeq•2mo ago
What would it look like to migrate from using plain UUIDs to AshObjectIds?
Randall
RandallOP•2mo ago
I did think about adding some kind of "allow raw uuids" option to provide an upgrade path. But I primarly made this lib for greenfield projects, so I hadn't added it yet
Meeq
Meeq•2mo ago
The absence of a _ character in the ID should be a giveaway that it isn't an Object ID. Of course, you still need to disambiguate Object IDs that have been orphaned from their prefix vs raw UUIDs, but luckily they are encoded differently.
Randall
RandallOP•2mo ago
Yeah it shouldn't be a lot of work; it's just work I haven't done 😛
Gonzalo Muñoz
Gonzalo Muñoz•2mo ago
Hi! This is great, thanks! A nitpick about the name though: it can be confusing. At least in my experience, people who know about databases usually identify "objectId" with MongoDB's _ids. If this is only for Stripe IDs, it should be easily recognizable by its name. Same thing if it is a generic package for several types of unique identifiers. I read the article though, and they themselves call it both ways so I don't even know lol
Georges
Georges•2mo ago
Ah, I see they are stored as UUID, so theoretically, one could switch to using these without changing the database
Randall
RandallOP•2mo ago
Yes they're stored as plain UUIDs. I believe stripe actually stores them encoded and all, but I have a use case where I want to keep them UUIDv7 in the database.
Meeq
Meeq•2mo ago
What benefit is there of storing as UUIDv7 in the database? Timestamp extraction?
Randall
RandallOP•2mo ago
Sorting & cursor-based pagination
Oliver
Oliver•2mo ago
Hexdocs link from hex is broken @Randall
Randall
RandallOP•2mo ago
Fixed, thanks
ZachDaniel
ZachDaniel•2w ago
@Randall I'm putting this in the ash weekly news letter, do you want me to use your full name? Publishing for now but happy to edit as you like later 🙂
Randall
RandallOP•2w ago
It's fine like this

Did you find this page helpful?