Skip to content

Commit b87d774

Browse files
author
Slava Fomin II
committed
Refactored and simplified the decorators.
1 parent 7bd2e12 commit b87d774

File tree

6 files changed

+33
-141
lines changed

6 files changed

+33
-141
lines changed

src/decorators/OrmConnection.ts

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,19 @@
11
import {ConnectionManager} from "typeorm";
22
import {Container} from "typedi";
3-
import {Handler} from "typedi/types/Handler";
43

54
/**
65
* Allows to inject an Connection using typedi's Container.
76
*/
87
export function OrmConnection(connectionName: string = "default"): Function {
9-
return function(target: Object|Function, propertyName: string, index?: number) {
10-
11-
const getValue = () => {
8+
return function(object: Object|Function, propertyName: string, index?: number) {
9+
Container.registerHandler({ object, index, propertyName, value: () => {
1210
const connectionManager = Container.get(ConnectionManager);
1311
if (!connectionManager.has(connectionName))
1412
throw new Error(`Cannot get connection "${connectionName}" from the connection manager. ` +
15-
`Make sure you have created such connection. Also make sure you have called useContainer(Container) ` +
16-
`in your application before you established a connection and importing any entity.`);
13+
`Make sure you have created such connection. Also make sure you have called useContainer(Container) ` +
14+
`in your application before you established a connection and importing any entity.`);
1715

1816
return connectionManager.get(connectionName);
19-
};
20-
21-
let handler = <Handler> {
22-
object: target,
23-
value: getValue
24-
};
25-
26-
if (index !== undefined) {
27-
handler.index = index;
28-
}
29-
30-
if (propertyName !== undefined) {
31-
handler.propertyName = propertyName;
32-
}
33-
34-
Container.registerHandler(handler);
35-
17+
}});
3618
};
3719
}

src/decorators/OrmCustomRepository.ts

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,21 @@
11
import { ConnectionManager } from "typeorm";
22
import { Container } from "typedi";
3-
import {Handler} from "typedi/types/Handler";
43

54
/**
65
* Allows to inject a custom Repository using typedi's Container.
76
* Use it to get the repository class decorated with @EntityRepository decorator.
87
*/
98
export function OrmCustomRepository(cls: Function, connectionName: string = "default"): Function {
10-
return function(target: Object|Function, propertyName: string, index?: number) {
11-
12-
const getValue = () => {
9+
return function(object: Object|Function, propertyName: string, index?: number) {
10+
Container.registerHandler({ object, index, propertyName, value: () => {
1311
const connectionManager = Container.get(ConnectionManager);
1412
if (!connectionManager.has(connectionName))
1513
throw new Error(`Cannot get connection "${connectionName}" from the connection manager. ` +
16-
`Make sure you have created such connection. Also make sure you have called useContainer(Container) ` +
17-
`in your application before you established a connection and importing any entity.`);
14+
`Make sure you have created such connection. Also make sure you have called useContainer(Container) ` +
15+
`in your application before you established a connection and importing any entity.`);
1816

1917
const connection = connectionManager.get(connectionName);
2018
return connection.getCustomRepository(cls as any);
21-
};
22-
23-
let handler = <Handler> {
24-
object: target,
25-
value: getValue
26-
};
27-
28-
if (index !== undefined) {
29-
handler.index = index;
30-
}
31-
32-
if (propertyName !== undefined) {
33-
handler.propertyName = propertyName;
34-
}
35-
36-
Container.registerHandler(handler);
37-
19+
}});
3820
};
3921
}

src/decorators/OrmEntityManager.ts

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,25 @@
11
import {ConnectionManager} from "typeorm";
22
import {Container} from "typedi";
3-
import {Handler} from "typedi/types/Handler";
43

54
/**
65
* Allows to inject an EntityManager using typedi's Container.
76
*/
87
export function OrmEntityManager(connectionName: string = "default"): Function {
9-
return function(target: Object|Function, propertyName: string, index?: number) {
10-
11-
const getValue = () => {
8+
return function(object: Object|Function, propertyName: string, index?: number) {
9+
Container.registerHandler({ object, index, propertyName, value: () => {
1210
const connectionManager = Container.get(ConnectionManager);
1311
if (!connectionManager.has(connectionName))
1412
throw new Error(`Cannot get connection "${connectionName}" from the connection manager. ` +
15-
`Make sure you have created such connection. Also make sure you have called useContainer(Container) ` +
16-
`in your application before you established a connection and importing any entity.`);
13+
`Make sure you have created such connection. Also make sure you have called useContainer(Container) ` +
14+
`in your application before you established a connection and importing any entity.`);
1715

1816
const connection = connectionManager.get(connectionName);
1917
const entityManager = connection.entityManager;
2018
if (!entityManager)
2119
throw new Error(`Entity manager was not found on "${connectionName}" connection. ` +
22-
`Make sure you correctly setup connection and container usage.`);
20+
`Make sure you correctly setup connection and container usage.`);
2321

2422
return entityManager;
25-
};
26-
27-
let handler = <Handler> {
28-
object: target,
29-
value: getValue
30-
};
31-
32-
if (index !== undefined) {
33-
handler.index = index;
34-
}
35-
36-
if (propertyName !== undefined) {
37-
handler.propertyName = propertyName;
38-
}
39-
40-
Container.registerHandler(handler);
41-
23+
}});
4224
};
4325
}

