title: Nest JS Circular Dependency Issue - Mongoose Module
body: <p>I am trying to inject dependencies in mongoose module for root async. I want to kind of simulate a cascade delete using mongoose hooks.</p>
  <p>I have this module, which imports mongoose module, and it imports other modules.</p>
  <pre><code>@Module({
    imports: [
      MongooseModule.forFeatureAsync([
        {
          imports: [NotificationModule, UserModule, MuseumModule, ImageModule, CommentModule],
          name: Shirt.name,
          useFactory: (
            NotificationService: NotificationService,
            UserService: UserService,
            MuseumService: MuseumService,
            ImageService: ImageService,
            ConfigService: ConfigService,
            CommentService: CommentService,
          ) =&gt; {
            const schema = ShirtSchema;
            schema.post('findOneAndDelete', async function (document: ShirtDocument) {
              /* Delete notifications */
              if (document) {
                await NotificationService.deleteManyFromArray(document._id);
  
                /* Remove shirt from museum */
                const shirtUser = await UserService.getById(document.shirtUser.userId);
                await MuseumService.removeShirtByMuseumId(shirtUser.museums[0], document._id);
  
                /* Delete images from bucket */
                if (document.images &amp;&amp; document.images.length &gt; 0) {
                  document.images.forEach(async (image) =&gt; {
                    if (image.thumbnail) {
                      await ImageService.deleteImageFromBucketS3({
                        bucket: ConfigService.get('AWS_THUMBNAIL_BUCKET'),
                        key: getImageUUID(image.thumbnail),
                      });
                    }
                    await ImageService.deleteImageFromBucketS3({
                      bucket: ConfigService.get('AWS_BUCKET'),
                      key: getImageUUID(image.cloudImage),
                    });
                  });
                }
  
                /* Remove shirt comments */
                if (document.comments &amp;&amp; document.comments.length &gt; 0) {
                  await CommentService.deleteManyComments(document.comments.map((c) =&gt; c._id));
                }
              }
            });
            return schema;
          },
          inject: [NotificationService, UserService, MuseumService, ImageService, ConfigService, CommentService],
        },
      ]),
      UserModule,
      TeamModule,
      BrandModule,
      CountryModule,
      MuseumModule,
      ImageModule,
    ],
    controllers: [ShirtController],
    providers: [ShirtService, ShirtRepository],
    exports: [ShirtService],
  })
  export class ShirtModule {}
  </code></pre>
  <p>I also need to do the same in another module, but when I import the</p>
  <blockquote>
  <p>ShirtModule</p>
  </blockquote>
  <p>the compilation fails with the following error:</p>
  <blockquote>
  <p>Error: Nest cannot create the module instance. Often, this is because
  of a circular dependency between modules. Use forwardRef() to avoid
  it. Scope [AppModule -&gt; UserModule -&gt; MongooseModule -&gt; MuseumModule
  -&gt; MongooseModule -&gt; ShirtModule -&gt; MongooseModule]</p>
  </blockquote>
  <pre><code>@Module({
    imports: [
      MongooseModule.forFeatureAsync([
        {
          name: 'Museum',
          imports: [ShirtModule],
          useFactory: () =&gt; {
            const schema = MuseumSchema;
            schema.post('findOneAndDelete', async function (document: MuseumDocument) {});
            return schema;
          },
        },
      ]),
    ],
    controllers: [MuseumController],
    providers: [MuseumService, MuseumRepository],
    exports: [MuseumService],
  })
  export class MuseumModule {}
  </code></pre>
  <p>I tried using</p>
  <blockquote>
  <p>forwardRef(() =&gt; )</p>
  </blockquote>
  <p>In both modules, but still the same. I can not understand where is the circular dependency and how to solve it.
  Please could you help me?. Also, is this is a good approach to use mongoose hooks using nest?
  Thanks</p>