Configure sequelize using config.js and sequelizerc

This commit is contained in:
2022-04-17 16:39:56 +02:00
parent 845005bcce
commit 0419733925
4 changed files with 37 additions and 6 deletions

5
.sequelizerc Normal file
View File

@@ -0,0 +1,5 @@
const path = require('path');
module.exports = {
'config': path.resolve('config', 'config.js')
};

View File

@@ -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
View 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"
}
};

View File

@@ -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)