diff --git a/lib/geo/geography/country.ex b/lib/geo/geography/country.ex index aebc6c6..bb8f75b 100644 --- a/lib/geo/geography/country.ex +++ b/lib/geo/geography/country.ex @@ -2,10 +2,10 @@ defmodule Geo.Geography.Country do use Ash.Resource, otp_app: :geo, domain: Geo.Geography, - data_layer: AshPostgres.DataLayer + data_layer: AshPostgres.DataLayer, + extensions: [Geo.Resources.Attributes.Id] # === Attributes === - use Geo.Resources.Attributes.Id use Geo.Resources.Attributes.Name, allow_nil?: false, unique?: true use Geo.Resources.Attributes.Slug, allow_nil?: false, unique?: true use Geo.Resources.Attributes.Timestamps diff --git a/lib/geo/resources/attributes/id.ex b/lib/geo/resources/attributes/id.ex index 32f595b..1a169a0 100644 --- a/lib/geo/resources/attributes/id.ex +++ b/lib/geo/resources/attributes/id.ex @@ -1,12 +1,32 @@ defmodule Geo.Resources.Attributes.Id do @moduledoc """ + An Ash extension that adds a UUID v7 primary key attribute to a resource. """ - defmacro __using__(_opts) do - quote do - attributes do - uuid_v7_primary_key :id - end + use Spark.Dsl.Extension, + transformers: [ + __MODULE__.Transformer + ] + + defmodule Transformer do + @moduledoc false + use Spark.Dsl.Transformer + + def before?(Ash.Resource.Transformers.BelongsToAttribute), do: true + def before?(_), do: false + + def transform(dsl_state) do + attribute = %Ash.Resource.Attribute{ + name: :id, + type: :uuid_v7, + allow_nil?: false, + writable?: false, + public?: true, + primary_key?: true, + default: &Ash.UUIDv7.generate/0 + } + + {:ok, Spark.Dsl.Transformer.add_entity(dsl_state, [:attributes], attribute)} end end end