/
OrmRepository.ts
29 lines (25 loc) · 1.03 KB
/
OrmRepository.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import {ConnectionManager} from "../connection/ConnectionManager";
// todo: should this decorator be outside of this module?
// todo: also create "inject" version of this to allow to inject to properties
/**
* Allows to inject a Repository using typedi's Container.
*/
export function OrmRepository(cls: Function, connectionName?: string): Function {
return function(target: Function, key: string, index: number) {
let container: any;
try {
container = require("typedi/Container").Container;
} catch (err) {
throw new Error("OrmRepository cannot be used because typedi extension is not installed.");
}
container.registerParamHandler({
type: target,
index: index,
getValue: () => {
const connectionManager: ConnectionManager = container.get(ConnectionManager);
const connection = connectionManager.getConnection(connectionName);
return connection.getRepository(cls);
}
});
};
}