src/decorators/OrmRepository.ts

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,21 @@
11
import {ConnectionManager} from "typeorm";
22
import {Container} from "typedi";
3-
import {Handler} from "typedi/types/Handler";
43

54
/**
65
* Allows to inject a Repository using typedi's Container.
76
* If you want to inject custom Repository class decorated with @EntityRepository decorator, use OrmCustomRepository instead.
87
*/
98
export function OrmRepository(cls: Function, connectionName: string = "default"): Function {
10-
return function(target: Object|Function, propertyName: string, index?: number) {
11-
12-
const getValue = () => {
9+
return function(object: Object|Function, propertyName: string, index?: number) {
10+
Container.registerHandler({ object, index, propertyName, value: () => {
1311
const connectionManager = Container.get(ConnectionManager);
1412
if (!connectionManager.has(connectionName))
1513
throw new Error(`Cannot get connection "${connectionName}" from the connection manager. ` +
16-
`Make sure you have created such connection. Also make sure you have called useContainer(Container) ` +
17-
`in your application before you established a connection and importing any entity.`);
14+
`Make sure you have created such connection. Also make sure you have called useContainer(Container) ` +
15+
`in your application before you established a connection and importing any entity.`);
1816

1917
const connection = connectionManager.get(connectionName);
2018
return connection.getRepository(cls as any);
21-
};
22-
23-
let handler = <Handler> {
24-
object: target,
25-
value: getValue
26-
};
27-
28-
if (index !== undefined) {
29-
handler.index = index;
30-
}
31-
32-
if (propertyName !== undefined) {
33-
handler.propertyName = propertyName;
34-
}
35-
36-
Container.registerHandler(handler);
37-
19+
}});
3820
};
3921
}
Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,20 @@
11
import {ConnectionManager} from "typeorm";
22
import {Container} from "typedi";
3-
import {Handler} from "typedi/types/Handler";
43

54
/**
65
* Allows to inject a SpecificRepository using typedi's Container.
76
*/
87
export function OrmSpecificRepository(cls: Function, connectionName: string = "default"): Function {
9-
return function(target: Object|Function, propertyName: string, index?: number) {
10-
11-
const getValue = () => {
8+
return function(object: Object|Function, propertyName: string, index?: number) {
9+
Container.registerHandler({ object, index, propertyName, value: () => {
1210
const connectionManager = Container.get(ConnectionManager);
1311
if (!connectionManager.has(connectionName))
1412
throw new Error(`Cannot get connection "${connectionName}" from the connection manager. ` +
15-
`Make sure you have created such connection. Also make sure you have called useContainer(Container) ` +
16-
`in your application before you established a connection and importing any entity.`);
13+
`Make sure you have created such connection. Also make sure you have called useContainer(Container) ` +
14+
`in your application before you established a connection and importing any entity.`);
1715

1816
const connection = connectionManager.get(connectionName);
1917
return connection.getSpecificRepository(cls as any);
20-
};
21-
22-
let handler = <Handler> {
23-
object: target,
24-
value: getValue
25-
};
26-
27-
if (index !== undefined) {
28-
handler.index = index;
29-
}
30-
31-
if (propertyName !== undefined) {
32-
handler.propertyName = propertyName;
33-
}
34-
35-
Container.registerHandler(handler);
36-
18+
}});
3719
};
38-
}
20+
}

src/decorators/OrmTreeRepository.ts

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,20 @@
11
import {ConnectionManager} from "typeorm";
22
import {Container} from "typedi";
3-
import {Handler} from "typedi/types/Handler";
43

54
/**
65
* Allows to inject a TreeRepository using typedi's Container.
76
*/
87
export function OrmTreeRepository(cls: Function, connectionName: string = "default"): Function {
9-
return function(target: Object|Function, propertyName: string, index?: number) {
10-
11-
const getValue = () => {
8+
return function(object: Object|Function, propertyName: string, index?: number) {
9+
Container.registerHandler({ object, index, propertyName, value: () => {
1210
const connectionManager = Container.get(ConnectionManager);
1311
if (!connectionManager.has(connectionName))
1412
throw new Error(`Cannot get connection "${connectionName}" from the connection manager. ` +
15-
`Make sure you have created such connection. Also make sure you have called useContainer(Container) ` +
16-
`in your application before you established a connection and importing any entity.`);
13+
`Make sure you have created such connection. Also make sure you have called useContainer(Container) ` +
14+
`in your application before you established a connection and importing any entity.`);
1715

1816
const connection = connectionManager.get(connectionName);
1917
return connection.getTreeRepository(cls as any);
20-
};
21-
22-
let handler = <Handler> {
23-
object: target,
24-
value: getValue
25-
};
26-
27-
if (index !== undefined) {
28-
handler.index = index;
29-
}
30-
31-
if (propertyName !== undefined) {
32-
handler.propertyName = propertyName;
33-
}
34-
35-
Container.registerHandler(handler);
36-
18+
}});
3719
};
38-
}
20+
}

0 commit comments

Comments
 (0)