ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

์‚ฌ์šฉํ•˜๋‹ค๋ณด๋‹ˆ ๋ช‡ ๊ฐ€์ง€ ๋ฌธ์ œ์ ์ด ์žˆ์–ด์„œ ํ•ด๊ฒฐ๋ฐฉ๋ฒ•์€ ์ถ”ํ›„์— ์—…๋ฐ์ดํŠธํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

์›๋ž˜๋Š” ํ˜ผ์ž ๊ณต๋ถ€๋ชฉ์ ์œผ๋กœ ์“ฐ๋‹ค๊ฐ€ ์‹ค๋ฌด์—์„œ ์ ์šฉํ•˜๋ ค๋‹ค๋ณด๋‹ˆ ๊ธฐ์กด์— ์“ฐ๋˜ ๊ตฌ์กฐ์—์„œ ๋ณ€๊ฒฝ์˜ ํ•„์š”์„ฑ์˜ ๋Š๊ปด์กŒ์Šต๋‹ˆ๋‹ค. ์ด์œ ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

  1. ์ƒ์šฉ๊ณผ ๊ฐœ๋ฐœํ™˜๊ฒฝ์— ๋”ฐ๋ผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๊ฐ€ ๋‹ค๋ฆ…๋‹ˆ๋‹ค.
  2. ํ•˜๋‚˜์˜ ํ”„๋กœ๊ทธ๋žจ์—์„œ ํ•„์š”์— ์—ฌ๋Ÿฌ๊ฐœ์˜ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.
  3. ๊ฐœ๋ฐœ์„œ๋ฒ„DB์™€ ์ƒ์šฉ์„œ๋ฒ„DB๊ฐ€ ์™„๋ฒฝํ•˜๊ฒŒ ์ผ์น˜ํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— entity ์ƒ์„ฑ๋„ ๋ณ„๊ฐœ๋กœ ๋˜์–ด์•ผํ•ฉ๋‹ˆ๋‹ค.

์ด์ „ ์ฑ•ํ„ฐ๊นŒ์ง€ ์ง„ํ–‰ํ–ˆ๋‹ค๋ฉด sequelize ๋ฌธ๋ฒ• ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•œ js ํŒŒ์ผ๋“ค (๋ณดํ†ต entity๋ผ๊ณ  ํ•˜๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.) ์ด ์ƒ์„ฑ๋˜์—ˆ์„ ๊ฒ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์œ„์™€๊ฐ™์€ ์ œ์•ฝ์‚ฌํ•ญ์„ ํ•ด๊ฒฐํ•ด์•ผํ•œ๋‹ค๋ฉด index.js ํŒŒ์ผ๋ถ€ํ„ฐ sequelize-auto ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•˜์—ฌ entity๋ฅผ ์ƒ์„ฑํ•˜๋Š” js ํŒŒ์ผ๋„ ์ˆ˜์ •์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

 

์ง€๊ธˆ๋ถ€ํ„ฐ ์“ฐ๋Š” ์ฝ”๋“œ๋Š” ๊ธฐ์กด๊ณผ ๋‹ค๋ฅธ ํ™˜๊ฒฝ์—์„œ ์ž‘์„ฑ๋˜๋Š” ์ฝ”๋“œ์ด๋ฏ€๋กœ ํŒŒ์ผ๊ฒฝ๋กœ๋‚˜ DB๋ช…์€ ์ „ํ˜€ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ์ผ๋‹จ ๋””๋ ‰ํ† ๋ฆฌ ๊ตฌ์กฐ๋Š” ์ด๋ ‡์Šต๋‹ˆ๋‹ค.

_DEV์ด ๋ถ™๋Š” ๋””๋ ‰ํ† ๋ฆฌ๋Š” ๊ฐœ๋ฐœ์„œ๋ฒ„์šฉ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๊ทธ์™€ ๋ฐ˜๋Œ€๋กœ _DEV๊ฐ€ ์—†์œผ๋ฉด ์ƒ์šฉ์„œ๋ฒ„ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์ธ๊ฑฐ์ฃ . sequelize ์ปค๋„ฅ์…˜์„ ๋ฐ˜ํ™˜ํ•˜๋Š” index.js ์ฝ”๋“œ๋Š” multiple database connection๋ฅผ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•˜๊ธฐ์œ„ํ•ด ๋ฐ”๋€ ๋ถ€๋ถ„์ด ์žˆ์Šต๋‹ˆ๋‹ค.

 

