【问题标题】:I need to pass username and password to typeORM on runtime我需要在运行时将用户名和密码传递给 typeORM
【发布时间】:2020-06-02 19:21:50
【问题描述】:

在 NodeJS (NestJS) 上,我使用 TypeORM 连接到数据库 (Oracle)。我想在应用程序运行时传递用户名和密码。

出于安全原因,在我的公司中,安全性配置为为每个用户创建一个数据库模式。他使用他的数据库凭证登录到应用程序。我知道这在业界不是很流行的做法。

const connection = await createConnection({
    type: "oracle",
    host: "localhost",
    port: 3306,
    username: "test", // this need to pass during run time 
    password: "test", // same thing for the password 
    database: "test"
});

您能否与我分享有关如何在 typeORM 和 nodeJs/NestJs 上实现此目标的任何参考/提示?

谢谢

【问题讨论】:

    标签: node.js oracle nestjs typeorm


    【解决方案1】:

    您可以在根级别使用注册 TypeORM,并使用配置服务提供连接详细信息:

        TypeOrmModule.forRootAsync({
            imports: [ConfigModule],
            useFactory: (config: ConfigService) => ({
                host: config.get('DB_HOST'),
                username: config.get('DB_USER'),
                password: config.get('DB_PASSWORD'),
                database: config.get('DB_NAME'),
                entities: [__dirname + '/**/*.entity{.ts,.js}'],
                port: 3306,
                type: 'oracle',
            }),
            inject: [ConfigService],
        }),
    

    ConfigService 只是一个简单的服务,它实现了一个提供特定请求配置的get 函数。

    在此处了解更多信息:https://docs.nestjs.com/techniques/database#async-configuration

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-19
      • 1970-01-01
      • 1970-01-01
      • 2020-02-15
      • 1970-01-01
      • 1970-01-01
      • 2022-11-19
      • 2012-02-12
      相关资源
      最近更新 更多