Skip to content

Commit 11d41c8

Browse files
authored
Merge pull request #18 from 19majkel94/feature/generic-repository-decorator
Generic repository decorator
2 parents 2d03516 + 73999c9 commit 11d41c8

File tree

13 files changed

+248
-186
lines changed

13 files changed

+248
-186
lines changed

README.md

Lines changed: 25 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,8 @@ Optionally, you can specify a connection name in the decorator parameters.
111111

112112
### @OrmRepository
113113

114-
Injects `Repository` of some Entity.
115-
116-
If you want to inject custom Repository (class decorated with `@EntityRepository` decorator), use `OrmCustomRepository` instead.
114+
Injects `Repository`, `MongoRepository`, `TreeRepository` or custom repository of some Entity.
115+
Be aware that the property or param decorated with `@OrmRepository` has to be annotated with repository type!
117116

118117
Example using property injection:
119118

@@ -143,16 +142,27 @@ import "../entity/Post";
143142
@Service()
144143
export class PostRepository {
145144

146-
constructor(@OrmRepository(Post) private repository: Repository<Post>) {
147-
}
145+
constructor(
146+
@OrmRepository(Post)
147+
private repository: Repository<Post>
148+
) {}
148149

149150
}
150151
```
151-
Optionally, you can specify a connection name in the decorator parameters.
152+
Optionally, you can specify a connection name in the decorator parameters:
152153

153-
### @OrmCustomRepository
154+
```ts
155+
@Service()
156+
export class PostRepository {
157+
158+
@OrmRepository(Post, "custom-con-name")
159+
private repository: Repository<Post>;
160+
161+
}
162+
```
154163

155-
Injects custom `Repository` of some Entity. Be aware that you have to create the class which extends the generic `Repository<T>` and decorate it with `EntityRepository<T>` decorator.
164+
You can also inject custom `Repository` of some Entity.
165+
Be aware that you have to create the class which extends the generic `Repository<T>` and decorate it with `EntityRepository<T>` decorator.
156166

157167
Example using constructor injection:
158168

@@ -178,9 +188,9 @@ export class PostService {
178188

179189
// using constructor injection
180190
constructor(
181-
@OrmCustomRepository(UserRepository)
182-
private readonly userRepository: UserRepository
183-
)
191+
@OrmRepository()
192+
private readonly userRepository: UserRepository,
193+
) {}
184194

185195
public userExist(user: User): boolean {
186196
return this.userRepository.findByEmail(user.email) ? true : false;
@@ -191,86 +201,16 @@ export class PostService {
191201

192202
Optionally, you can specify a connection name in the decorator parameters.
193203

194-
### @OrmTreeRepository
195-
196-
Injects `TreeRepository` of some Entity.
197-
198-
Example using property injection:
199-
200-
```typescript
201-
import {Service} from "typedi";
202-
import {TreeRepository} from "typeorm";
203-
import {OrmTreeRepository} from "typeorm-typedi-extensions";
204-
import "../entity/Post";
205-
206-
@Service()
207-
export class PostRepository {
208-
209-
@OrmTreeRepository(Post)
210-
private repository: TreeRepository<Post>;
211-
212-
}
213-
```
214-
215-
Example using constructor injection:
216-
217-
```typescript
218-
import {Service} from "typedi";
219-
import {TreeRepository} from "typeorm";
220-
import {OrmTreeRepository} from "typeorm-typedi-extensions";
221-
import "../entity/Post";
222-
223-
@Service()
224-
export class PostRepository {
225-
226-
constructor(@OrmTreeRepository(Post) private repository: TreeRepository<Post>) {
227-
}
228-
229-
}
230-
```
231-
232-
Optionally, you can specify a connection name in the decorator parameters.
233-
234-
### @OrmSpecificRepository
235-
236-
Injects `SpecificRepository` of some Entity.
237-
238-
Example using property injection:
239-
240-
```typescript
241-
import {Service} from "typedi";
242-
import {SpecificRepository} from "typeorm";
243-
import {OrmSpecificRepository} from "typeorm-typedi-extensions";
244-
import "../entity/Post";
245-
246-
@Service()
247-
export class PostRepository {
248-
249-
@OrmSpecificRepository(Post)
250-
private repository: SpecificRepository<Post>;
251-
252-
}
253-
```
254-
255-
Example using constructor injection:
256-
257-
```typescript
258-
import {Service} from "typedi";
259-
import {SpecificRepository} from "typeorm";
260-
import {OrmSpecificRepository} from "typeorm-typedi-extensions";
261-
import "../entity/Post";
262-
204+
```ts
263205
@Service()
264-
export class PostRepository {
206+
export class PostService {
265207

266-
constructor(@OrmSpecificRepository(Post) private repository: SpecificRepository<Post>) {
267-
}
208+
@OrmRepository("custom-con-name")
209+
private userRepository: UserRepository;
268210

269211
}
270212
```
271213

272-
Optionally, you can specify a connection name in the decorator parameters.
273-
274214
## Samples
275215

276216
Take a look on samples in [./sample](sample) for examples of usages.

package.json

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
{
22
"name": "typeorm-typedi-extensions",
3-
"version": "0.0.12",
3+
"version": "0.1.0",
44
"description": "Dependancy injection and service container integration with TypeORM using typedi library.",
55
"license": "MIT",
66
"readmeFilename": "README.md",
77
"author": {
88
"name": "Umed Khudoiberdiev",
99
"email": "[email protected]"
1010
},
11+
"contributors": [
12+
{
13+
"name": "Michał Lytek",
14+
"url": "https://github.com/19majkel94"
15+
}
16+
],
1117
"repository": {
1218
"type": "git",
1319
"url": "https://github.com/typeorm/typeorm-typedi-extensions.git"
@@ -38,12 +44,12 @@
3844
"ts-node": "^1.3.0",
3945
"tslint": "^3.15.1",
4046
"tslint-stylish": "^2.1.0-beta",
41-
"typedi": "^0.5.0",
42-
"typeorm": "^0.0.11",
43-
"typescript": "^2.1.5"
47+
"typedi": "^0.5.2",
48+
"typeorm": "^0.1.0-alpha.47",
49+
"typescript": "^2.5.2"
4450
},
4551
"peerDependencies": {
46-
"typeorm": "^0.0.11",
47-
"typedi": "^0.5.0"
52+
"typeorm": "^0.1.0-alpha.47",
53+
"typedi": "^0.5.2"
4854
}
4955
}

sample/sample1-simple-usage/app.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,12 @@ import {Post} from "./entity/Post";
66

77
useContainer(Container);
88
createConnection({
9-
driver: {
10-
type: "postgres",
11-
host: "localhost",
12-
port: 5432,
13-
username: "test",
14-
password: "admin",
15-
database: "test"
16-
},
9+
type: "postgres",
10+
host: "localhost",
11+
port: 5432,
12+
username: "test",
13+
password: "admin",
14+
database: "test",
1715
entities: [
1816
__dirname + "/entity/*.js"
1917
],

sample/sample1-simple-usage/entity/Post.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import {Table, PrimaryColumn, Column} from "typeorm";
1+
import {Entity, PrimaryGeneratedColumn, Column} from "typeorm";
22

3-
@Table()
3+
@Entity()
44
export class Post {
55

6-
@PrimaryColumn("int", { generated: true })
6+
@PrimaryGeneratedColumn()
77
id: number;
88

99
@Column()

src/decorators/OrmCustomRepository.ts

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)