Having queries in the schema? Thoughts?

ecto-query
schema
#1

Hey there,

I am currently reading Programming Phoenix 1.4 and it says that the non generic, more complex queries should be in the schemas.
I thought I would like to ask the people what do they think about this, in my previous project I had these in the controller, as the book mentions it too, and even though I was aware that that approach might not be the best, I was stunned by reading that it should be in the schema.

What do you guys think? What are the pros and cons?

Thanks

0 Likes

#2

It’s important to note that the query is not the execution of the query. A query is just a data structure, so putting it in the schema where all the functions are pure is fine. I think it’s the best place to store a shared query between multiple controller actions. I may sometimes choose to write a query in the context however, if there’s no point in sharing it. I try to make the web layer unaware of any database stuff and have it deal only with web concerns and calling context functions.

2 Likes