-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
API for extending context #473
Comments
+1 I think there are plugins & middlewares, plugins just do extend request/reponse/context stuff. And they
|
when we were building this, @tj didn't want for it to be public. i use it all the time, but i don't want people to abuse it. @koajs/owners ? |
-1 on extending the shared object request/response/context without any sandbox or namespace. I used to share something by using |
+1 for @magicdawn's way. Extend const app = koa();
// ... add yours middlewares whatever
// extend whatever you want
app.db = createMySuperDBClient();
app.context.render = myReader;
// or pass the app to your koa modules
someFeatureModule(app); |
it seems like a standardized approach will cause more conflicts than everyone doing it their own way |
yea extending the context is nicer than having middleware plop stuff on the context, so I'm +1 for having the object doc'd |
+1, I like this way that outside the middleware. |
and here i thought it was already public haha +1 On Thu, Aug 27, 2015 at 11:01 AM, Fangdun Cai notifications@github.com
Aaron |
Sounds good, especially since it's already being used by so many middleware. In the particular case described by @cesarandreu, I feel that it solves only part of the problem though - if you have a service layer outside the http request/response cycle and you need to access the same db client/connection instance, how do you tie that with koa? |
+1 to have this and move that kind of stuff off the middleware |
would anyone like to make a PR w/ docs? |
e710b4b is this sufficient for y'all? |
LGTM On Sun, Aug 30, 2015 at 10:04 PM, jongleberry notifications@github.com
Aaron |
As an application developer, I want to extend the context with app globals, such as database instance, model instances, and other client instances (e.g. s3, redis, etc). I prefer this approach over actual globals or singletons because those options are more difficult to test.
I made koa-add-to-context, a middleware that mixes in keys into the request's context. However, since these values are all static, instead I could just do something like:
This would be more efficient, since it lets me avoid a middleware on every request. The problem is that I'm not sure if
app.context
is considered a public API or not. If it's not, I'd propose we add an API for enhancing the application context. Thoughts?The text was updated successfully, but these errors were encountered: