Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query hook before execution #93

Open
nalbeza opened this issue Aug 30, 2014 · 4 comments
Open

Query hook before execution #93

nalbeza opened this issue Aug 30, 2014 · 4 comments

Comments

@nalbeza
Copy link

nalbeza commented Aug 30, 2014

I need to alter the query made by methods such as page and resource before execution, to gain finer control over transactions, for example.

Do you support inclusion of this feature into your project ?

I'd be willing to discusss technical details and PR.

@GavinJoyce
Copy link
Contributor

Can you give an example of what you would like to modify?

@nalbeza
Copy link
Author

nalbeza commented Aug 30, 2014

I'd like to allow something along the lines of:

AlbumSerializer.page(params, Albums.where("year < 1950")) do |query|
  # query has not been executed yet
  results = Albums.transaction do
    # Query whatever you want
    SomeModel.some_query
    next query.load
  end
  # Do something with results, or not
 next results
end

From what i've seen, we would at least need to defer sideloading queries (use eager_load ?) until callback has been called.
But you must know what would need modifications better than me :-)

@GavinJoyce
Copy link
Contributor

Thanks. I can't yet think of a reason why I'd need this in my applications. What's your use case?

@nalbeza
Copy link
Author

nalbeza commented Aug 30, 2014

For example, wrap the query in a transaction (like above).

Separating query parsing/generation/execution would also help to plug other DB adapters (Elasticsearch for example), but that's probably outside the scope of this issue.

Maybe explaining the full use case would help to to make this clearer :
I'm building an ember app, and wants to use JSONAPI spec to communicate with the RoR+Grape backend.
I also need to extend the spec to support more use cases (domain specific queries, etc).
Multiples databases/sources are used (postgres, elasticsearch, maybe more), so flexibility in that area is required.
Also, the subject of this issue, being able to wrap the query execution in custom code, to run it in a transaction and/or execute additional queries (enqueue mail job for example).

Thank you for your answers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants