gpt4 book ai didi

typescript - NestJS 使用 .env 和 @nestjs/config 设置 TypeOrm 连接

转载 作者:行者123 更新时间:2023-12-04 14:11:49 25 4
gpt4 key购买 nike

我试图找到使用 .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 can't resolve dependencies of the TypeOrmConfigService (?). Please make sure that the argument at index [0] is available in the TypeOrmCoreModule context.如何解决?
或者(最喜欢的)是否有 NestJs + TypeOrm + @nestjs/config + .env(在 repo 之外,带有 DATABASE_PASSWORD)+ config(我的意思是处理 config/development.yml、config/的 npm 包配置)的例子production.yml 等)?
似乎我正在寻找一个非常标准的东西,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...

最佳答案

这是我在 app.module.ts 中使用 Postgres 的工作脚本,但对于 MySQL 将非常相似。有时在重建之前需要手动删除 dist 文件夹 - 当 DB 无法同步时。

import { ConfigModule, ConfigService } from '@nestjs/config';


@Module({
imports: [
ConfigModule.forRoot(),
TypeOrmModule.forRootAsync({
imports: [ConfigModule],
useFactory: (configService: ConfigService) => ({
type: 'postgres' as 'postgres',
host: configService.get<string>('DATABASE_HOST'),
port: parseInt(configService.get<string>('DATABASE_PORT')),
username: configService.get<string>('DATABASE_USER'),
password: configService.get<string>('DATABASE_PASS'),
database: configService.get<string>('DATABASE_NAME'),
entities: [__dirname + '/**/*.entity{.ts,.js}'],
synchronize: true,
}),
inject: [ConfigService],
}),
.env 在根文件夹中
DATABASE_USER=
DATABASE_PASS=
DATABASE_HOST=
DATABASE_NAME=
DATABASE_PORT=

关于typescript - NestJS 使用 .env 和 @nestjs/config 设置 TypeOrm 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63678216/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com