'use strict';

var fs        = require('fs');
var path      = require('path');
var Sequelize = require('sequelize');
var basename  = path.basename(__filename);
var env       = process.env.NODE_ENV;
var config = {};
var db = {};

if (process.env.BY_LOCAL) {
  config = require(__dirname + '/../../../../config/sequelize.local.json')[env];
} else {
  config = require(__dirname + '/../../../../config/sequelize.json')[env];
}

/* Add Databases */
const databases = Object.keys(config.databases);
for(let i=0; i < databases.length; i++) {
  let database = databases[i];
  let dbPath = config.databases[database];
  db[database] = new Sequelize(dbPath.database, dbPath.username, dbPath.password, dbPath);
}

fs
  .readdirSync(__dirname + `/${databases[0]}`)
  .filter(file => 
      (file.indexOf('.') !== 0) &&
      (file !== basename) && 
      (file.slice(-3) === '.js'))
  .forEach(file => {
    const model = db[`${databases[0]}`].import(path.join(__dirname + `/${databases[0]}`, file));
    db[model.name] = model;
 });

//  Add models from database  

 fs
  .readdirSync(__dirname + `/${databases[1]}`)
  .filter(file => {
      (file.indexOf('.') !== 0) &&
      (file !== basename) && 
      (file.slice(-3) === '.js');
  })
  .forEach(file => {
    const model = db[`${databases[1]}`].import(path.join(__dirname + `/${databases[1]}`, file));
    db[model.name] = model;
 });

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

module.exports = db;

์ผ๋‹จ ์ง€๊ธˆ๋ถ€ํ„ฐ๋Š” process.env๋ฅผ ์ด์šฉํ•˜์—ฌ ํ˜„์žฌ ํ™˜๊ฒฝ์ด ๊ฐœ๋ฐœํ™˜๊ฒฝ์ธ์ง€ ์ƒ์šฉํ™˜๊ฒฝ์ธ์ง€ ๊ตฌ๋ถ„ํ•˜๋Š” ๋กœ์ง์ด ์ถ”๊ฐ€๋ฉ๋‹ˆ๋‹ค. ์ €๋Š” ์ถ”๊ฐ€์ ์œผ๋กœ localhost ํ™˜๊ฒฝ๊นŒ์ง€ ๊ตฌ๋ถ„ํ•ด์•ผํ•˜๋Š” ๋ถ€๋ถ„์ด ํ•„์š”ํ–ˆ์–ด์„œ BY_LOCAL์ด๋ผ๋Š” ํ™˜๊ฒฝ๋ณ€์ˆ˜๋ฅผ ๊ฐœ์ธ์ ์œผ๋กœ ์ถ”๊ฐ€ํ•ด์„œ ๋ถ„๊ธฐ์ฒ˜๋ฆฌํ–ˆ์Šต๋‹ˆ๋‹ค๋งŒ, ๋ณดํ†ต์€ development์™€ product๋งŒ์„ ๊ตฌ๋ถ„ํ•˜์—ฌ ์‚ฌ์šฉํ•˜๊ณค ํ•ฉ๋‹ˆ๋‹ค.

 

๊ธฐ์กด์— ์ฝ”๋“œ์—์„œ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋Š” ๊ตฌ๋ฌธ๋งŒ ์ถ”๊ฐ€๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

sequelize.json ํŒŒ์ผ์€ ์•„๋ž˜์™€ ๊ฐ™์€ ํ˜•์‹์œผ๋กœ ์ž‘์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค.

