koajs / session

Simple session middleware for koa
MIT License
901 stars 113 forks source link

Feature Request: pass ctx on to custom store methods #144

Closed lehni closed 5 years ago

lehni commented 6 years ago

I have a koa middleware that handles the creation of database transactions before, and their committing / rolling back after the execution of other middlewares. The transaction object is exposed on the ctx object that is passed to all middlewares as ctx.trx

Since the session itself is also stored in the database, it would be nice if the various custom store methods could receive the associated ctx object, so this transaction could also be used to tie the session changes into the same transaction.

Some of the store methods already receive a third object argument with various parameters (changed and rolling so far), could ctx be added there as well? And could this argument also be passed to store.destroy()?

If this is agreeable, I'd be happy to create a PR for it.

ngot commented 5 years ago

If I understand you correctly, you can use opts.ContextStore instead of opts.store.

ContextStore must be a class which claims three instance methods demonstrated above. new ContextStore(ctx) will be executed on every request.

lehni commented 5 years ago

@ngot you're right, that will solve it, thanks!