Skip to content

Commit 80997e6

Browse files
author
Umed Khudoiberdiev
authored
Merge pull request #5 from 19majkel94/patch-2
Updated readme
2 parents a307392 + 5fc5426 commit 80997e6

File tree

1 file changed

+43
-1
lines changed

1 file changed

+43
-1
lines changed

README.md

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ Optionally, you can specify a connection name in the decorator parameters.
113113

114114
Injects `Repository` of some Entity.
115115

116+
If you want to inject custom Repository (class decorated with `@EntityRepository` decorator), use `OrmCustomRepository` instead.
117+
116118
Example using property injection:
117119

118120
```typescript
@@ -144,6 +146,46 @@ export class PostRepository {
144146
constructor(@OrmRepository(Post) private repository: Repository<Post>) {
145147
}
146148

149+
}
150+
```
151+
Optionally, you can specify a connection name in the decorator parameters.
152+
153+
### @OrmCustomRepository
154+
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.
156+
157+
Example using constructor injection:
158+
159+
```typescript
160+
import { Service } from "typedi";
161+
import { Repository, EntityRepository } from "typeorm";
162+
import { OrmCustomRepository } from "typeorm-typedi-extensions";
163+
import "../entity/user";
164+
165+
// create custom Repository class
166+
@Service()
167+
@EntityRepository(User)
168+
export class UserRepository extends Repository<User> {
169+
170+
public findByEmail(email: string) {
171+
return this.findOne({ email });
172+
}
173+
174+
}
175+
176+
@Service()
177+
export class PostService {
178+
179+
// using constructor injection
180+
constructor(
181+
@OrmCustomRepository(UserRepository)
182+
private readonly userRepository: UserRepository
183+
)
184+
185+
public userExist(user: User): boolean {
186+
return this.userRepository.findByEmail(user.email) ? true : false;
187+
}
188+
147189
}
148190
```
149191

@@ -231,4 +273,4 @@ Optionally, you can specify a connection name in the decorator parameters.
231273

232274
## Samples
233275

234-
Take a look on samples in [./sample](sample) for examples of usages.
276+
Take a look on samples in [./sample](sample) for examples of usages.

0 commit comments

Comments
 (0)