{
	"development": {
		"logging": true,
		"databases": {
			"SEASON3_DEV": 
			{
				"acquireTimeout": 5000,
				"connectionLimit" : 30,
				"timezone": "+09:00",
				"charset": "utf8mb4",
				"database": "SEASON3_DEV",
				"username": "์œ ์ €๋„ค์ž„",
				"password": "๋น„๋ฐ€๋ฒˆํ˜ธ",
				"host": "localhost",
				"port": "ํฌํŠธ๋ฒˆํ˜ธ",
				"dialect": "mysql",
				"define":{
					"timestamps" : false,
					"underscored" : false
				}
			},
			"STORE_DEV": 
			{
				"acquireTimeout": 5000,
				"connectionLimit" : 30,
				"timezone": "+09:00",
				"charset": "utf8mb4",
				"database": "STORE_DEV",
				"username": "์œ ์ €๋„ค์ž„",
				"password": "๋น„๋ฐ€๋ฒˆํ˜ธ",
				"host": "localhost",
				"port": "ํฌํŠธ๋ฒˆํ˜ธ",
				"dialect": "mysql",
				"define":{
					"timestamps" : false,
					"underscored" : false
				}
			}
		}
	},

	"production": {
		// ์œ„์™€ ๊ฐ™์€ ๊ตฌ์กฐ์ž…๋‹ˆ๋‹ค.
	}
}

Object.keys() ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋ ค๋Š” DB config ์ •๋ณด๋ฅผ ๋ฝ‘์•„์˜ค๋Š” ๊ฒƒ๋ฐ–์— ์—†์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  exports๋˜๋Š” db ๋ชจ๋“ˆ์€ ๊ฐ๊ฐ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋งˆ๋‹ค ํ•ด๋‹น๋˜๋Š” ๋””๋ ‰ํ† ๋ฆฌ์˜ entity ํŒŒ์ผ์„ ์ฐธ์กฐํ•˜๊ฒŒ๋” ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค.

 

๊ทธ๋Ÿผ sequelize-auto ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•˜์—ฌ entity๋ฅผ ๋งŒ๋“œ๋Š” js ํŒŒ์ผ๋„ ์ˆ˜์ •์ด ํ•„์š”ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด ๋ถ€๋ถ„์€ ์ œ๊ฐ€ ์ƒํ™ฉ์ด ์ข€ ๊ธ‰ํ–ˆ์–ด์„œ ๋‹น์žฅ์€ ์ข€ ๋ถˆํŽธํ•˜๊ฒŒ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค๋งŒ, ์ถ”ํ›„์— ์ˆ˜์ •ํ•˜๋ ค๊ณ ํ•ฉ๋‹ˆ๋‹ค.

 

const async = require('async');
const sequelizeAuto = require('sequelize-auto');
const model = require('../src/backend/model/entity');
const env = process.env.NODE_ENV;
let config = {};
if (process.env.BY_LOCAL) {
    config = require('../config/sequelize.local.json')[env];
} else {
    config = require('../config/sequelize.json')[env];
}

let db = [];
/* Add Databases */
Object.keys(config.databases).map(v=>{
    db.push({
        DATABASE: config.databases[v].database,
        USERNAME: config.databases[v].username,
        PASSWORD: config.databases[v].password,
        HOST: config.databases[v].host
    })
})

const tasks = [
    //ํ…Œ์ด๋ธ” ๋ชฉ๋ก ๊ฐ€์ ธ์˜ค๊ธฐ (DB ๋ถ„๊ธฐ ์ ‘์†์ฒ˜๋ฆฌ ๋˜์–ด์žˆ์Œ.)
    function(callback) {
        model[db[process.argv[2]].DATABASE].query('show tables')
        .then((rows)=>{
            callback(null, rows[0])
        })
        .catch((err)=>{
            callback(err);
        })
    },
    //ํ…Œ์ด๋ธ” ๋ชฉ๋ก ๋ฐฐ์—ด์— ์ €์žฅ
    function(rows,callback) {
        let tables = [];
        rows.map(v=>tables.push(v[`Tables_in_${db[process.argv[2]].DATABASE}`]) );
        callback(null, tables);
    },
    //sequelize-auto ๋ชจ๋“ˆ๋กœ ํ•ด๋‹น DB์˜ ๋ชจ๋“  ํ…Œ์ด๋ธ” entity ํŒŒ์ผ ์ƒ์„ฑ
    function(tables,callback) {
        const auto = new sequelizeAuto(db[process.argv[2]].DATABASE,db[process.argv[2]].USERNAME,db[process.argv[2]].PASSWORD,{
            host: db[process.argv[2]].HOST,
            tables: tables,
            directory: `./src/backend/model/entity/${db[process.argv[2]].DATABASE}`
        })
        auto.run((err)=>{
            if(err) throw err;
        })
        callback(null);
    },
];

