#Passing config values to @RabbitSubscribe decorator

41 messages · Page 1 of 1 (latest)

patent pilot
#

I am using @golevelup/nestjs-rabbitmq package.
Is it at all possible to pass values from ConfigService into the @RabbitSubscribe decorator?
I want to have the queues and routing keys etc. be configurable...

chilly bridge
#

It's not possible to use the ConfigService inside a decorator. You can use dotenv to populate process.env and use process.env inside the decorator instead

patent pilot
#

When the decorator is initialized process.env is empty, only after that the configurations are loaded into it 😦

chilly bridge
#

That'swhy I said to use dotenv to populate process.env. If you do that as the first lines in your main.ts then process.env will be properly populated

patent pilot
#

You mean not the ConfigModule but pure dotenv?

chilly bridge
#

And I mean

import { config } from 'dotenv'
config()

As lines 1 and 2

patent pilot
#

I see

#

Shouldn't I just use pure amqplib at this point? This seems kinda hacky

chilly bridge
#

This seems kinda hacky
Why?

patent pilot
#

What if I wanted to load values from Azure for example?

#

Using the @azure/app-configuration package

#

Well in that case I suppose I could load the URL from config to environment variables then use that with the package in the decorator

chilly bridge
#

Overall, what you're seeing is an issue with how decorators are processed at the time of file import. It might be possible to use a lazy loader function and reference the class's properties to get the ConfigService, but that would certanily be hacky

patent pilot
#

Yeah I tried stuff like that, using a static property on a class for example...
The only thing other than what you suggested that allows you to "outsource" a value and have the decorator be familier with it is defining a variable outside of any scope but then it's hardcoded and I didn't solve anything 😛

#

Anyway, thanks for the answer, I'll try to do what you suggested

#

Update: that didn't work either, the decorator is initialized even before the bootstrap sequence...

chilly bridge
#

That's not true. Dedcorators are higher order functions that are ran one file import. What does your main.ts look like?

patent pilot
#
async function bootstrap(): Promise<void> {
  config({ path: `../${process.env.NODE_ENV ?? 'na'}.env` });
  console.log(process.env.TEST);
chilly bridge
#

That's not lines 1 and 2 is it?

patent pilot
#

It is

chilly bridge
#

That's the first lines of the bootstrap, not of the file

patent pilot
#

Ohhhh

chilly bridge
#

They need to be the very first lines of the file

patent pilot
#

I see, it's the import that makes this happen

chilly bridge
#

Literal, lines 1 and 2

patent pilot
#

By importing I make it load the decorator

#

Well, I just moved those lines to the very first and second lines of the main.ts files and I have debug on and it still seems like the decorator's breakpoint is hit before the dotenv config breakpoint...

patent pilot
#

Yeah, nothing I tried works...

chilly bridge
#

Hmm, that should be fine. Got a reproduction?

patent pilot
#

I just left the office at work so I'll work on that later/tomorrow

patent pilot
#

I tried creating the same scenario with a new project and it works there...
Could it be because I set the project with the problem to ES2022?

chilly bridge
#

That shouldn't effect it

patent pilot
#

Weird.. In the project I just created, in debug mode I can see that it reaches the code line first then it reaches the decorator

#

And in my project it's the other way around

#

And the only difference I can see is one is CommonJS and the other is ES2022

chilly bridge
#

I don't believe ES2022 will change that behavior, but you can give it a shot

patent pilot
#

Well, yep, that's the reason

#

I can send you a link to the repo if you wish

#

I changed in tsconfig "module" and "target" to ES2022, added moduleResolution: "node" and changed in package.json to "type": "module" and that makes the problem happen

#

I think it's actually loading the file which has the @RabbitSusbcribe decorator first, not necessarily the decorator itself, since when I put the import { config } from 'dotenv' statement there it does work

chilly bridge
#

Most be how es2022 loads packages and files. I wonder if ts is hoisting something during compilation. I can take a look if you've got the reproduction