NestJS将ConfigService与TypeOrmModule一起使用 [英] NestJS Using ConfigService with TypeOrmModule

查看:994
本文介绍了NestJS将ConfigService与TypeOrmModule一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我按照docs https://docs.nestjs.com/techniques/configuration中的说明设置了ConfigService

如何与TypeOrmModule一起使用此服务?

How can I use this service with the the TypeOrmModule?

TypeOrmModule.forRoot({
  type: 'mysql',
  host: 'localhost',
  port: 3306,
  username: 'root',
  password: 'root',
  database: 'test',
  entities: [__dirname + '/**/*.entity{.ts,.js}'],
  synchronize: true,
}),

推荐答案

请参见 https://docs.nestjs .com/techniques/database 异步配置一章

import {ConfigService} from './config.service';
import {Module} from '@nestjs/common';
import {TypeOrmModule} from '@nestjs/typeorm';

@Module({
    imports: [
        TypeOrmModule.forRootAsync({
            imports: [ConfigModule],
            useFactory: (config: ConfigService) => config.get('database'),
            inject: [ConfigService],
        }),
    ],
})
export class AppModule {}

这篇关于NestJS将ConfigService与TypeOrmModule一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