async.waterfall(tasks, (err)=>{
    if(err)
        console.log(err);
    else
        console.log('done');
})

ํ•ด๋‹น ํŒŒ์ผ์„ ์‹คํ–‰ํ–ˆ์„ ๋•Œ ํ•˜๋‚˜์˜ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ๋Œ€ํ•œ entity๋งŒ ์ƒ์„ฑ์ด ๊ฐ€๋Šฅํ•˜๊ฒŒ ๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๊ฒƒ๋„ ์‹คํ–‰ argument๋ฅผ ์ง€๊ธˆ ๊ตฌ์กฐ์—์„œ๋Š” ์ˆซ์ž๋กœ ์ค˜์„œ ๋ง์ด์ฃ . ๋‚˜์ค‘์—๋Š” DB๋ช…์œผ๋กœ ์‹คํ–‰ argument๋ฅผ ์ค˜์„œ entity๋ฅผ ์ƒ์„ฑํ•˜๊ฒŒ๋” ๋ณ€๊ฒฝํ•  ์ƒ๊ฐ์ž…๋‹ˆ๋‹ค. ์ฝ”๋“œ๋Š” ๊ต‰์žฅํžˆ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค.

 

 

  1. entity๋ฅผ ๋งŒ๋“œ๋ ค๋Š” DATABASE ์˜ config ์ •๋ณด๋ฅผ db ๋ฐฐ์—ด ๋ณ€์ˆ˜์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.
  2. ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์กด์žฌํ•˜๋Š” ๋ชจ๋“  ํ…Œ์ด๋ธ”๋ช…์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.
  3. ํ•ด๋‹น ํ…Œ์ด๋ธ”์„ sequelize-auto ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•˜์—ฌ ํ•ด๋‹น๋˜๋Š” ๋””๋ ‰ํ† ๋ฆฌ์— js ํŒŒ์ผ ํ˜•ํƒœ์˜ entity๋กœ ๋ชจ๋‘ ๋ณ€ํ™˜ํ•˜์—ฌ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

 

์„ค์ •์˜ ๋Œ€ํ•œ ์ •๋ณด๋Š” ์—ฌ๊ธฐ๊นŒ์ง€๊ณ  ๊ธฐ์กด์ฒ˜๋Ÿผ

const model = require('./src/backend/model/entity');

์œ„์™€๊ฐ™์ด entity ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ requireํ•˜๋ฉด ํ•ด๋‹น ๋””๋ ‰ํ† ๋ฆฌ์˜ index.js ํŒŒ์ผ์„ ์ฐธ์กฐํ•˜๊ฒŒ ๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿผ index.js ์—์„œ ์šฐ๋ฆฌ๊ฐ€ ๋ฐ˜ํ™˜ํ•˜๊ฒŒ ๋งŒ๋“  db ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐฉ์‹์€ ๊ธฐ์กด๊ณผ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ๋‹จ, ์‚ฌ์šฉ ์‹œ์— ์–ด๋Š ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์‚ฌ์šฉํ• ๊ฑด์ง€ ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ทธ๋•Œ๊ทธ๋•Œ ์ž‘์„ฑํ•ด์ค˜์•ผํ•ฉ๋‹ˆ๋‹ค.

 

 

 

 

๋Œ“๊ธ€
๊ณต์ง€์‚ฌํ•ญ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€
Total
Today
Yesterday
๋งํฌ
ยซ   2024/11   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
๊ธ€ ๋ณด๊ด€ํ•จ