Skip to content

Commit cdfdd84

Browse files
authored
Merge pull request #11 from slavafomin/master
Decorators updated to use latest TypeDI version
2 parents 9f51658 + b87d774 commit cdfdd84

File tree

8 files changed

+39
-79
lines changed

8 files changed

+39
-79
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
.idea/
12
node_modules/
23
build/

package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"typeorm-typedi"
2424
],
2525
"devDependencies": {
26+
"@types/node": "^7.0.4",
2627
"chai": "^3.5.0",
2728
"del": "^2.2.2",
2829
"gulp": "^3.9.1",
@@ -33,16 +34,16 @@
3334
"gulpclass": "^0.1.1",
3435
"pg": "^6.1.0",
3536
"reflect-metadata": "^0.1.9",
37+
"sinon-chai": "^2.9.0",
3638
"ts-node": "^1.3.0",
3739
"tslint": "^3.15.1",
3840
"tslint-stylish": "^2.1.0-beta",
39-
"typedi": "^0.4.2",
41+
"typedi": "^0.5.0",
4042
"typeorm": "^0.0.11",
41-
"typescript": "^2.1.5",
42-
"@types/node": "^7.0.4"
43+
"typescript": "^2.1.5"
4344
},
4445
"peerDependencies": {
4546
"typeorm": "^0.0.11",
46-
"typedi": "^0.4.2"
47+
"typedi": "^0.5.0"
4748
}
4849
}

