How to connect database with different credential

2019-08-29 10:12发布

问题:

I want to connect the databases which are for the different servers (like dev, staging, live, production). There are different databases and credential for all the server. When a server will be called it will access data only that particular database. In my code, I am using node js with sequelize for the database connection to build the API in GraphQL.How can I do this? I have already tried a lot. Please suggest to me. If need code I can post it here.

回答1:

The best way to do this is to keep a seperate .env file on each server. and load the credentials form .env file itself by using dotenv module.

you can load credenials by process.env.CREDENTIALS_KEY

Example

DB_USERNAME=username
DB_PASSWPRD=password
DB_DATABASE=db_name
DB_HOST=localhost

Load in your js/ts file like:

    "USERNAME": process.env.DB_USERNAME,
    "PASSWORD": process.env.DB_PASSWPRD,
    "DATABASE": process.env.DB_DATABASE,
    "HOST": process.env.DB_HOST,