defql icon indicating copy to clipboard operation
defql copied to clipboard

Some how enable Struct returns

Open cgarciae opened this issue 8 years ago • 5 comments
trafficstars

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 [%{...}]

cgarciae avatar Feb 04 '17 02:02 cgarciae

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.

implicitly-awesome avatar Feb 04 '17 12:02 implicitly-awesome

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

fazibear avatar Feb 04 '17 12:02 fazibear

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.

implicitly-awesome avatar Feb 04 '17 12:02 implicitly-awesome

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

fazibear avatar Feb 04 '17 12:02 fazibear

@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.

cgarciae avatar Feb 04 '17 12:02 cgarciae