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

TypeOrmModule.forRoot - dynamic configuration #29

Closed
mgamperl opened this issue May 24, 2018 · 4 comments
Closed

TypeOrmModule.forRoot - dynamic configuration #29

mgamperl opened this issue May 24, 2018 · 4 comments

Comments

@mgamperl
Copy link

Hi,

I really like the simplicity of this module but there is one thing I can not understand:
How do you dynamically configure the database connection when using TypeOrmModule.forRoot

@Module({
  imports: [
    TypeOrmModule.forRoot({
      type: 'mysql',
      host: 'localhost',
      port: 3306,
      ...      
    }),
  ],
})

I have a similar ConfigService like the one in https://docs.nestjs.com/techniques/configuration in my application and want to use that for the configuration of the database connection but its embarrassing, I can't find a solution on how to use this configuration in combination with TypeOrmModule.

I know I could do the creation of the connection in a custom provider following https://docs.nestjs.com/recipes/sql-typeorm but isn't there a simpler way which is as convenient as this module?

Thanks for help and thank you for the hard work you all are putting into nestjs I really love it!

@BrunnerLivio
Copy link
Member

BrunnerLivio commented May 24, 2018

I used another approach in my project. I basically share the options through the AppModule:

main.ts

const settings = dotenv.parse(fs.readFileSync(filePath));
const app = await NestFactory.create(AppModule.forRoot(settings));
...

app.module.ts

export class AppModule {
    static forRoot(settings: APISettings): DynamicModule {
        return {
            module: AppModule,
            imports: [
                // This module has a ConfigurationService, which
                // does the same as described in the docs
                // but it does not read the env file.
                AppSettingsModule.forRoot(settings),
                DatabaseModule.forRoot(settings),
                ...
            ]
        };
    }
}

database.module.ts

export function getOrmConfig(settings: IDatabaseSettings) {
     return {
            type: 'postgres',
            host: settings.host,
            port: settings.port,
            username: settings.username,
            password: settings.password,
            database: settings.database,
            entities,
            synchronize: true,
            logging: true,
        };
}

export class DatabaseModule {
  public static forRoot(settings: IDatabaseSettings): DynamicModule {
    const ormConfig = getOrmConfig(settings);
    return {
      module: DatabaseModule,
      imports: [
        TypeOrmModule.forRoot(ormConfig)
      ],
      components: [
        DatabaseService,
      ],
      exports: [
        DatabaseService
      ]
    };
  }
}

I think it is a bad idea to read the configuration inside the service as suggested in the docs. For example; I have the use case, that my application should be useable and configureable by npm package (npm i -s my-api). Lets assume my API is called MyAPI. Then the following code would not be feasable, if the configuration is read by a nestjs service.

import { MyAPI } from 'my-api';

// Bootstraps the NestJS configuration and calls await NestFactory.create(AppModule.forRoot(settings));
new MyAPI({ databaseHost: 'postgres', ... }).run();

Thats why I prefer my solution; its more dynamic and modular.

I hope you get what I mean :)

@mgamperl
Copy link
Author

This is a really nice approach, I think I will try this for my use case.
Thank you very much!

I'm closing this issue..

@Alexandredc
Copy link

I'm facing the same problem.

@BrunnerLivio your approach is ok but I still prefer handle settings with a service.

For now I didn't found a great a solution, I'm using process.env to retrieve settings in this particular case

@BrunnerLivio
Copy link
Member

BrunnerLivio commented Jun 22, 2018

The project I described in my upper comment is now opensourced.

You can checkout the API to see how I solved this issue.
The starting point would be start-api.js. Then you can go down further the rabbit hole in main.ts and app.module.ts.

Good luck. Hit me up if you have any questions :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants