fazibear / defql

Create elixir functions with SQL as a body.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Some how enable Struct returns

cgarciae opened this issue · comments

Would it be possible to pass the Module into the using statement so the queries return a Struct? Example:

defmodule UserQuery do
  alias MyProject.User
  use Defql, model: User
 
  defquery get_by_blocked(blocked) do
    "SELECT * FROM users WHERE blocked = $blocked"
  end
end

Ideally get_by_id would return [%User{...}] instead of [%{...}]

What is MyProject.User? Is it just a structure or Ecto schema (I ask because you use the attr model: User, when (imho) it's better to use struct: %User{} in order not to mix definitions)?

It's controversial, personally I don't think there is any need to reinvent Ecto. But, sometimes it might be useful, for sure.

What is an advantage using structs here? Notice that you can change column names, you can join tables so you will have additional fields.

I see only one advantage at the moment: if returned data strictly conforms a structure, you haven't cast it manually then. But, I'm for keeping defql's query result as simple as possible. Just data.

Anyway, Idea behind Ecto Adapter was to use the connection only. Any other Ecto integration is not planned.

If you want to wrap result into ecto struct you'll find more informations here: http://stackoverflow.com/questions/27751216/how-to-use-raw-sql-with-ecto-repo

@madeinussr struct: User is fine, I was think about simple stucts. The this feature would potentially avoid a Enum.map with a cast fn after every call to a defquery defined function, and could be made optional so people that want to use simple maps can do so.

The reason I would like to have this feature is because you can use Protocols on structs.