Ash FrameworkAF
Ash Framework3y ago
17 replies
lamp-town-guy

How to replace Ecto.Schema modifications?

So I have my custom ULID implementation. It takes UUID format as well as BASE64 url encoded and transforms it to UUID in database layer. When taking out of DB it's transformed to BASE64 again. It creates shorter URLs which is nice. With ecto I needed just 2 lines of code to have all database IDs in this format. But I don't know what's the best way to do it in ash.

Also I've modified timestamps to be utc_datetime_usec instead of naive datetime ones. It's easier to convert them to different timezones that way.

Here's the code:

defmodule App.Schema do
  defmacro __using__(_env) do
    quote do
      use Ecto.Schema

      @timestamps_opts [
        type: :utc_datetime_usec
      ]
      @primary_key {:id, App.Schema.ULID, autogenerate: true}
      @foreign_key_type App.Schema.ULID

      import Ecto.Changeset
      import Ecto.Query
    end
  end
end

All I needed to do then was to use App.Schema and it was done. Is there a similar way to do it in Ash? Or at least a way to add ULID in this convenient as a separate data type?

In case anyone is interested into my ULID implementation here's gist https://gist.github.com/lamp-town-guy/306694cdb510bd93894c51bb1aaf917e
Gist
ULID Elixir. GitHub Gist: instantly share code, notes, and snippets.
ULID Elixir
Was this page helpful?