herbsjs / herbs-cli

Herbs CLI
https://herbsjs.org/
MIT License
29 stars 30 forks source link

MongoDB - variables for connecting to the DB #192

Open folive3 opened 1 year ago

folive3 commented 1 year ago

Problem Description

In the mongo.js file, two environment variables are defined: $MONGO_DATABASE and $MONGO_CONN_STR. When creating the .env file, if the $ character is used to name the variables, the dotenv and sugar-env libraries won't be able to locate them. As a result, the connection to the database won't be established. Variable names in .env files should not start with the $ character, as this is not in line with best practices. Therefore, when using MongoDB, in addition to creating the .env file with the variables and their values, it's also necessary to edit the mongo.js file to remove the $ character.

Steps to Reproduce

Create a new project as shown below:

herbs new Project Config

Example of generated mongo.js file

MongoJS Default Values

Suggested correction:

Remove the $ character from the variables in the mongo.ejs template file. Implement the automatic creation of the .env file, which by default can contain the same fallback values for their respective variables (similar to what's done in mongo.ejs), as shown in the example below:

.env MONGO_DATABASE='<%- props.dbName %>' MONGO_CONN_STR=mongodb://localhost:27017

This will ensure that the variable names comply with naming guidelines and that the libraries can properly load the environment variables. Additionally, the automatic creation of the .env file will streamline the configuration process.