Skip to content

Commit 3f13dc7

Browse files
feat: add support for scoped containers
1 parent 2a650f1 commit 3f13dc7

File tree

3 files changed

+13
-8
lines changed

3 files changed

+13
-8
lines changed

src/decorators/InjectConnection.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ export function InjectConnection(connectionName: string = 'default'): Function {
1010
object: object as Constructable<unknown>,
1111
index,
1212
propertyName,
13-
value: () => {
14-
const connectionManager = Container.get(ConnectionManager);
13+
value: containerInstance => {
14+
const connectionManager = containerInstance.get(ConnectionManager);
1515
if (!connectionManager.has(connectionName))
1616
throw new Error(
1717
`Cannot get connection "${connectionName}" from the connection manager. ` +

src/decorators/InjectManager.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ export function InjectManager(connectionName: string = 'default'): Function {
1010
object: object as Constructable<unknown>,
1111
index,
1212
propertyName,
13-
value: () => {
14-
const connectionManager = Container.get(ConnectionManager);
13+
value: containerInstance => {
14+
const connectionManager = containerInstance.get(ConnectionManager);
1515
if (!connectionManager.has(connectionName))
1616
throw new Error(
1717
`Cannot get connection "${connectionName}" from the connection manager. ` +

src/decorators/InjectRepository.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ConnectionManager, Repository, TreeRepository, MongoRepository } from 'typeorm';
2-
import { Constructable, Container } from 'typedi';
2+
import { Constructable, Container, ContainerInstance } from 'typedi';
33

44
import { EntityTypeMissingError } from '../errors/EntityTypeMissingError';
55
import { PropertyTypeMissingError } from '../errors/PropertyTypeMissingError';
@@ -8,8 +8,13 @@ import { ParamTypeMissingError } from '../errors/ParamTypeMissingError';
88
/**
99
* Helper to avoid V8 compilation of anonymous function on each call of decorator.
1010
*/
11-
function getRepository(connectionName: string, repositoryType: Function, entityType: Function) {
12-
const connectionManager = Container.get(ConnectionManager);
11+
function getRepository(
12+
connectionName: string,
13+
repositoryType: Function,
14+
entityType: Function,
15+
containerInstance: ContainerInstance
16+
) {
17+
const connectionManager = containerInstance.get(ConnectionManager);
1318
if (!connectionManager.has(connectionName)) {
1419
throw new Error(
1520
`Cannot get connection "${connectionName}" from the connection manager. ` +
@@ -159,7 +164,7 @@ export function InjectRepository(
159164
index,
160165
object: object as Constructable<unknown>,
161166
propertyName,
162-
value: () => getRepository(connectionName, repositoryType, entityType!),
167+
value: containerInstance => getRepository(connectionName, repositoryType, entityType!, containerInstance),
163168
});
164169
};
165170
}

0 commit comments

Comments
 (0)