You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This issue is more of an idea than a well-formatted issue. I wrote a meta-function in one of my projects which would really work as a core SwissSchema fn. Here's now it looks overall:
It's supposed to be used like this: you would pass a function of typespec (Ecto.Query.t() -> Ecto.Query.t()), receive a pre-formatted query (Ecto.Query), and augment it with your own querying criteria:
User.query(fnquery->query# Apply your own WHERE criteria|>where([u],u.username=="joeljuca")# Join the user table with other tables|>join(:left,[u],cinComment,on: c.user_id==u.id)# Order, limit, etc.|>order_by([u],[desc: u.inserted_at])|>limit(100)end)
I think this function needs some mechanism to provide a default function to prepare the query, as well as a way to call query/2 w/o this preparation (smt like User.query(fn q -> ... end, raw: true)? I'm not sure yet).
The main thing here is: I want to avoid creating very specific querying functions (eg.: get_user_by_this/1, get_user_by_that/1, get_users_by_smt_else/2`, etc.), and I'd like to avoid creating these complex functions with a huge map with params for each situation:
I never loved this function style! The reason? Well, it's pretty much a DSL (Domain-Specific Language) for a single function! You need to remember the syntax of all these params and their specifics – and I never recall which one goes where, and their specific syntax, etc.
Well, Ecto is awesome – and Ecto.Query is an amazing DSL for writing SQL code in Elixir. I'd like to just using wherever and whenever I need. Hence query/2 was born. 😄
The text was updated successfully, but these errors were encountered:
This issue is more of an idea than a well-formatted issue. I wrote a meta-function in one of my projects which would really work as a core SwissSchema fn. Here's now it looks overall:
It's supposed to be used like this: you would pass a function of typespec
(Ecto.Query.t() -> Ecto.Query.t())
, receive a pre-formatted query (Ecto.Query), and augment it with your own querying criteria:I think this function needs some mechanism to provide a default function to prepare the query, as well as a way to call
query/2
w/o this preparation (smt likeUser.query(fn q -> ... end, raw: true)
? I'm not sure yet).The main thing here is: I want to avoid creating very specific querying functions (eg.:
get_user_by_this/1
,get_user_by_that/1
, get_users_by_smt_else/2`, etc.), and I'd like to avoid creating these complex functions with a huge map with params for each situation:I never loved this function style! The reason? Well, it's pretty much a DSL (Domain-Specific Language) for a single function! You need to remember the syntax of all these params and their specifics – and I never recall which one goes where, and their specific syntax, etc.
Well, Ecto is awesome – and Ecto.Query is an amazing DSL for writing SQL code in Elixir. I'd like to just using wherever and whenever I need. Hence
query/2
was born. 😄The text was updated successfully, but these errors were encountered: