|
1 | 1 | # 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. |
0 commit comments