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

API for extending context #473

Closed
cesarandreu opened this issue Aug 25, 2015 · 13 comments
Closed

API for extending context #473

cesarandreu opened this issue Aug 25, 2015 · 13 comments

Comments

@cesarandreu
Copy link

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:

Object.assign(app.context, {
  models: models,
  db: db,
  s3: s3
})

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?

@magicdawn
Copy link

+1

I think there are plugins & middlewares, plugins just do extend request/reponse/context stuff. And they

avoid a middleware on every request

https://cnodejs.org/topic/553f8c242bd4939b1e905587

@jonathanong
Copy link
Member

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 ?

@yorkie
Copy link
Member

yorkie commented Aug 27, 2015

-1 on extending the shared object request/response/context without any sandbox or namespace. I used to share something by using context.{my-namespace}.{custom-key}.

@fengmk2
Copy link
Member

fengmk2 commented Aug 27, 2015

+1 for @magicdawn's way.

Extend app, context, request and response before your app instance start to listen a port.

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);

@juliangruber
Copy link
Contributor

it seems like a standardized approach will cause more conflicts than everyone doing it their own way

@tj
Copy link
Member

tj commented Aug 27, 2015

yea extending the context is nicer than having middleware plop stuff on the context, so I'm +1 for having the object doc'd

@fundon
Copy link
Contributor

fundon commented Aug 27, 2015

+1, I like this way that outside the middleware.

@aheckmann
Copy link
Member

and here i thought it was already public haha +1

On Thu, Aug 27, 2015 at 11:01 AM, Fangdun Cai notifications@github.com
wrote:

+1, I like this way that outside the middleware.


Reply to this email directly or view it on GitHub
#473 (comment).

Aaron
@aaronheckmann https://twitter.com/#!/aaronheckmann
soundcloud.com/ajhecky
github.com/aheckmann

@ruimarinho
Copy link
Contributor

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?

@travisjeffery
Copy link
Member

+1 to have this and move that kind of stuff off the middleware

@jonathanong
Copy link
Member

would anyone like to make a PR w/ docs?

@jonathanong
Copy link
Member

e710b4b is this sufficient for y'all?

@aheckmann
Copy link
Member

LGTM

On Sun, Aug 30, 2015 at 10:04 PM, jongleberry notifications@github.com
wrote:

e710b4b
e710b4b
is this sufficient for y'all?


Reply to this email directly or view it on GitHub
#473 (comment).

Aaron
@aaronheckmann https://twitter.com/#!/aaronheckmann
soundcloud.com/ajhecky
github.com/aheckmann

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