From 1560196ac4da4d6f8ba3b7083f74bc49c57f4c00 Mon Sep 17 00:00:00 2001 From: Andrew Starostin Date: Mon, 26 Aug 2019 18:32:38 +0300 Subject: [PATCH] docs(sql) Mention exporting repository providers --- content/techniques/sql.md | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/content/techniques/sql.md b/content/techniques/sql.md index cbf4a44ef3..3dc7144ea2 100644 --- a/content/techniques/sql.md +++ b/content/techniques/sql.md @@ -180,6 +180,39 @@ export class PhotoService { > warning **Notice** Do not forget to import the `PhotoModule` into the root `ApplicationModule`. +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 of your projects may require multiple database connections. Fortunately, this can also be achieved with this module. To work with multiple connections, the first thing to do is to create those connections. In this case, the connection naming becomes **mandatory**.