nallwhy

nallwhy

Repo.get/1 only covers one primary key id.
I want to create macro that implements get function with multiple primary keys.

My first try

  defmacro defquery(:get) do
    module = __CALLER__.module
    primary_keys = module.__schema__(:primary_key)

    args =
      0..(Enum.count(primary_keys) - 1)
      |> Enum.map(&Macro.var(&1, module))

    quote do
      def get(unquote_splicing(args)) do
        ... 
      end
    end
  end

But module doesn’t have __schema__ when that macro runs.

So, my second try


  defmacro defquery(:get, primary_keys) do
    module = __CALLER__.module

    args =
      primary_keys
      |> Enum.map(&Macro.var(&1, module))

    quote do
      def get(unquote_splicing(args)) do
        ...
      end
    end
  end

But I don’t know how to write something like Repo.get_by([pk1: pk1, pk2: pk2]) dynamically.

Showing Posts 1 to 4

idi527

idi527

:waving_hand:

How would your macro look at the call-site? What does it have over Repo.get_by(Schema, pk1: pk1, pk2: pk2)?

Eiji

Eiji

Yes, it’s because __CALLER__ is not yet compiled. You can create a MyApp.Queries module and pass MyApp.MySchema as second argument instead.

I’m writing it from memory, so I could make a mistake, but it should work:

defmodule Example do
  defmacro defquery(type, module_ast) do
    module = Macro.expand(module_ast, __ENV__)
    primary_keys = module.__schema__(:primary_key)
    module_snake_case = module |> Module.split() |> List.last() |> Macro.underscore()
    call_args = Enum.map(primary_keys, &Macro.var(&1, module))
    func_name = :"#{type}_#{module_snake_case}"
    repo_args = Enum.map(call_args, &{elem(&1, 0), &1})
    repo_func = get_repo_func(type)

    quote do
      def unquote(func_name)(unquote_splicing(call_args)) do
        apply(MyApp.Repo, unquote(repo_func), [unquote(module), unquote(repo_args)])
      end
    end
  end

  defp get_repo_func(:get), do: :get_by
end

defmodule MyApp.Queries do
  require Example

  Example.defquery(:get, MyApp.MySchema)
end

MyApp.Queries.get_my_schema(…)

This is fully generic macro. Feel free to edit it as you wish. :slight_smile:

It’s also possible to change this code to work inside MyApp.MySchema, but that’s not recommend as such modules are designed to handle different things (field definitions, changesets etc.) and business logic should be inside context modules. That was already well described on forum.

For me it looks like that author want to have instead: Schema.get(pk1, pk2). For me it does not looks that bad, because in Repo.get_by/2 you don’t know which keys are primary (h helper in iex and on documentation level). Here you could see them in generated documentation.

The only problem is that author probably want to generate such functions inside modules with schema definition. Instead I proposed example with same functions, but in context module.

In order words instead of:

MyApp.Repo.get_by(MyApp.Comment, [id: comment_id])
MyApp.Repo.get_by(MyApp.PostComment, [comment_id: comment_id, post_id: post_id])
MyApp.Repo.get_by(MyApp.Post, [id: post_id])

author wants:

MyApp.Comment.get(comment_id)
MyApp.Post.get(post_id)
MyApp.PostComment.get(comment_id, post_id)

and my macro allows to call like:

MyApp.Queries.get_comment(comment_id)
MyApp.Queries.get_post(post_id)
MyApp.Queries.get_post_comment(comment_id, post_id)
idi527

idi527

Allows for an easy typo, get_by is better at least in that regard. Also, I’d make it get_post_comment(post_id, comment_id)?

Eiji

Eiji

Not sure if I remember it correctly, but when using such generator then thing which matter is order of field definition (assuming that there is no sorting in generator etc.), so for example:

defmodule MyApp.PostComment do
  use Ecto.Schema

  @primary_key false
  schema "posts_comments" do
    belongs_to :comment, MyApp.Comment, primary_key: true
    belongs_to :post, MyApp.Post, primary_key: true
  end

  # …
end

would give: MyApp.PostComment.__schema__(:primary_key) == [:comment_id, :post_id]

and simply changing fields (here belongs_to calls) order like:

defmodule MyApp.PostComment do
  use Ecto.Schema

  @primary_key false
  schema "posts_comments" do
    belongs_to :post, MyApp.Post, primary_key: true
    belongs_to :comment, MyApp.Comment, primary_key: true
  end

  # …
end

would give: MyApp.PostComment.__schema__(:primary_key) == [:post_id, :comment_id].

Again I wrote it from memory and it could not even compile due to typo or other small mistake.

Also I’m not sure if get_by is better in this case. In modern (of course well supported by Elixir extension/plugin) editor and in app with no similar model names (like MyApp.Worlds and MyApp.Words) there should not be any problem as editor should give proper hints and auto completion. Same goes to arguments order as they would be corrected by editor app.

Finally I’m not sure if post_id (always) should be before comment_id. For lots of cases order really matters, for example it’s easier to write pipe calls. However in cases like here (many-to-many assoc model) we can go from comment_id as well as from post_id - everything depends on scenario and I don’t think that force change of order is best idea (again in this particular case).

Keep in mind that Comment and Post are just most popular in database examples, so even if you would always go from Post and never from Comment then everything change in real world app where both of sides are important unlike Comment which is nice to have enhancement to Post.

Of course it’s only how I think and it’s definitely not an rule from senior developer. :slight_smile:

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews