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
RequestContext middleware breaks entire Koa application #709
Comments
During further research, I've also managed to find a solution with I also noticed they have two distinct middleware implementations. |
Weird, can you verify (by adjusting the ORM code in node_modules) that it is actually caused by this? If so, we can add Btw I would not suggest to use cls-hooked (at least not for production). When I was testing it some time ago (might be more than a year, but I don't see any commits in last year either), it was causing memory leaks (just like plain |
I'm not sure if this is what you had in mind, but I was able to change the code so that the issue no longer happens. This is my approach: static create(em) {
return async function(ctx, next) {
const context = new RequestContext(em.fork(true, true));
const d = domain_1.default.create();
d.__mikro_orm_context = context;
await new Promise(function(resolve, reject) {
d.run(() => next().then(resolve).catch(reject));
});
}
} The usage was adjusted as: Thanks for the note regarding cls-hooked. That probably saved me some headaches in the long run. Preferably, I'd stick with MikroORM approaches for this specific area, but I like the idea. In the past we'd often blindly attach properties to the |
Thanks, will try to create example repository for Koa too when debugging this to ensure it really works. |
If the `next` handler needs to be awaited (like in Koa), one can now use `RequestContext.createAsync()` method. Closes #709
|
If the `next` handler needs to be awaited (like in Koa), one can now use `RequestContext.createAsync()` method. Closes #709
Works great. Migration to v4 was also smooth so far. |
Describe the bug
When registering a middleware to use the
RequestContext
, it will cause previously existing routes to be answered with a 404 reply.To Reproduce
Register the middleware per the documentation:
Expected behavior
You can later retrieve the forked entity manager through the context and your application generally still works as expected.
Additional context
My personal understanding of the fault is:
RequestContext
expectsnext
to be synchronous and will call it as such and not await the result. This allows the request to finalize without ever having been handled. The handlers will still run, but they have no effect on the result, which was already dispatched.It appears as if the current approach is modeled specifically around express middlewares, which behave differently from Koa middlewares.
A current workaround I found for myself was to just register the EntityManager on the context (something I'd like to avoid obviously) myself:
Versions
The text was updated successfully, but these errors were encountered: