3

I am trying to create an application with mikro-orm and apollo-server-express, I want to use the batch processing and the caching of the Facebook dataloader.

Normally, Facebook dataloader instances are creates per request. If mikro-orm also creates custom repository instances per request and if all calls to EntityManager.getRepository() in the same request gets the same instance, it may be the perfect place to create the dataloader instances.

4

1 回答 1

2

存储库是作为单例创建的,因此每个实例仅存在一个EntityManager实例。您应该手动或通过RequestContext中间件分叉此 EM,以便每个请求拥有一个实例:

https://b4nan.github.io/mikro-orm/identity-map/

这样,每个请求都将拥有自己的EntityManager,将拥有自己的存储库实例缓存。

请记住,如果您使用RequestContext,您应该从中获取特定的请求EntityManager,并从那里获取存储库:

// beware that this will return null if the context is not yet started
const em = RequestContext.getEntityManager();

// gets request specific repository instance
const repo = em.getRepository(Book);
于 2019-04-03T08:27:45.930 回答