src/decorators/OrmConnection.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,15 @@ import {Container} from "typedi";
55
* Allows to inject an Connection using typedi's Container.
66
*/
77
export function OrmConnection(connectionName: string = "default"): Function {
8-
return function(target: Object|Function, propertyName: string, index?: number) {
9-
10-
const getValue = () => {
8+
return function(object: Object|Function, propertyName: string, index?: number) {
9+
Container.registerHandler({ object, index, propertyName, value: () => {
1110
const connectionManager = Container.get(ConnectionManager);
1211
if (!connectionManager.has(connectionName))
1312
throw new Error(`Cannot get connection "${connectionName}" from the connection manager. ` +
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.`);
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.`);
1615

1716
return connectionManager.get(connectionName);
18-
};
19-
20-
if (index !== undefined) {
21-
Container.registerParamHandler({ type: target as Function, index: index, getValue: getValue });
22-
} else {
23-
Container.registerPropertyHandler({ target: target as Function /* todo: looks like typedi wrong type here */, key: propertyName, getValue: getValue });
24-
}
17+
}});
2518
};
2619
}

src/decorators/OrmCustomRepository.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,16 @@ import { Container } from "typedi";
66
* Use it to get the repository class decorated with @EntityRepository decorator.
77
*/
88
export function OrmCustomRepository(cls: Function, connectionName: string = "default"): Function {
9-
return function(target: Object|Function, propertyName: string, index?: number) {
10-
11-
const getValue = () => {
9+
return function(object: Object|Function, propertyName: string, index?: number) {
10+
Container.registerHandler({ object, index, propertyName, value: () => {
1211
const connectionManager = Container.get(ConnectionManager);
1312
if (!connectionManager.has(connectionName))
1413
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.`);
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.`);
1716

1817
const connection = connectionManager.get(connectionName);
1918
return connection.getCustomRepository(cls as any);
20-
};
21-
22-
if (index !== undefined) {
23-
Container.registerParamHandler({ type: target as Function, index: index, getValue: getValue });
24-
} else {
25-
Container.registerPropertyHandler({ target: target as Function /* todo: looks like typedi wrong type here */, key: propertyName, getValue: getValue });
26-
}
19+
}});
2720
};
2821
}

src/decorators/OrmEntityManager.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,21 @@ import {Container} from "typedi";
55
* Allows to inject an EntityManager using typedi's Container.
66
*/
77
export function OrmEntityManager(connectionName: string = "default"): Function {
8-
return function(target: Object|Function, propertyName: string, index?: number) {
9-
10-
const getValue = () => {
8+
return function(object: Object|Function, propertyName: string, index?: number) {
9+
Container.registerHandler({ object, index, propertyName, value: () => {
1110
const connectionManager = Container.get(ConnectionManager);
1211
if (!connectionManager.has(connectionName))
1312
throw new Error(`Cannot get connection "${connectionName}" from the connection manager. ` +
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.`);
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.`);
1615

1716
const connection = connectionManager.get(connectionName);
1817
const entityManager = connection.entityManager;
1918
if (!entityManager)
2019
throw new Error(`Entity manager was not found on "${connectionName}" connection. ` +
21-
`Make sure you correctly setup connection and container usage.`);
20+
`Make sure you correctly setup connection and container usage.`);
2221

2322
return entityManager;
24-
};
25-
26-
if (index !== undefined) {
27-
Container.registerParamHandler({ type: target as Function, index: index, getValue: getValue });
28-
} else {
29-
Container.registerPropertyHandler({ target: target as Function /* todo: looks like typedi wrong type here */, key: propertyName, getValue: getValue });
30-
}
23+
}});
3124
};
3225
}

src/decorators/OrmRepository.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,16 @@ import {Container} from "typedi";
66
* If you want to inject custom Repository class decorated with @EntityRepository decorator, use OrmCustomRepository instead.
77
*/
88
export function OrmRepository(cls: Function, connectionName: string = "default"): Function {
9-
return function(target: Object|Function, propertyName: string, index?: number) {
10-
11-
const getValue = () => {
9+
return function(object: Object|Function, propertyName: string, index?: number) {
10+
Container.registerHandler({ object, index, propertyName, value: () => {
1211
const connectionManager = Container.get(ConnectionManager);
1312
if (!connectionManager.has(connectionName))
1413
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.`);
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.`);
1716

1817
const connection = connectionManager.get(connectionName);
1918
return connection.getRepository(cls as any);
20-
};
21-
22-
if (index !== undefined) {
23-
Container.registerParamHandler({ type: target as Function, index: index, getValue: getValue });
24-
} else {
25-
Container.registerPropertyHandler({ target: target as Function /* todo: looks like typedi wrong type here */, key: propertyName, getValue: getValue });
26-
}
19+
}});
2720
};
2821
}

src/decorators/OrmSpecificRepository.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,16 @@ import {Container} from "typedi";
55
* Allows to inject a SpecificRepository using typedi's Container.
66
*/
77
export function OrmSpecificRepository(cls: Function, connectionName: string = "default"): Function {
8-
return function(target: Object|Function, propertyName: string, index?: number) {
9-
10-
const getValue = () => {
8+
return function(object: Object|Function, propertyName: string, index?: number) {
9+
Container.registerHandler({ object, index, propertyName, value: () => {
1110
const connectionManager = Container.get(ConnectionManager);
1211
if (!connectionManager.has(connectionName))
1312
throw new Error(`Cannot get connection "${connectionName}" from the connection manager. ` +
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.`);
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.`);
1615

1716
const connection = connectionManager.get(connectionName);
1817
return connection.getSpecificRepository(cls as any);
19-
};
20-
21-
if (index !== undefined) {
22-
Container.registerParamHandler({ type: target as Function, index: index, getValue: getValue });
23-
} else {
24-
Container.registerPropertyHandler({ target: target as Function /* todo: looks like typedi wrong type here */, key: propertyName, getValue: getValue });
25-
}
18+
}});
2619
};
27-
}
20+
}

src/decorators/OrmTreeRepository.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,16 @@ import {Container} from "typedi";
55
* Allows to inject a TreeRepository using typedi's Container.
66
*/
77
export function OrmTreeRepository(cls: Function, connectionName: string = "default"): Function {
8-
return function(target: Object|Function, propertyName: string, index?: number) {
9-
10-
const getValue = () => {
8+
return function(object: Object|Function, propertyName: string, index?: number) {
9+
Container.registerHandler({ object, index, propertyName, value: () => {
1110
const connectionManager = Container.get(ConnectionManager);
1211
if (!connectionManager.has(connectionName))
1312
throw new Error(`Cannot get connection "${connectionName}" from the connection manager. ` +
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.`);
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.`);
1615

1716
const connection = connectionManager.get(connectionName);
1817
return connection.getTreeRepository(cls as any);
19-
};
20-
21-
if (index !== undefined) {
22-
Container.registerParamHandler({ type: target as Function, index: index, getValue: getValue });
23-
} else {
24-
Container.registerPropertyHandler({ target: target as Function /* todo: looks like typedi wrong type here */, key: propertyName, getValue: getValue });
25-
}
18+
}});
2619
};
27-
}
20+
}

0 commit comments

Comments
 (0)