在初始化 NestJS 模块之前获取数据。

huangapple go评论69阅读模式
英文:

Fetch data on nestjs module before initialating it

问题

以下是翻译好的部分:

"so I'm trying to create a kafka client and my credentials are on aws KMS, so i need to fetch my sasl username and password before starting my module."

因此,我正在尝试创建一个 Kafka 客户端,我的凭据存储在 AWS KMS 上,所以我需要在启动模块之前获取我的 SASL 用户名和密码。

"Does anyone know how to fetch data on a module first? Thanks"

有人知道如何首先在模块中获取数据吗?谢谢。

"Since this is executed right after a nest run, i cant use an await to first load this on my dotenv and then access it, so i need an async way"

由于这是在 nest 运行之后立即执行的,我无法使用 await 先加载它到我的 dotenv,然后访问它,所以我需要一种异步的方法。

英文:

so I'm trying to create a kafka client and my credentials are on aws KMS, so i need to fetch my sasl username and password before starting my module.

Does anyone know how to fetch data on a module fisrt? Thanks

@Module({
  imports: [
    ConfigModule
    ClientsModule.register([
      {
        name: 'producer',
        transport: Transport.KAFKA,
        options: {
          client: {
            clientId: 'messages',
            brokers: [...BROKERS.split(',')],
            ssl: {
              rejectUnauthorized: false,
            },
            sasl: {
              mechanism: 'scram-sha-512',
              username, // This is what i want to fetch
              password, // This is what i want to fetch
            },
          },
          consumer: {
            groupId: 'client',
            sessionTimeout: 60000,
            minBytes: 5,
            maxBytes: 40000000,
          },
        },
      },
    ]),
  ],
  controllers: [ProducerController],
  providers: [ProducerService],
})
export class ProducerModule {}

Since this is executed right after a nest run, i cant use an await to first load this on my dotenv and then access it, so i need an asnync way

答案1

得分: 0

好的!我做好了,所以你可以在模块中使用异步函数!

@Module({
  imports: [yourFunction()], // 创建了一个异步函数并可以使用!
  controllers: [ProducerController],
  providers: [ProducerService],
})
export class ProducerModule {}
英文:

Okay! I made it, so you can use an async function in the module!

@Module({
 imports: [yourFunction()], // Made an async func and works!
 controllers: [ProducerController],
 providers: [ProducerService],
})
export class ProducerModule {}

huangapple
  • 本文由 发表于 2023年2月23日 21:03:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75545214.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定