Configure sequelize using config.js and sequelizerc
This commit is contained in:
5
.sequelizerc
Normal file
5
.sequelizerc
Normal file
@@ -0,0 +1,5 @@
|
||||
const path = require('path');
|
||||
|
||||
module.exports = {
|
||||
'config': path.resolve('config', 'config.js')
|
||||
};
|
||||
@@ -5,3 +5,11 @@
|
||||
```bash
|
||||
$ docker-compose -f docker-compose.yml -f docker-compose.dev.yml up
|
||||
```
|
||||
|
||||
### Sequelize migrations
|
||||
|
||||
If ran outside the container, DB_HOST must be set to localhost or the container's IP address.
|
||||
|
||||
```bash
|
||||
$npx sequelize db:migrate
|
||||
```
|
||||
|
||||
17
config/config.js
Normal file
17
config/config.js
Normal file
@@ -0,0 +1,17 @@
|
||||
require('dotenv').config();
|
||||
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"
|
||||
},
|
||||
"production": {
|
||||
"username": process.env.DB_USERNAME,
|
||||
"password": process.env.DB_PASSWORD,
|
||||
"database": process.env.DB_DATABASE,
|
||||
"host": process.env.DB_HOST,
|
||||
"dialect": "mysql"
|
||||
}
|
||||
};
|
||||
@@ -1,18 +1,19 @@
|
||||
'use strict';
|
||||
|
||||
require('dotenv').config();
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const Sequelize = require('sequelize');
|
||||
const basename = path.basename(__filename);
|
||||
const env = process.env.NODE_ENV || 'development';
|
||||
const config = require(__dirname + '/../config/config.js')[env];
|
||||
const db = {};
|
||||
|
||||
let sequelize = new Sequelize(process.env.DB_DATABASE, process.env.DB_USERNAME, process.env.DB_PASSWORD, {
|
||||
dialect: 'mysql',
|
||||
host: process.env.DB_HOST,
|
||||
port: 3306
|
||||
});
|
||||
let sequelize;
|
||||
if (config.use_env_variable) {
|
||||
sequelize = new Sequelize(process.env[config.use_env_variable], config);
|
||||
} else {
|
||||
sequelize = new Sequelize(config.database, config.username, config.password, config);
|
||||
}
|
||||
|
||||
fs
|
||||
.readdirSync(__dirname)
|
||||
|
||||
Reference in New Issue
Block a user