dbos-inc / dbos-transact

The Transactional TypeScript Framework
https://docs.dbos.dev
MIT License
278 stars 19 forks source link

What is the recommended way of accessing configuration env settings #499

Closed demetris-manikas closed 3 weeks ago

demetris-manikas commented 4 weeks ago

In order to configure the kafka from dbos-config env I did the following (copy/paste from knexfile.js)

import { KafkaConfig, KafkaMessage} from "kafkajs";
import { Workflow, WorkflowContext, Kafka, KafkaConsume } from '@dbos-inc/dbos-sdk';
import { parseConfigFile } from '@dbos-inc/dbos-sdk/dist/src/dbos-runtime/config';
const [config, ] = parseConfigFile()
export const brokers = (config.env?.kafka_brokers ?? '').split(',').map(r => r.trim());
export const clientId = config.env?.kafka_clientId ?? '';
export const topic = config.env?.kafka_topic ?? '';

const kafkaConfig: KafkaConfig = {
    brokers: brokers,
    clientId: clientId,
};

@Kafka(kafkaConfig)
export class KafkaConsumer {
  @KafkaConsume(topic)
  @Workflow()
  static async kafkaConsumeWorkflow(ctxt: WorkflowContext, topic: string, partition: number, message: KafkaMessage) {
      ctxt.logger.info(`Message received: ${message.value?.toString()}`);
      return Promise.resolve();
  }
}

I would expect the env variables to be present in process.env or be accessible via a library call. I would a lot rather

import { ENV } from '@dbos-inc/dbos-sdk'

or

export const topic = process.env.kafka_topic ?? '';

and go from there than having to

import { parseConfigFile } from '@dbos-inc/dbos-sdk/dist/src/dbos-runtime/config';
const [config, ] = parseConfigFile()

every time I need to access an env variable.

Is there a better way to do this?

Thanks.

demetris-manikas commented 3 weeks ago

Well... Sorry. Mabe a typo made me think that env is not in process.env but it is.

demetris-manikas commented 3 weeks ago

closing