Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/geo/geography/country.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 25 additions & 5 deletions lib/geo/resources/attributes/id.ex
Original file line number Diff line number Diff line change
@@ -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
Loading