Commandbox, .env, and multiple datasources

So, the .env file gets entries like these:
DB_HOST=192.168.60.79
DB_PORT=1433
DB_DRIVER=MSSQL
DB…etc.

And if I correct it, very nicely, a server restart makes it effective.

But how do I add a SECOND datasource? Can it be done through the Commandbox server definition?

To be clear, the existence of those env vars doesn’t define a datasource. The datasource is presumable defined in your .cfconfig.json file, which uses these env var placeholders.

So if you want to have two datasources, I recommend changing your naming convention so the env vars are called something like:

DB_HOST_MYDSN1=192.168.60.79

DB_HOST_MYDSN2=192.168.60.80

and then modify the JSON file to use the correct env var where required.

And for what it’s worth, I’ve never understood the allure of extracting mundane never-changing details such as the db driver and port into an env var. My rule of thumb for what to put in an env var is

  • data that changes between environments or just often in general
  • sensitive data you don’t want committed to the repo like passwords.

So, the hostname may be different between dev/stage/prod and the password is a secret, but chances are you’re using MSSQL on all servers with no plan to change any time soon, so having an env var placeholder for that just seems like effort for the sake of effort. There are a handful of places where you’ll see that in examples such as the ColdBox app templates, but I’ll just say I didn’t create those :slight_smile:

Also, FWIW, it’s possible to configure your datasources in the Lucee/Adobe admin and export it out to your JSON file AND have CFConfig automatically extract the dynamic bits into your .env file automatically. Check out these docs:

Very helpful, thank you. As to the .env vars, yes, I’m getting that from CB docs! :slight_smile:
But very good to know that it’s not Commandbox (directly) driving it, that the cfconfig.json file is running the show and I see the datasources is an array.

Which means, too, I can drop the MySQL stuff out of there I’m not using. Once I get this all squared away, then I can move on to getting Contentbox thrown in the mix. So far, not great results. First time, got an ORM error. Second time, I got nothing! (More on this later!) Trying to git commit in stages so I have a known working point before each step.

1 Like