ozomer / node-red-contrib-mongodb2

MongoDB driver node for Node-RED
Apache License 2.0
15 stars 19 forks source link

mongodb uri with mustache style #10

Closed rafael-pinho closed 7 years ago

rafael-pinho commented 7 years ago

Hi there,

When I configure mongodb server uri, I need to put the uri as a string, like 'mongodb://localhost:27017'. But in my case, this uri will stay in a environment variable. In my settings.js I put the process.env values in the global context, then in a function box I get the environment variable value in the global context like this:

msg.mongoUri = context.global.env.MONGO_URI

So, is there a way to pass msg.mongoUri value to mongodb box uri field? If not, can I send you a pull request to make this possible using mustache style? It would be like this:

image

Best regards

Rafael

And by the way...sorry for my bad english skills.

ozomer commented 7 years ago

Hi, This is a general problem discussed here. Solving it for strings is not won't be enough, what about numerical values? checkboxes?

In my place we solved this issue by replacing the storage module that is used to store the flows (and their changes). There are few examples for implementing storage module here and here.

We are using mongodb to store the flows, in a code similar to this. Notice the preprocess function. It is called for each node configuration and loops on the values. Whenever the value is an object with type: "config", it runs a vm on key (with process.env for its context). For example, if the object is: {"type": "config", "key": "MONGO_URI"} it will be replaced with your MONGO_URI environment variable.

Now all you have to do is to save the flows with a mongo uri of: "BLABLABLA", shutdown node-red, manually change the "BLABLABLA" value in your db (or local-storage) with {"type": "config", "key": "MONGO_URI"} and restart node-red.

You can also use this trick for numerical values, and write short code in key, like: {"type": "config", "key": "(MY_ENV_VAR * 1) || 0"}.

Regards, Oren

P.S. Notice also that the saveFlows function does not simply overrides the configuration. It checks if the value actually changed, and otherwise keeps the old value. So if you change a different field in the configuration, the old field, which is saved as {"type": "config", "key", "..."}, will remain as-is.

rafael-pinho commented 7 years ago

Hi Oren.

I understand and I'm going to implement a storage module to solve my problem too. I found the same issue in "other boxes" and I think this solution will solve them all. I will share my implementation when I finish it.

Best regards, Rafael

rslangham commented 7 years ago

I was able to get this to work using env variables in the format of $(VARIABLE) in the mongodb2 server config for the URI, user, and password. In research of this, I found where it was stated the env value reference will work for almost any Node-Red node config text entry. I placed the env format content in each of the fields and verified that it accessed and used environment variables that I had defined, e.g. $(MONGODB_USER). $(MONGODB_URI), ...

I did find one minor bug with this. When you close the mongodb server and config dialogs and deploy, then go back and re-open them again, the user text field will contain the value (e.g. bobjones) of the environment user specified. I did some additional tests to see if the underlying reference was still there. I changed the env user variable value in the system and mongodb access failed. Changed it back to the correct value and it worked. So, appears to be just a display issue in the config dialog. Though, pretty sure if I had modified something else in the config dialog and saved it, that it would lose my $(env) reference. So, just have to remember to re-input the user env field if ever modify anything in the config dialog.