How to get the database connection string from the environment variables?



  • Is it possible to get the database connection string from the .env file?
    Or it should be a part of jsreport.config.json only?



  • Here are the docs for configuration loading
    https://jsreport.net/learn/configuration#configuration-sources

    So the .env file isn't used. Use the config file or write a logic that loads .env file inside the jsreport startup file server.js.



  • Could you please guide us on how to load the MongoDB connection string from .env file to jsreport startup file server.js?



  • You can use for example the following lib
    https://github.com/motdotla/dotenv

    and simply add the documented code at the beginning of the jsreport server.js file

    require('dotenv').config()
    

    You can do whatever you want in server.js file. It is interpreted with nodejs just like any other nodejs app. In other words, you can find solutions for such tasks just by searching nodejs forums, blogs...



  • We do not want to keep some sensitive details in the config file. We want to get these details from the .env file.

    So, is the following configuration possible?

    0_1704911261259_upload-f5216b1e-b742-4917-bfea-662704dde480

    What if we want to keep only one common config file for all the environments and load environment-specific config from .env file in server.js?



  • This is our Dockerfile:
    0_1704976125929_upload-c99b1af3-44f4-4c9b-81bb-2815d15d5e1e

    When we placed our MongoDB connection string in server.js:
    0_1704976429494_upload-ec1f9725-3e09-44f1-9100-a6dae6321ba0

    It was throwing this error:
    0_1704976470449_upload-b1b010b0-c5fa-4221-b5e5-d6aa35615d9b

    Could you please help us with this process?

    1. How do we use the MongoDB connection string in server.js?
    2. If we add certain configurations directly in server.js then do we need to remove the respective configurations from jsreport.config.json?
    3. If we want to specify config in server.js (using .env) then do we need to specify all of the configuration in server.js and discard jsreport.config.json?
    4. Do server.js and jsreport.config.json complement each other for configuration?

    Please guide us with the correct steps.



  • How do we use the MongoDB connection string in server.js?

    See the documentation
    https://github.com/jsreport/jsreport/tree/master/packages/jsreport-mongodb-store
    The mongodb connection string is passed like this

    require('jsreport')({
      "store": {
        "provider": "mongodb"
      },
      "extensions": {
        "mongodb-store": {
          "uri": "mongodb://db1.example.net,db2.example.net:2500/?replicaSet=test"
        }
      }
    })
    

    If we add certain configurations directly in server.js then do we need to remove the respective configurations from jsreport.config.json?

    See the documentation
    https://jsreport.net/learn/configuration#configuration-sources

    The options passed in the server.js has the highest priority


Log in to reply
 

Looks like your connection to jsreport forum was lost, please wait while we try to reconnect.