Looking to implement a request-scoped cache for my repositories, inspired by Hibernate's first-level-cache. I have some ideas on how to achieve this and integrate it with typeorm-transactional-cls-hooked.
For now, I've created a basic provider setup like so:
@Injectable({ scope: Scope.REQUEST })
export class RequestScopedCache extends Object {
private storage: any = {};
public set(key: string, value: any) {
this.storage[key] = value;
}
public get(key: string) {
return this.storage[key];
}
I attempted to inject this provider into a custom repository:
@Injectable()
@EntityRepository(Enqueued)
export class EnqueuedRepository extends Repository<Enqueued> {
@Inject() readonly cache: RequestScopedCache;
public async get(id: string) {
const key = `${this.constructor.name}_${id}`;
const result = this.cache.get(key);
if (result) {
return result;
}
const dbResult = await super.findOne(id);
this.cache.set(key, dbResult);
return dbResult;
}
}
Unfortunately, neither constructor injection nor property injection seem to work in the custom repository. It seems that a typeorm-specific constructor (which appears to be private) is being called with the first injected parameter being a connection.
Subsequently, I tried property injection, but that also didn't yield results.
Is there a way to successfully inject custom configuration into a custom repository?