|
1 | 1 | # TypeDI Service container integration with TypeORM
|
2 | 2 |
|
3 |
| -This extension for TypeORM provides handy decorators that can be used with [typedi](https://github.com/pleerock/typedi). |
| 3 | + |
| 4 | +[](https://badge.fury.io/js/typeorm-typedi-extensions) |
| 5 | +[](https://david-dm.org/typeorm/typeorm-typedi-extensions) |
| 6 | + |
| 7 | +This package provides decorators for TypeORM that can be used with [TypeDI](https://github.com/pleerock/typedi). |
4 | 8 |
|
5 | 9 | ## Installation
|
6 | 10 |
|
7 |
| -* Install module: |
| 11 | +To start using TypeDI install the required packages via NPM: |
8 | 12 |
|
9 |
| -`npm install typeorm-typedi-extensions --save` |
| 13 | +```bash |
| 14 | +npm install typeorm-typedi-extensions typedi reflect-metadata |
| 15 | +``` |
10 | 16 |
|
11 |
| -* Install TypeDI: |
| 17 | +Import the `reflect-metadata` package at the **first line** of your application: |
12 | 18 |
|
13 |
| -`npm install typedi --save` |
| 19 | +```ts |
| 20 | +import 'reflect-metadata'; |
14 | 21 |
|
15 |
| -* Configure in your app TypeORM to use TypeDI Container, before you create a connection: |
| 22 | +// Your other imports and initialization code |
| 23 | +// comes here after you imported the reflect-metadata package! |
| 24 | +``` |
16 | 25 |
|
17 |
| -```typescript |
18 |
| -import "reflect-metadata"; |
19 |
| -import {createConnection, useContainer} from "typeorm"; |
20 |
| -import {Container} from "typedi"; |
| 26 | +You need to enable emitting decorator metadata in your Typescript config. Add these two lines to your `tsconfig.json` file under the `compilerOptions` key: |
21 | 27 |
|
22 |
| -useContainer(Container); |
23 |
| -createConnection({ |
24 |
| - /// .... |
25 |
| -}); |
| 28 | +```json |
| 29 | +"emitDecoratorMetadata": true, |
| 30 | +"experimentalDecorators": true, |
26 | 31 | ```
|
27 | 32 |
|
28 |
| -* That's all, start using decorators! |
| 33 | +Configure TypeORM in your app to use the TypeDI container before you create a connection: |
| 34 | + |
| 35 | +```ts |
| 36 | +import { createConnection, useContainer } from 'typeorm'; |
| 37 | +import { Container } from 'typedi'; |
| 38 | + |
| 39 | +/** Tell TypeORM to use the TypeDI container to resolve it's dependencies. */ |
| 40 | +useContainer(Container); |
29 | 41 |
|
| 42 | +/** Create a connection and start using TypeORM. */ |
| 43 | +createConnection({ /* <connection options> */}) |
| 44 | + .catch(error => { |
| 45 | + console.error(`Couldn't connect to the database!`); |
| 46 | + console.error(error); |
| 47 | + }); |
| 48 | +``` |
30 | 49 |
|
31 | 50 | ## Usage
|
32 | 51 |
|
33 |
| -All decorators can be used on properties and constructor arguments, e.g. you can use both |
34 |
| -property and constructor injection. |
| 52 | +This package exposes three decorators all three decorators can be used on properties or on constructor parameters. |
35 | 53 |
|
36 |
| -### @InjectConnection |
| 54 | +> **IMPORTANT:** |
| 55 | +> To allow TypeDI to resolve the dependencies on your classes you must mark them with `@Service` decorator from the TypeDI package. |
37 | 56 |
|
38 |
| -Injects `Connection` from where you can access anything in your connection. |
| 57 | +### `@InjectConnection` decorator |
39 | 58 |
|
40 |
| -Example using property injection: |
| 59 | +Injects `Connection` from where you can access anything in your connection. |
| 60 | +Optionally, you can specify a connection to inject by name in the decorator parameter. |
41 | 61 |
|
42 | 62 | ```typescript
|
43 |
| -import {Service} from "typedi"; |
44 |
| -import {Connection} from "typeorm"; |
45 |
| -import {InjectConnection} from "typeorm-typedi-extensions"; |
| 63 | +import { Service } from 'typedi'; |
| 64 | +import { Connection } from 'typeorm'; |
| 65 | +import { InjectConnection } from 'typeorm-typedi-extensions'; |
46 | 66 |
|
47 | 67 | @Service()
|
48 |
| -export class PostRepository { |
49 |
| - |
50 |
| - @InjectConnection() |
51 |
| - private connection: Connection; |
52 |
| - |
53 |
| -} |
54 |
| -``` |
| 68 | +export class MyCustomClass { |
55 | 69 |
|
56 |
| -Example using constructor injection: |
| 70 | + @InjectConnection() |
| 71 | + private propertyInjectedConnection: Connection; |
57 | 72 |
|
58 |
| -```typescript |
59 |
| -import {Service} from "typedi"; |
60 |
| -import {Connection} from "typeorm"; |
61 |
| -import {InjectConnection} from "typeorm-typedi-extensions"; |
62 |
| - |
63 |
| -@Service() |
64 |
| -export class PostRepository { |
65 |
| - |
66 |
| - constructor(@InjectConnection() private connection: Connection) { |
67 |
| - } |
68 |
| - |
| 73 | + constructor(@InjectConnection() private constructorInjectedConnection: Connection) {} |
69 | 74 | }
|
70 | 75 | ```
|
71 | 76 |
|
72 |
| -Optionally, you can specify a connection name in the decorator parameters. |
| 77 | +### `@InjectManager` decorator |
73 | 78 |
|
74 |
| -### @InjectManager |
| 79 | +Injects `EntityManager` from where you can access any entity in your connection. |
| 80 | +Optionally, you can specify a connection to inject by name in the decorator parameter. |
75 | 81 |
|
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 {InjectManager} from "typeorm-typedi-extensions"; |
| 82 | +```ts |
| 83 | +import { Service } from 'typedi'; |
| 84 | +import { EntityManager } from 'typeorm'; |
| 85 | +import { InjectManager } from 'typeorm-typedi-extensions'; |
84 | 86 |
|
85 | 87 | @Service()
|
86 |
| -export class PostRepository { |
87 |
| - |
88 |
| - @InjectManager() |
89 |
| - private entityManager: EntityManager; |
90 |
| - |
91 |
| -} |
92 |
| -``` |
| 88 | +export class MyCustomClass { |
93 | 89 |
|
94 |
| -Example using constructor injection: |
95 |
| - |
96 |
| -```typescript |
97 |
| -import {Service} from "typedi"; |
98 |
| -import {EntityManager} from "typeorm"; |
99 |
| -import {InjectManager} from "typeorm-typedi-extensions"; |
| 90 | + @InjectManager() |
| 91 | + private propertyInjectedEntityManager: EntityManager; |
100 | 92 |
|
101 |
| -@Service() |
102 |
| -export class PostRepository { |
103 |
| - |
104 |
| - constructor(@InjectManager() private entityManager: EntityManager) { |
105 |
| - } |
106 |
| - |
| 93 | + constructor(@InjectManager() private constructorInjectedEntityManager: EntityManager) {} |
107 | 94 | }
|
108 | 95 | ```
|
109 | 96 |
|
110 |
| -Optionally, you can specify a connection name in the decorator parameters. |
111 |
| - |
112 |
| -### @InjectRepository |
| 97 | +### `@InjectRepository` decorator |
113 | 98 |
|
114 | 99 | Injects `Repository`, `MongoRepository`, `TreeRepository` or custom repository of some Entity.
|
115 |
| -Be aware that the property or param decorated with `@InjectRepository` has to be annotated with repository type! |
116 |
| - |
117 |
| -Example using property injection: |
| 100 | +Optionally, you can specify a connection to inject by name in the decorator parameter. |
118 | 101 |
|
119 | 102 | ```typescript
|
120 |
| -import {Service} from "typedi"; |
121 |
| -import {Repository} from "typeorm"; |
122 |
| -import {InjectRepository} from "typeorm-typedi-extensions"; |
123 |
| -import "../entity/Post"; |
| 103 | +import { Service } from 'typedi'; |
| 104 | +import { Repository } from 'typeorm'; |
| 105 | +import { InjectRepository } from 'typeorm-typedi-extensions'; |
| 106 | +// MyDatabaseModel is a TypeORM entity (class marked with `@Entity()` decorator) |
| 107 | +import { MyDatabaseModel } from './entities/post.entity.ts'; |
124 | 108 |
|
125 | 109 | @Service()
|
126 |
| -export class PostRepository { |
127 |
| - |
128 |
| - @InjectRepository(Post) |
129 |
| - private repository: Repository<Post>; |
130 |
| - |
131 |
| -} |
132 |
| -``` |
| 110 | +export class MyCustomClass { |
133 | 111 |
|
134 |
| -Example using constructor injection: |
| 112 | + @InjectRepository(MyDatabaseModel) |
| 113 | + private propertyInjectedRepository: Repository<MyDatabaseModel>; |
135 | 114 |
|
136 |
| -```typescript |
137 |
| -import {Service} from "typedi"; |
138 |
| -import {Repository} from "typeorm"; |
139 |
| -import {InjectRepository} from "typeorm-typedi-extensions"; |
140 |
| -import "../entity/Post"; |
141 |
| - |
142 |
| -@Service() |
143 |
| -export class PostRepository { |
144 |
| - |
145 |
| - constructor( |
146 |
| - @InjectRepository(Post) |
147 |
| - private repository: Repository<Post> |
148 |
| - ) {} |
149 |
| - |
| 115 | + constructor(@InjectRepository(MyDatabaseModel) private constructorInjectedRepository: Repository<MyDatabaseModel>) {} |
150 | 116 | }
|
151 | 117 | ```
|
152 |
| -Optionally, you can specify a connection name in the decorator parameters: |
| 118 | + |
| 119 | +Example with custom connection name: |
153 | 120 |
|
154 | 121 | ```ts
|
155 | 122 | @Service()
|
156 | 123 | export class PostRepository {
|
157 |
| - |
158 |
| - @InjectRepository(Post, "custom-con-name") |
159 |
| - private repository: Repository<Post>; |
160 |
| - |
| 124 | + @InjectRepository(Post, 'custom-con-name') |
| 125 | + private repository: Repository<Post>; |
161 | 126 | }
|
162 | 127 | ```
|
163 | 128 |
|
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. |
| 129 | +You can also inject custom `Repository` of some Entity. To make this work have to create the class which extends the |
| 130 | +generic `Repository<T>` class and decorate it with `EntityRepository<T>` decorator. |
166 | 131 |
|
167 | 132 | Example using constructor injection:
|
168 | 133 |
|
169 | 134 | ```typescript
|
170 |
| -import { Service } from "typedi"; |
171 |
| -import { Repository, EntityRepository } from "typeorm"; |
172 |
| -import { InjectRepository } from "typeorm-typedi-extensions"; |
173 |
| -import "../entity/user"; |
| 135 | +import { Service } from 'typedi'; |
| 136 | +import { Repository, EntityRepository } from 'typeorm'; |
| 137 | +import { InjectRepository } from 'typeorm-typedi-extensions'; |
| 138 | +// UserModel is a TypeORM entity (class marked with `@Entity()` decorator) |
| 139 | +import { UserModel } from './entities/user.entity.ts'; |
174 | 140 |
|
175 |
| -// create custom Repository class |
176 | 141 | @Service()
|
177 |
| -@EntityRepository(User) |
178 |
| -export class UserRepository extends Repository<User> { |
179 |
| - |
180 |
| - public findByEmail(email: string) { |
181 |
| - return this.findOne({ email }); |
182 |
| - } |
183 |
| - |
| 142 | +@EntityRepository(UserModel) |
| 143 | +export class UserRepository extends Repository<UserModel> { |
| 144 | + public findByEmail(email: string) { |
| 145 | + return this.findOne({ email }); |
| 146 | + } |
184 | 147 | }
|
185 | 148 |
|
186 | 149 | @Service()
|
187 | 150 | export class PostService {
|
188 |
| - |
189 |
| - // using constructor injection |
190 |
| - constructor( |
191 |
| - @InjectRepository() |
192 |
| - private readonly userRepository: UserRepository, |
193 |
| - ) {} |
194 |
| - |
195 |
| - public async userExist(user: User): boolean { |
196 |
| - return await this.userRepository.findByEmail(user.email) ? true : false; |
197 |
| - } |
198 |
| - |
| 151 | + constructor( |
| 152 | + @InjectRepository() |
| 153 | + private readonly userRepository: UserRepository |
| 154 | + ) {} |
| 155 | + |
| 156 | + public async userExist(user: User): boolean { |
| 157 | + return (await this.userRepository.findByEmail(user.email)) ? true : false; |
| 158 | + } |
199 | 159 | }
|
200 | 160 | ```
|
201 | 161 |
|
202 |
| -Optionally, you can specify a connection name in the decorator parameters. |
203 |
| - |
204 |
| -```ts |
205 |
| -@Service() |
206 |
| -export class PostService { |
207 |
| - |
208 |
| - @InjectRepository("custom-con-name") |
209 |
| - private userRepository: UserRepository; |
210 |
| - |
211 |
| -} |
212 |
| -``` |
213 |
| - |
214 |
| -## Samples |
215 |
| - |
216 |
| -Take a look on samples in [./sample](sample) for examples of usages. |
| 162 | +[typedi]: https://github.com/typestack/typedi |
0 commit comments