Skip to content

Commit 5cfef54

Browse files
author
Umed Khudoiberdiev
committed
added OrmConnection decorator and some docs
1 parent c71334e commit 5cfef54

File tree

4 files changed

+262
-2
lines changed

4 files changed

+262
-2
lines changed

README.md

Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,234 @@
11
# TypeDI Service container integration with TypeORM
2+
3+
This extension for TypeORM provides handy decorators that can be used with [typedi](https://github.com/pleerock/typedi).
4+
5+
## Installation
6+
7+
* Install module:
8+
9+
`npm install typeorm-typedi-extensions --save`
10+
11+
* Install TypeDI:
12+
13+
`npm install typedi --save`
14+
15+
* Configure in your app TypeORM to use TypeDI Container, before you create a connection:
16+
17+
```typescript
18+
import "reflect-metadata";
19+
import {createConnection, useContainer} from "typeorm";
20+
import {Container} from "typedi";
21+
22+
useContainer(Container);
23+
createConnection({
24+
/// ....
25+
});
26+
```
27+
28+
* That's all, start using decorators!
29+
30+
31+
## Usage
32+
33+
All decorators can be used on properties and constructor arguments, e.g. you can use both
34+
property and constructor injection.
35+
36+
### @OrmConnection
37+
38+
Injects `Connection` from where you can access anything in your connection.
39+
40+
Example using property injection:
41+
42+
```typescript
43+
import {Service} from "typedi";
44+
import {Connection} from "typeorm";
45+
import {OrmConnection} from "typeorm-typedi-extensions";
46+
47+
@Service()
48+
export class PostRepository {
49+
50+
@OrmConnection()
51+
private connection: Connection;
52+
53+
}
54+
```
55+
56+
Example using constructor injection:
57+
58+
```typescript
59+
import {Service} from "typedi";
60+
import {Connection} from "typeorm";
61+
import {OrmConnection} from "typeorm-typedi-extensions";
62+
63+
@Service()
64+
export class PostRepository {
65+
66+
constructor(@OrmConnection() private connection: Connection) {
67+
}
68+
69+
}
70+
```
71+
72+
Optionally, you can specify a connection name in the decorator parameters.
73+
74+
### @OrmEntityManager
75+
76+
Injects `EntityManager` from where you can access any entity in your connection.
77+
78+
Example using property injection:
79+
80+
```typescript
81+
import {Service} from "typedi";
82+
import {EntityManager} from "typeorm";
83+
import {OrmEntityManager} from "typeorm-typedi-extensions";
84+
85+
@Service()
86+
export class PostRepository {
87+
88+
@OrmEntityManager()
89+
private entityManager: EntityManager;
90+
91+
}
92+
```
93+
94+
Example using constructor injection:
95+
96+
```typescript
97+
import {Service} from "typedi";
98+
import {EntityManager} from "typeorm";
99+
import {OrmEntityManager} from "typeorm-typedi-extensions";
100+
101+
@Service()
102+
export class PostRepository {
103+
104+
constructor(@OrmEntityManager() private entityManager: EntityManager) {
105+
}
106+
107+
}
108+
```
109+
110+
Optionally, you can specify a connection name in the decorator parameters.
111+
112+
### @OrmRepository
113+
114+
Injects `Repository` of some Entity.
115+
116+
Example using property injection:
117+
118+
```typescript
119+
import {Service} from "typedi";
120+
import {Repository} from "typeorm";
121+
import {OrmRepository} from "typeorm-typedi-extensions";
122+
import "../entity/Post";
123+
124+
@Service()
125+
export class PostRepository {
126+
127+
@OrmRepository(Post)
128+
private repository: Repository<Post>;
129+
130+
}
131+
```
132+
133+
Example using constructor injection:
134+
135+
```typescript
136+
import {Service} from "typedi";
137+
import {Repository} from "typeorm";
138+
import {OrmRepository} from "typeorm-typedi-extensions";
139+
import "../entity/Post";
140+
141+
@Service()
142+
export class PostRepository {
143+
144+
constructor(@OrmRepository(Post) private repository: Repository<Post>) {
145+
}
146+
147+
}
148+
```
149+
150+
Optionally, you can specify a connection name in the decorator parameters.
151+
152+
### @OrmTreeRepository
153+
154+
Injects `TreeRepository` of some Entity.
155+
156+
Example using property injection:
157+
158+
```typescript
159+
import {Service} from "typedi";
160+
import {TreeRepository} from "typeorm";
161+
import {OrmTreeRepository} from "typeorm-typedi-extensions";
162+
import "../entity/Post";
163+
164+
@Service()
165+
export class PostRepository {
166+
167+
@OrmTreeRepository(Post)
168+
private repository: TreeRepository<Post>;
169+
170+
}
171+
```
172+
173+
Example using constructor injection:
174+
175+
```typescript
176+
import {Service} from "typedi";
177+
import {TreeRepository} from "typeorm";
178+
import {OrmTreeRepository} from "typeorm-typedi-extensions";
179+
import "../entity/Post";
180+
181+
@Service()
182+
export class PostRepository {
183+
184+
constructor(@OrmTreeRepository(Post) private repository: TreeRepository<Post>) {
185+
}
186+
187+
}
188+
```
189+
190+
Optionally, you can specify a connection name in the decorator parameters.
191+
192+
### @OrmSpecificRepository
193+
194+
Injects `SpecificRepository` of some Entity.
195+
196+
Example using property injection:
197+
198+
```typescript
199+
import {Service} from "typedi";
200+
import {SpecificRepository} from "typeorm";
201+
import {OrmSpecificRepository} from "typeorm-typedi-extensions";
202+
import "../entity/Post";
203+
204+
@Service()
205+
export class PostRepository {
206+
207+
@OrmSpecificRepository(Post)
208+
private repository: SpecificRepository<Post>;
209+
210+
}
211+
```
212+
213+
Example using constructor injection:
214+
215+
```typescript
216+
import {Service} from "typedi";
217+
import {SpecificRepository} from "typeorm";
218+
import {OrmSpecificRepository} from "typeorm-typedi-extensions";
219+
import "../entity/Post";
220+
221+
@Service()
222+
export class PostRepository {
223+
224+
constructor(@OrmSpecificRepository(Post) private repository: SpecificRepository<Post>) {
225+
}
226+
227+
}
228+
```
229+
230+
Optionally, you can specify a connection name in the decorator parameters.
231+
232+
## Samples
233+
234+
Take a look on samples in [./sample](sample) for examples of usages.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@
2626
"pg": "^6.1.0",
2727
"reflect-metadata": "^0.1.8",
2828
"typedi": "^0.4.2",
29-
"typeorm": "^0.0.2-alpha.62",
29+
"typeorm": "^0.0.2-alpha.63",
3030
"typescript": "^2.0.2"
3131
},
3232
"peerDependencies": {
33-
"typeorm": "^0.0.2-alpha.62",
33+
"typeorm": "^0.0.2-alpha.63",
3434
"typedi": "^0.4.2"
3535
}
3636
}

src/decorators/OrmConnection.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import {ConnectionManager} from "typeorm";
2+
import {Container} from "typedi";
3+
4+
/**
5+
* Allows to inject an Connection using typedi's Container.
6+
*/
7+
export function OrmConnection(connectionName: string = "default"): Function {
8+
return function(target: Object|Function, propertyName: string, index?: number) {
9+
10+
const getValue = () => {
11+
const connectionManager = Container.get(ConnectionManager);
12+
if (!connectionManager.has(connectionName))
13+
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.`);
16+
17+
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+
}
25+
};
26+
}

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export * from "./decorators/OrmConnection";
12
export * from "./decorators/OrmEntityManager";
23
export * from "./decorators/OrmRepository";
34
export * from "./decorators/OrmSpecificRepository";

0 commit comments

Comments
 (0)