I'm submitting a...
[ ] Regression
[X] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.
Current behavior
I'm using the SequielizeModule and ConfigModule, I have my database configurations created with registerAs
and loaded with the forRoot method in my ApplicationModule.
In my database module, I import the Sequelize module using the forRootAsync, importing ConfigModule, injecting the ConfigService, and using useClass for Sequelize configurations.
In my configuration class if I use the config service everything works as expected but if I use the @INJECT
decorator with the KEY of the configuration, Nest can't resolve the dependency. (if the ConfigModule is global, using the isGlobal flag, the inject decorator works)
Error:
my_awesome_project_dev_1 | [Nest] 96 - 07/10/2020, 9:54:52 PM [ExceptionHandler] Nest can't resolve dependencies of the SequelizeConfig (?). Please make sure that the argument CONFIGURATION(database) at index [0] is available in the SequelizeCoreModule context.
my_awesome_project_dev_1 |
my_awesome_project_dev_1 | Potential solutions:
my_awesome_project_dev_1 | - If CONFIGURATION(database) is a provider, is it part of the current SequelizeCoreModule?
my_awesome_project_dev_1 | - If CONFIGURATION(database) is exported from a separate @Module, is that module imported within SequelizeCoreModule?
my_awesome_project_dev_1 | @Module({
my_awesome_project_dev_1 | imports: [ /* the Module containing CONFIGURATION(database) */ ]
my_awesome_project_dev_1 | })
my_awesome_project_dev_1 | +5ms
my_awesome_project_dev_1 | Error: Nest can't resolve dependencies of the SequelizeConfig (?). Please make sure that the argument CONFIGURATION(database) at index [0] is available in the SequelizeCoreModule context.
my_awesome_project_dev_1 |
my_awesome_project_dev_1 | Potential solutions:
my_awesome_project_dev_1 | - If CONFIGURATION(database) is a provider, is it part of the current SequelizeCoreModule?
my_awesome_project_dev_1 | - If CONFIGURATION(database) is exported from a separate @Module, is that module imported within SequelizeCoreModule?
my_awesome_project_dev_1 | @Module({
my_awesome_project_dev_1 | imports: [ /* the Module containing CONFIGURATION(database) */ ]
my_awesome_project_dev_1 | })
Expected behavior
NestJs must be able to resolve the namespace dependency.
Minimal reproduction of the problem with instructions
- Create a configuration file with registerAs:
import { registerAs } from '@nestjs/config';
import { Dialect } from 'sequelize';
export default registerAs('database', () => {
const { NODE_ENV } = process.env;
return {
dialect: (process.env.DATABASE_DIALECT || 'mysql') as Dialect,
host: process.env.DATABASE_HOST,
port: parseInt(process.env.DATABASE_PORT || '3306', 10),
username: process.env.DATABASE_USERNAME,
password: process.env.DATABASE_PASSWORD,
database: NODE_ENV === 'test' ? 'e2e' : process.env.DATABASE_NAME,
logging: NODE_ENV === 'development',
};
});
- Import the config module in App:
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { databaseConfig } from './config';
import { DatabaseModule } from './database/database.module';
@Module({
imports: [
ConfigModule.forRoot({
load: [databaseConfig],
ignoreEnvFile: true,
}),
DatabaseModule,
],
})
export class AppModule {}
- Import sequelize module in database module:
import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { SequelizeModule } from '@nestjs/sequelize';
import { SequelizeConfig } from './config';
@Module({
imports: [
SequelizeModule.forRootAsync({
imports: [ConfigModule],
useClass: SequelizeConfig,
inject: [ConfigService],
}),
],
})
export class DatabaseModule {}
- Create the SequelizeConfig class
import { Injectable, Logger, Inject } from '@nestjs/common';
import { ConfigType } from '@nestjs/config';
import {
SequelizeOptionsFactory,
SequelizeModuleOptions,
} from '@nestjs/sequelize';
import { databaseConfig } from '../../config';
@Injectable()
export class SequelizeConfig implements SequelizeOptionsFactory {
constructor(
@Inject(databaseConfig.KEY)
private readonly config: ConfigType<typeof databaseConfig>,
) {}
createSequelizeOptions(): SequelizeModuleOptions {
return {
dialect: this.config.dialect,
host: this.config.host,
port: this.config.port,
username: this.config.username,
password: this.config.password,
database: this.config.database,
logging: this.config.logging
? (sql) => Logger.log(sql, 'Database')
: false,
autoLoadModels: true,
synchronize: false,
};
}
}
What is the motivation / use case for changing the behavior?
Inject namespaces in other modules.
Environment
Nest version: 7.3.1
For Tooling issues:
- Node version: 12.18.1
- Platform: Linux
Others:
"@nestjs/config": "^0.5.0",
"@nestjs/sequelize": "^0.1.0",