I want to use Facebook's DataLoader with Koa2 in Typescript. I want per-request DataLoader instances to go with my per-request database connections. How is this best achieved?
My current approach is to augment the Koa2 context but I'm failing because I don't know how to fix the type definition.
Here is my attempt at module augmentation...
import 'koa';
declare module 'koa' {
namespace Application {
interface BaseContext {
dataLoader(): any;
}
}
}
Application.BaseContext.prototype.dataLoader = function() {
console.log("Cannot find name 'Application' at line 11 col 1");
}
In addition to the error shown in the log call, I also get Property 'dataLoader' does not exist on type 'BaseContext'
when I import the above and attempt to call dataLoader
.
Cheers
Just figured out an approach that doesn't require any type-hacking:
interface ICustomAppContext {
mySlowToInitializeClient: string;
}
interface ICustomAppState {
poop: string;
}
const app = new Koa<ICustomAppState, ICustomAppContext>();
See https://github.com/DefinitelyTyped/DefinitelyTyped/blob/b081a5b7d0db7181901d7834f8a85206af263094/types/koa/index.d.ts#L434 for details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With