-
Notifications
You must be signed in to change notification settings - Fork 203
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
Comments
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 ( 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 :) |
This is a really nice approach, I think I will try this for my use case. I'm closing this issue.. |
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 |
The project I described in my upper comment is now opensourced. You can checkout the API to see how I solved this issue. Good luck. Hit me up if you have any questions :) |
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
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!
The text was updated successfully, but these errors were encountered: