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

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

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

  1. @Module({
  2. imports: [
  3. ConfigModule
  4. ClientsModule.register([
  5. {
  6. name: 'producer',
  7. transport: Transport.KAFKA,
  8. options: {
  9. client: {
  10. clientId: 'messages',
  11. brokers: [...BROKERS.split(',')],
  12. ssl: {
  13. rejectUnauthorized: false,
  14. },
  15. sasl: {
  16. mechanism: 'scram-sha-512',
  17. username, // This is what i want to fetch
  18. password, // This is what i want to fetch
  19. },
  20. },
  21. consumer: {
  22. groupId: 'client',
  23. sessionTimeout: 60000,
  24. minBytes: 5,
  25. maxBytes: 40000000,
  26. },
  27. },
  28. },
  29. ]),
  30. ],
  31. controllers: [ProducerController],
  32. providers: [ProducerService],
  33. })
  34. 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

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

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

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

  1. @Module({
  2. imports: [yourFunction()], // Made an async func and works!
  3. controllers: [ProducerController],
  4. providers: [ProducerService],
  5. })
  6. 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:

确定