我正在尝试找到最合法的方法,使用.env文件设置NestJS数据库。也就是说,我想使用@nestjs/config
包导入.env变量,并在TypeOrmModule中使用它们。
看起来我需要使用TypeOrmModule.forRootAsync
。
我正在尝试这样做:
// app.module.ts
@Module({
imports: [
ConfigModule.forRoot({
isGlobal: true,
}),
TypeOrmModule.forRootAsync({
useClass: TypeOrmConfigService,
}),
...
],
})
export class AppModule {}
然后,还有TypeOrmConfigService
:
import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { TypeOrmModuleOptions, TypeOrmOptionsFactory } from '@nestjs/typeorm';
@Module({
imports: [ConfigModule],
})
export class TypeOrmConfigService implements TypeOrmOptionsFactory {
constructor(private configService: ConfigService) {}
createTypeOrmOptions(): TypeOrmModuleOptions {
return {
type: 'mysql',
host: this.configService.get('DATABASE_HOST'),
username: this.configService.get('DATABASE_USERNAME'),
password: this.configService.get('DATABASE_PASSWORD'),
};
}
}
最后一条错误是不正确的:
Nest无法解析TypeOrmConfigService的依赖项(?)。 请确保索引[0]处的参数在TypeOrmCoreModule上下文中可用。
如何修复它?
或者(最好的方式)是否有任何NestJs + TypeOrm + @nestjs/config + .env(位于repo之外,带有DATABASE_PASSWORD)+ config(我指处理config/development.yml、config/production.yml等的npm包配置文件)的示例?看起来我正在寻找一个非常标准的东西,即Hello World,它应该是每个NestJS项目的起点,但我发现难以结合@nestjs/config和TypeOrm。
更新。 如果我将
@Module
替换为@Injectable
,则错误完全相同:yarn run v1.22.4
$ NODE_ENV=development nodemon
[nodemon] 1.19.0
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: /home/kasheftin/work/pubngn4/nestjs-course-task-management/src/**/*
[nodemon] starting `ts-node -r tsconfig-paths/register src/main.ts`
[Nest] 25384 - 09/01/2020, 8:07 PM [NestFactory] Starting Nest application...
[Nest] 25384 - 09/01/2020, 8:07 PM [InstanceLoader] AppModule dependencies initialized +11ms
[Nest] 25384 - 09/01/2020, 8:07 PM [InstanceLoader] TypeOrmModule dependencies initialized +0ms
[Nest] 25384 - 09/01/2020, 8:07 PM [InstanceLoader] PassportModule dependencies initialized +0ms
[Nest] 25384 - 09/01/2020, 8:07 PM [ExceptionHandler] Nest can't resolve dependencies of the TypeOrmConfigService (?). Please make sure that the argument at index [0] is available in the TypeOrmCoreModule context. +1ms
Error: Nest can't resolve dependencies of the TypeOrmConfigService (?). Please make sure that the argument at index [0] is available in the TypeOrmCoreModule context.
at Injector.lookupComponentInExports (/home/kasheftin/work/pubngn4/nestjs-course-task-management/node_modules/@nestjs/core/injector/injector.js:180:19)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
at Injector.resolveComponentInstance (/home/kasheftin/work/pubngn4/nestjs-course-task-management/node_modules/@nestjs/core/injector/injector.js:143:33)
at resolveParam (/home/kasheftin/work/pubngn4/nestjs-course-task-management/node_modules/@nestjs/core/injector/injector.js:96:38)
at async Promise.all (index 0)
at Injector.resolveConstructorParams (/home/kasheftin/work/pubngn4/nestjs-course-task-management/node_modules/@nestjs/core/injector/injector.js:112:27)
at Injector.loadInstance (/home/kasheftin/work/pubngn4/nestjs-course-task-management/node_modules/@nestjs/core/injector/injector.js:78:9)
at Injector.loadProvider (/home/kasheftin/work/pubngn4/nestjs-course-task-management/node_modules/@nestjs/core/injector/injector.js:35:9)
at async Promise.all (index 3)
at InstanceLoader.createInstancesOfProviders (/home/kasheftin/work/pubngn4/nestjs-course-task-management/node_modules/@nestjs/core/injector/instance-loader.js:41:9)
1: 0xa2afd0 node::Abort() [/home/kasheftin/.nvm/versions/node/v14.3.0/bin/node]
2: 0xa9e7a9 [/home/kasheftin/.nvm/versions/node/v14.3.0/bin/node]
3: 0xc06bab [/home/kasheftin/.nvm/versions/node/v14.3.0/bin/node]
4: 0xc08156 [/home/kasheftin/.nvm/versions/node/v14.3.0/bin/node]
5: 0xc087d6 v8::internal::Builtin_HandleApiCall(int, unsigned long*, v8::internal::Isolate*) [/home/kasheftin/.nvm/versions/node/v14.3.0/bin/node]
6: 0x13a9f19 [/home/kasheftin/.nvm/versions/node/v14.3.0/bin/node]
Aborted (core dumped)
[nodemon] app crashed - waiting for file changes before starting...
@Injectable
转换为@Module
的原因,因为后者支持导入属性,但仍然有些东西缺失。 - Kasheftin@Injectable()
而不是@Module()
时,您会遇到什么错误?这应该是一个提供程序,因此应该是@Injectable()
。我以前用过这种模式。 - Jay McDoniel