diff --git a/content/techniques/sql.md b/content/techniques/sql.md index 80f498dc47..894fb45695 100644 --- a/content/techniques/sql.md +++ b/content/techniques/sql.md @@ -178,6 +178,39 @@ export class PhotoService { > warning **Notice** Don't forget to import the `PhotoModule` into the root `AppModule`. +If you want to use the repository outside of the module which imports `TypeOrmModule.forFeature`, you'll need to re-export the providers generated by it. +You can do this by exporting the whole module, like this: + +```typescript +@@filename(photo.module) +import { Module } from '@nestjs/common'; +import { TypeOrmModule } from '@nestjs/typeorm'; +import { Photo } from './photo.entity'; + +@Module({ + imports: [TypeOrmModule.forFeature([Photo])], + exports: [TypeOrmModule] +}) +export class PhotoModule {} +``` + +Now if we import `PhotoModule` in `PhotoHttpModule`, we can use `@InjectRepository(Photo)` in the providers of the latter module. + +```typescript +@@filename(photo-http.module) +import { Module } from '@nestjs/common'; +import { PhotoModule } from './photo.module'; +import { PhotoService } from './photo.service'; +import { PhotoController } from './photo.controller'; + +@Module({ + imports: [PhotoModule], + providers: [PhotoService], + controllers: [PhotoController] +}) +export class PhotoHttpModule {} +``` + #### Multiple databases Some projects require multiple database connections. This can also be achieved with this module. To work with multiple connections, first create the connections. In this case, connection naming becomes **mandatory**.