윤준석

ADD: node.js sequelize orm install and initialization

1 +{
2 + "development": {
3 + "username": "root",
4 + "password": "mamuri",
5 + "database": "mamuri_db",
6 + "host": "127.0.0.1",
7 + "dialect": "mysql"
8 + },
9 + "test": {
10 + "username": "root",
11 + "password": null,
12 + "database": "database_test",
13 + "host": "127.0.0.1",
14 + "dialect": "mysql"
15 + },
16 + "production": {
17 + "username": "root",
18 + "password": null,
19 + "database": "database_production",
20 + "host": "127.0.0.1",
21 + "dialect": "mysql"
22 + }
23 +}
1 +'use strict';
2 +
3 +const fs = require('fs');
4 +const path = require('path');
5 +const Sequelize = require('sequelize');
6 +const basename = path.basename(__filename);
7 +const env = process.env.NODE_ENV || 'development';
8 +const config = require(__dirname + '/../config/config.json')[env];
9 +const db = {};
10 +
11 +let sequelize;
12 +if (config.use_env_variable) {
13 + sequelize = new Sequelize(process.env[config.use_env_variable], config);
14 +} else {
15 + sequelize = new Sequelize(config.database, config.username, config.password, config);
16 +}
17 +
18 +fs
19 + .readdirSync(__dirname)
20 + .filter(file => {
21 + return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
22 + })
23 + .forEach(file => {
24 + const model = require(path.join(__dirname, file))(sequelize, Sequelize.DataTypes);
25 + db[model.name] = model;
26 + });
27 +
28 +Object.keys(db).forEach(modelName => {
29 + if (db[modelName].associate) {
30 + db[modelName].associate(db);
31 + }
32 +});
33 +
34 +db.sequelize = sequelize;
35 +db.Sequelize = Sequelize;
36 +
37 +module.exports = db;