Skip to content

Commit

Permalink
Update sequelize config
Browse files Browse the repository at this point in the history
  • Loading branch information
WSMathias committed Jul 22, 2020
1 parent 7c83295 commit ac8c5c8
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 20 deletions.
2 changes: 1 addition & 1 deletion starterkits/node-express-postgres/.sequelizerc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const path = require('path');

module.exports = {
"config": path.resolve('./server/config', 'config.json'),
"config": path.resolve('./server/config', 'config.js'),
"models-path": path.resolve('./server/models'),
"seeders-path": path.resolve('./server/seeders'),
"migrations-path": path.resolve('./server/migrations')
Expand Down
20 changes: 10 additions & 10 deletions starterkits/node-express-postgres/server/config/config.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
{
"development": {
"username": "pguser",
"password": "mypassword",
"database": "mypgdb",
"host": "postgres",
"username": "root",
"password": null,
"database": "database_development",
"host": "127.0.0.1",
"dialect": "postgres"
},
"test": {
"username": "root",
"password": null,
"database": "database_test",
"host": "127.0.0.1",
"dialect": "mysql"
"dialect": "postgres"
},
"production": {
"username": "qwerty",
"password": "'SFKWEOPFGKLFGJEIORGUOERGHWROGQOjbkldfbjdfbdgh'",
"database": "slkfsaldf9ds08f",
"host": "ec2-xx-xx-xx-xx.compute-1.amazonaws.com",
"dialect": "postgresql"
"username": "root",
"password": null,
"database": "database_production",
"host": "127.0.0.1",
"dialect": "postgres"
}
}
35 changes: 26 additions & 9 deletions starterkits/node-express-postgres/server/models/index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,37 @@
'use strict';

var Sequelize = require('sequelize');
var env = process.env.NODE_ENV || 'development';
var config = require(__dirname + '/../config/config.js')[env];
var db = {};

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;
if (config.use_env_variable) {
var sequelize = new Sequelize(process.env[config.use_env_variable]);
sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
var sequelize = new Sequelize(config.database, config.username, config.password, config);
sequelize = new Sequelize(config.database, config.username, config.password, config);
}

fs
.readdirSync(__dirname)
.filter(file => {
return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
})
.forEach(file => {
const model = require(path.join(__dirname, file))(sequelize, Sequelize.DataTypes);
db[model.name] = model;
});

Object.keys(db).forEach(modelName => {
if (db[modelName].associate) {
db[modelName].associate(db);
}
});

db.sequelize = sequelize;
db.Sequelize = Sequelize;

db.users = require("./user.js")(sequelize, Sequelize);

module.exports = db;

0 comments on commit ac8c5c8

Please sign in to comment.