using an enviroment variable for local sequelize configuration

前端 未结 4 1050
感情败类
感情败类 2021-02-04 07:25

I\'m looking to use an environment variable inside of the config.json file of my project using sequelize. I\'m using dotenv to set environment variables locally. My config.json

相关标签:
4条回答
  • 2021-02-04 07:39

    I worked on this for quite a bit. I do not know why Sequelize does not use production when it is literally in the environment if you run heroku run bash. I was able to get it working by modifying the Sequelize object depending on the JAWSDB_URL, not the NODE_ENV.

    require("dotenv").config();
    const express = require("express")
    const app = express();
    let seq;
    
    //express app configuration
    
    if (process.env.JAWSDB_URL) {
        console.log("There is a JAWS DB URL")
        seq = new Sequelize(process.env.JAWSDB_URL)
    }
    else {
        seq = require("./models").sequelize
    }
    seq.sync().then(() => {
      app.listen(PORT, () => console.log('server started on port ' + PORT));
    })
    
    0 讨论(0)
  • 2021-02-04 07:49

    you should change config.json file to a config.js module and make sure to require the dotenv at the very top.

    require('dotenv').config(); // this is important!
    module.exports = {
    "development": {
        "username": process.env.DB_USERNAME,
        "password": process.env.DB_PASSWORD,
        "database": process.env.DB_DATABASE,
        "host": process.env.DB_HOST,
        "dialect": "mysql"
    },
    "test": {
        "username": "root",
        "password": null,
        "database": "database_test",
        "host": "127.0.0.1",
        "dialect": "mysql"
    },
    "production": {
        "username": "root",
        "password": null,
        "database": "database_production",
        "host": "127.0.0.1",
        "dialect": "mysql"
    }
    };
    

    NOTE: update your .sequelizerc file to match the new config file.

    "config": path.resolve('./config', 'config.js'),
    
    0 讨论(0)
  • 2021-02-04 07:51

    Assuming you're using Passport, Sequelize, MySql:

    On the index.js file setup through sequelize, look for this line:

    var sequelize = new Sequelize(config.database, config.username, config.password, config);
    

    Try changing it to:

    var sequelize = new Sequelize(process.env.DB_DATABASE, process.env.DB_USERNAME, process.env.DB_PASSWORD, config);
    

    Your .env should have:

    DB_USERNAME:root (or whatever your username is)
    DB_PASSWORD:NYB (whatever your password is)
    DB_DATABASE:whatever_your_dbNameis_db
    

    Last, depending on how your passport strategy is setup, look for something that says:

    user.sequelize.sync().then(function(){
    }...
    

    You are going to need to place the: database:process.env.DB_DATABASE

    user.sequelize.sync().then(function(){
         database:"process.env.dbn"
    }...
    

    This means that you should remove that key:value from the config.json.

    It should be good to go and you will not have to convert anything. Sequelize will take care of all of that.

    0 讨论(0)
  • 2021-02-04 08:01

    you can use the use_env_variable for development too. you already seem to be using dotenv, so im assuming you have a .env file already setup. just add this line to it:

    LOCALDB=mysql://[user]:[pass]@[sqldomain]/[db name]

    replace the stuff in [] as necessary, and in your config file set "use_env_variable" to LOCALDB, just like how you have JAWSDB for production.

    0 讨论(0)
提交回复
热议问题