Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documentation for implementing forRootAsync (#530) #1415

Closed
iangregsondev opened this issue Jan 1, 2019 · 3 comments
Closed

Documentation for implementing forRootAsync (#530) #1415

iangregsondev opened this issue Jan 1, 2019 · 3 comments

Comments

@iangregsondev
Copy link

iangregsondev commented Jan 1, 2019

Please search GitHub for a similar issue or PR before submitting.
Check one of the following options with "x" -->


[ ] Regression 
[x] Bug report
[ ] Feature request
[x] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

Hi,

There have been some implementation details done for providing our own forRootAsync on our modules. Its mentioned here #530

I attached to implement it but I can't get it to work, it would be really nice for a small sample or some documentation.

What I have tried - and I am sure I am doing it wrong..

I have my asyncOptions

import { ModuleMetadata } from "@nestjs/common/interfaces"
import { ILoggerModuleOptions } from "./logger-module-options.interface"

export interface ILoggerModuleAsyncOptions extends Pick<ModuleMetadata, "imports"> {
  useFactory?: (...args: any[]) => Promise<ILoggerModuleOptions> | ILoggerModuleOptions
  inject?: any[]
}

It references a simple interface

import { Configuration } from "log4js"

export interface ILoggerModuleOptions {
  configuration?: string | Configuration
}

In my main app module I do the following, The configService seems to be injected BUT all the values are undefined when I call them. In fact I have a static getter on the configService to return a string and it is also undefined.

    forwardRef(() =>
      LoggerModule.forRootAsync({
        inject: [ConfigService],
        useFactory: async (configService: ConfigService) => {
          return {
            configuration: configService.connectionString
          }
        }
      })
    )

and then my logger module. I am placing the Async and non async versions, the non async version works because I am only passing a string.

export class LoggerModule {
  static forRoot(options: ILoggerModuleOptions): DynamicModule {
    return {
      exports: [SystemLoggerService],
      module: LoggerModule,
      providers: [
        SystemLoggerService
      ]
    }
  }

  static forRootAsync(options: ILoggerModuleAsyncOptions): DynamicModule {
    if (options.useFactory && options.inject) {
      const result = this.callTheFunction(options)
      console.log("result " + result)
    }

    return {
      exports: [SystemLoggerService],
      module: LoggerModule,
      providers: [SystemLoggerService]
    }
  }

  private static async callTheFunction(options: any) {
    const result = await options.useFactory(options.inject[0])
    return result
  }

I am a little lost why its not working :-)

Does anyone have a sample or has anybody actually have it working ?

I looked into the code from the typeOrm which uses this method but without documentation its a little difficult to get it working.

Thanks

@kamilmysliwiec
Copy link
Member

I have created a corresponding issue :) Let's track this here: nestjs/docs.nestjs.com#223

@iangregsondev
Copy link
Author

I have created a corresponding issue :) Let's track this here: nestjs/docs.nestjs.com#223

Great! Thanks, I will have a look over there and keep my eye on it.

It was one of the missing pieces that I couldn't figure out on my own :-)

@lock
Copy link

lock bot commented Sep 24, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Sep 24, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants