bitwalker / bitwalker/uniq

Way to configure ParametrizedType options with Ecto @foreign_key_type module attribute

Open
#26 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Elixir
Stars
121
Forks
23
PR merge metrics
No merged PRs in 30d

Description

I'm thiking about create a new PR with a suggestion to allow define the the options for `Uniq.UUID.init/1` from the module attribute `@foreign_key_type`, but before I create it I want some feedback about what would be the best way to achieve that from the maintainers perspective.

The use case is, I have a module `App.BaseSchema` on my app that defines the `@primary_key` and `@foreign_key_type` for all my schemas. But I want to use the UUIDv7 and the format **slug**, it's easy to define it with the `@primary_key` module attribute, but for the `@foreign_key_type` I can't pass the options, the "workarround" is pass the options in every Ecto associations macro (`belongs_to`, `has_many` etc), but it's repetitive and hard to maintain (If one day I need to change).

One alternative is create a configuration something like:
```elixir
config :uniq, version: 7, format: :slug
```

And change https://github.com/bitwalker/uniq/blob/8ded0da4b648d72ead2ba7572445cb161231e458/lib/uuid.ex#L958-L963

To something like:

```elixir
format =
Keyword.get_lazy(opts, :format, fn -> Application.get_env(:uniq, :format, :default) end)

version =
Keyword.get_lazy(opts, :version, fn -> Application.get_env(:uniq, :version, 4) end)
```

To be honest, I don't really like the idea of the config, because you can have multiple uses of `Uniq` with different needs and it'll became a mess handle with the config.

Other alternative is create a Module, with a `__using__/1` macro. That allows to pass the options to the `use` macro and delegates all `ParametrizedType` protocol callbacks to the `Uniq.UUID` (I'm currently using something like thin on my own project).

```elixir
defmodule YouApp.Types.UUID do
use Uniq.UUID.ParametrizedType, version: 7, format: :slug
end
```

Any way, I would like to hear the maintainers thoughts about this. If you think having someway to achieve that natively in the lib has some value, just point me the way and I can help with a PR.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with lib/uuid.ex around lines 958-963 and review how Uniq.UUID.init/1 receives options. Then examine ParametrizedType callbacks and the Ecto @foreign_key_type use case described here. Done means maintainers have selected a supported approach for per-module UUID options and its behavior is defined.

Written by the indexing model from the issue text.

Assessment

Tech stack
elixir
Domain
database
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.