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

Access context and params in objectionjs hooks? #99

Open
avimar opened this issue Jun 10, 2020 · 2 comments
Open

Access context and params in objectionjs hooks? #99

avimar opened this issue Jun 10, 2020 · 2 comments
Assignees

Comments

@avimar
Copy link

avimar commented Jun 10, 2020

Objection has it's own hooks. Feathers is already passing in params which can be given to objection's context() function. It has the authentication information, when available.

https://github.com/feathersjs-ecosystem/feathers-objection/blob/master/src/index.js#L276
Would become: const q = this._createQuery(params).context(params).skipUndefined(); and is accessible in queryContext from many hooks

What about the full app for accessing other things? I know this is probably crazy, but I'm experimenting with different ways of writing my logic, and doing some of it at the model seems to make some sense.

I'm not familiar enough with classes - I'm pretty sure we're passing in app, that's how we can use the config to connect to the DB, but how can I access it within the class again later? e.g. to call other services or trigger events.

Thanks!

@dekelev
Copy link
Member

dekelev commented Jun 14, 2020

@avimar Thanks, I'll consider adding support for ObjectionJS context method.

A model is usually not used to call other services or trigger events and I wouldn't use the app instance in a model in my app since it prevents the model from being shared as relation for model in another app.

For separation of concepts, interaction between services and business logic should be applied in the service's hooks file.

The hooks methods in a model file are usually used to set "auto-generated" columns.

You can also use the $modify query operator to apply shared common query snippets.

@dekelev dekelev self-assigned this Jun 14, 2020
@alex-all3dp
Copy link
Contributor

alex-all3dp commented Jan 26, 2021

👍 for adding support for the .context() method! :)
One possible use-case on our end would be to pass request information to objection hooks like $beforeInsert

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

No branches or pull requests

3 participants