Missing a way to read env variables in the db connection files

It would be great if environment variables could be loaded on server start and read for:

  • DB connection files
  • APIs (including S3 connectors)

This would include changing how getDbConnection and setDbConnection are implemented so they can evaluate dynamic expressions in the json connection file.

Great for when you are just using Wappler to build, but you want to handle deployment manually.

I can’t tell if you are asking about LOADING env vars on the server (I think you already know that), or USING them in new places.

Just in case it is loading them…here’s an example for Docker/nodejs:

In the .wappler folder for the project, modify the docker-compose.yml file that is contained with each target:

services:
  web:
    env_file:
      - web-variables.env

and then create the web-variables.env file alongside it, which is just a text file with all your vars:

API_PASS="THEPASSWORD"
2 Likes

Thanks Ken! I am not using docker to deploy for this project though. I only use docker at the moment to develop.

Indeed to actually load them is not the problem. Once I have deployed to the servers I just use whatever the hosting provider has for that.I can work around not having them in UI. I was referring to make it easier via UI for the local node server that Wappler has.

What I really miss is being able to use them in the json connection files so that I can use the same connection file for all the environments I use (local machine, staging and production).

Right now what I have to do is push to remote git with the remote db connection details. Then revert to the local docker connection details and add to .gitignore so I don’t overwrite it when deploying again.

1 Like

Same, although admittedly I haven’t looked lately for a new way – just got in the habit early on.

1 Like

Well actually you can define and use already environment variables in server connect.

Those are just available as $_ENV[“varname”]
And you can use them in all expressions.

You can also make dynamic database connections like this by just entering {{$_ENV[“dbconn”]}}

Does node parse that expression when coming from the json file? When I tried it treated it as text.

Well it depends how you read the json file.

$_ENV variables are just available in all server connect settings and expressions.

Nono, I mean Wappler’s json connection file. The one in /app. To set an environment variable inside the app/modules/connections/db.json so I can set the appropriate variables in the server an use the same connection file name for all environments.

Yes you can fully use them there!

Just enter all the server connect database connection properties on the dialog as expressions like {{$_ENV[“dbname”]}}

I guess we can add a data picker there as well.

I believe it’s not parsed.

image

Hmm should be - @patrick will check it out

1 Like

The options for the connection do currently not accept expressions. Please test the following update, place the file in lib/core.

app.zip (2.5 KB)

2 Likes

Thanks @patrick

I confirm it works.

For my use case it would be enough to be able to add environment variables in the target that are loaded on server start. Plus the picker you mention of course.

@George

Just to mention that this approach is not compatible with the db manager. If these environment variables could be set in the target they could be read by the db manager.

1 Like

Yes indeed db manager needs real values for its direct connections.

But isn’t that ok? You need direct connections to manage your database. While in server connect you can use variables?

Yep. Indeed that would be ideal.

I need to do some testing but the DB manager values also got overwritten by the variables when setting them.

Hi @George ,

Indeed it is being overwritten.

I set up SC Connection with the variables then I set Direct Connection with the real values and the SC connection variables get overwritten with the real values.

As a workaround I use an external editor to edit manually the files. So just so you guys know.