Node.js/Sequelize

[ORM] ๐Ÿ“š sequelize-auto ๋ชจ๋“ˆ ์‚ฌ์šฉํ•˜๊ธฐ

์ธํŒŒ_ 2021. 11. 28. 14:02

sequelize-auto

sequelize-auto ๋ชจ๋“ˆ

 

[sequelize-cli]

  • ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๊ฐ€ ๊ตฌ์ถ•๋˜์ง€์•Š๋”๋ผ๋„, ํ”„๋กœ์ ํŠธ๋งŒ ๋ฐ›์•„์„œ ์„ค์ •ํ•˜๊ณ  ์„œ๋ฒ„์‹คํ–‰๋งŒ ํ•˜๋ฉด ์•Œ์•„์„œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ํ…Œ์ด๋ธ” ์ƒ์„ฑํ•ด์ฃผ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ด๋‹ค.
  • ๋ˆ„๊ตฐ๊ฐ€์˜ ํ”„๋กœ์ ํŠธ๋ฅผ ๋ฐ›์•„์„œ ๋น ๋ฅด๊ฒŒ wasํ™˜๊ฒฝ์„ ๋Œ๋ฆด์ƒํ™ฉ์ด๋ฉด ์ข‹์€ ์„ ํƒ์ง€๋‹ค.
  • ๋ชจ๋ธ ์ฝ”๋“œ ์ž‘์„ฑ → ์ž๋™ create๋ฌธ ๋ณ€ํ™˜ → RDB ํ…Œ์ด๋ธ” ์ƒ์„ฑ

 

[sequelize-auto]

  • ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์ด๋ฏธ ๊ตฌ์ถ•ํ•œ ์ƒํƒœ๋ผ๋ฉด, ์“ธ๋ชจ์žˆ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋‹ค.
  • ์ด๋ฏธ ๊ตฌ์ถ•๋œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์Šคํ‚ค๋งˆ๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ orm ๋ชจ๋ธ์„ ์ž๋™์œผ๋กœ ์ƒ์„ฑํ•ด์ค€๋‹ค.
  • ํž˜๋“ค๊ฒŒ sql์งœ๊ณ  orm์ฝ”๋”ฉ ํ•˜๋Š” ๋‘๋ฒˆ์˜ ๋ฒˆ๊ฑฐ๋กœ์›€์„ ์—†์• ์ค€๋‹ค.
  • ์ด๋ฏธ ์งœ์—ฌ์ง„ ํ…Œ์ด๋ธ”์„ ๊ธฐ๋ฐ˜์œผ๋กœ → ์ž๋™์œผ๋กœ ๋ชจ๋ธ ์ฝ”๋“œ ์ƒ์„ฑ

sequelize-auto ์„ค์น˜

Sequelize-auto๋Š” Sequelize cli์™€ ๋ฐ˜๋Œ€๋กœ, model์„ ๋จผ์ € ์ž‘์„ฑํ•˜์ง€ ์•Š๊ณ , ๋””๋น„๋ฅผ ๋จผ์ € ์ž‘์„ฑํ•œ ํ›„์— ์‹คํ–‰ํ•ด์ฃผ๋ฉด,

๋””๋น„์ •๋ณด๊ฐ€ ๋ชจ๋ธ์— ์—ญ์œผ๋กœ ์ €์žฅ๋œ๋‹ค.

 

ํ•˜์ง€๋งŒ sequelize-auto๋กœ ์—ฐ๋™ํ•  ๊ฒฝ์šฐ created_at, update_at, defaultValue๋ฅผ ์ง์ ‘ ์„ค์ •ํ•ด์ค˜์•ผ ํ•œ๋‹ค. ํ•˜์ง€๋งŒ workbentch์—์„œ ํ…Œ์ด๋ธ” ๊ฐ„์˜ ๊ด€๊ณ„๋ฅผ ์ž˜ ๋งŒ๋“ค์–ด๋†“์•˜์œผ๋ฉด auto๊ฐ€ ๊ด€๊ณ„๊นŒ์ง€ ์ž˜ ๊ฐ€์ ธ์™€ ํŽธ์•ˆํ•˜๊ฒŒ ์ฝ”๋”ฉ ํ•  ์ˆ˜ ์žˆ๋‹ค.

$ npm i sequelize-auto
 

GitHub - sequelize/sequelize-auto: Automatically generate bare sequelize models from your database.

Automatically generate bare sequelize models from your database. - GitHub - sequelize/sequelize-auto: Automatically generate bare sequelize models from your database.

github.com


sequelize-auto ์‚ฌ์šฉ

๋งŒ์ผ MySQL์— ์ด๋ฏธ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ…Œ์ด๋ธ”์ด ์ƒ์„ฑ๋˜์–ด์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜์ž.

์›๋ž˜๋Œ€๋กœ๋ผ๋ฉด ์ด ํ…Œ์ด๋ธ” ์ •๋ณด๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๋ชจ๋ธ ์ฝ”๋“œ๋ฅผ ๊ทธ๋Œ€๋กœ ์งœ์•ผ๋˜์ง€๋งŒ auto ๋ชจ๋“ˆ์„ ํ†ตํ•ด ์ž๋™์œผ๋กœ ๋ชจ๋ธ์„ ์ƒ์„ฑํ•˜๊ณ  Node์™€ MySQL์„ ์—ฐ๊ฒฐํ•˜๋Š” ์ฝ”๋“œ๋„ ์ƒ์„ฑํ•  ๊ฒƒ์ด๋‹ค.

sequelize-auto
sequelize-auto
sequelize-auto

 

 

๋ชจ๋ธ ์ž๋™์ƒ์„ฑ ํด๋ž˜์Šค ์ž‘์„ฑ

์šฐ์„  orm.js๋ผ๋Š” ํŒŒ์ผ์„ ๋งŒ๋“ค์–ด์ฃผ๊ณ  ์•ˆ์—๋‹ค๊ฐ€ sequelizeAuto ์ƒ์„ฑ ํด๋ž˜์Šค ๊ตฌ๋ฌธ์„ ๊ธฐ์žฌํ•˜์ž.

ํด๋ž˜์Šค ์ธ์ž๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ตฌ์„ฑ๋˜์–ด์žˆ๋‹ค.

์ฒ˜์Œ์—๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ช…, ์œ ์ €, ๋น„๋ฒˆ์„ ์“ฐ๊ณ  ๊ทธ๋‹ค์Œ ์ •๋ณด๋ฅผ ๋‹ด์€ ๊ฐ์ฒด๋ฅผ ์ ์–ด์ฃผ๋ฉด ๋œ๋‹ค.

sequelize-auto

 

orm.js

const SequelizeAuto = require('sequelize-auto');
const auto = new SequelizeAuto("nodejs", "root", "123123", {
      host: "127.0.0.1",
      port: "3306",
      dialect: "mysql",
      //noAlias: true // as ๋ณ„์นญ ๋ฏธ์„ค์ • ์—ฌ๋ถ€
   }
);
auto.run((err)=>{
   if(err) throw err;
})

 

๊ทธ๋ฆฌ๊ณ  orm.js๋ฅผ ๋…ธ๋“œ๋กœ ์‹คํ–‰ํ•˜๋ฉด ๋‹ค์Œ ๋กœ๊ทธ์™€ ํ•จ๊ป˜ models ๋””๋ ‰ํ† ๋ฆฌ์— ๋ชจ๋ธ์ด ์ž๋™ ์ƒ์„ฑ๋œ๋‹ค.

$ node orm

sequelize-auto
sequelize-auto
sequelize-auto

mysql์˜ commentsํ…Œ์ด๋ธ”๊ณผ usersํ…Œ์ด๋ธ”์ด ๊ฐ๊ฐ comments.js์™€ users.js๋ชจ๋ธ๋กœ ๋ณ€ํ™˜๋ ๊ฑธ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋‹ค.

init-models.js๋Š” ๊ฐ๊ฐ ์ƒ์„ฑ๋œ ๋ชจ๋ธ๋“ค์˜ ๊ด€๊ณ„๋ฅผ ์„ค์ •ํ•˜๊ณ  ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์ž‡๋Š” ์—ญํ• ์„ ํ•œ๋‹ค๊ณ  ๋ณด๋ฉด๋œ๋‹ค. ์ด ์—ญ์‹œ ์ž๋™ ์ƒ์„ฑ ๋œ๋‹ค.

 

init-models.js

var DataTypes = require("sequelize").DataTypes;
var _comments = require("./comments");
var _users = require("./users");

function initModels(sequelize) {
  var comments = _comments(sequelize, DataTypes);
  var users = _users(sequelize, DataTypes);

  // ๊ด€๊ณ„๋ฅผ ์ •์˜ํ•˜๋Š”๊ฑธ ์•Œ์ˆ˜ ์žˆ๋‹ค.
  comments.belongsTo(users, { as: "commenter_user", foreignKey: "commenter"});
  users.hasMany(comments, { as: "comments", foreignKey: "commenter"});

  return {
    comments,
    users,
  };
}
module.exports = initModels;
module.exports.initModels = initModels;
module.exports.default = initModels;

mysql ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ

index.js์—์„œ sequelizeAuto๋กœ ๋งŒ๋“  ๋ชจ๋ธ๊ณผ mysql ํ…Œ์ด๋ธ”์„ ์„œ๋กœ ์—ฐ๊ฒฐ ์‹œ์ผœ์ฃผ๊ณ  ๋ฆฌํ„ดํ•œ models๊ฐ์ฒด๋ฅผ exportํ•œ๋‹ค.

๊ทธ๋ฆฌ๊ณ  app.js์—์„œ ๊ฐ€์ ธ์™€ ๋ฐ”๋กœ ์ฟผ๋ฆฌ๋ฌธ์„ ๋‚ ๋ฆฌ๋ฉด ์ž‘๋™๋˜๋Š”๊ฑธ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

index.js

const initModels = require('./init-models'); // init-models.js์—์„œ ๋ฉ”์„œ๋“œ๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค.
const { Sequelize } = require('sequelize');

// config/config.json ํŒŒ์ผ์— ์žˆ๋Š” ์„ค์ •๊ฐ’๋“ค์„ ๋ถˆ๋Ÿฌ์˜จ๋‹ค.
// config๊ฐ์ฒด์˜ env๋ณ€์ˆ˜(development)ํ‚ค ์˜ ๊ฐ์ฒด๊ฐ’๋“ค์„ ๋ถˆ๋Ÿฌ์˜จ๋‹ค.
// ์ฆ‰, ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์„ค์ •์„ ๋ถˆ๋Ÿฌ์˜จ๋‹ค๊ณ  ๋งํ•  ์ˆ˜ ์žˆ๋‹ค.
const env = process.env.NODE_ENV || 'development';
const config = require("../config/config.json")[env]

// new Sequelize๋ฅผ ํ†ตํ•ด MySQL ์—ฐ๊ฒฐ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.
const sequelize = new Sequelize(config.database, config.username, config.password, config)

// ๋ชจ๋ธ๊ณผ ํ…Œ์ด๋ธ”๊ฐ„์˜ ๊ด€๊ณ„๊ฐ€ ๋งบ์–ด์ง„๋‹ค.
const models = initModels(sequelize);

module.exports = models;

 

app.js

const express = require('express');
const { users, comments } = require('./models/index'); // ./models/index.js์—์„œ ์„ค์ •ํ•œ ์—ฐ๊ฒฐ๋œ ๋ชจ๋ธ๋“ค์„ ๊ฐ€์ ธ์˜จ๋‹ค

const app = express();

app.set('port', process.env.PORT || 3000);

app.get('*', async (req, res, next) => {
   try {
      const _comments = await comments.findAll({ // ๋ฐ”๋กœ ์ฟผ๋ฆฌ๋ฌธ ์‚ฌ์šฉ
         include: {
            model: users,
            as: "commenter_user", // ์œ„ init-models.js์—์„œ ์ •์˜ํ•œ alias๋ฅผ ๋ฐ˜๋“œ์‹œ ๊ธฐ์žฌ
            where: { id: 2 },
         },
      });
      console.log(_comments);
      res.json(_comments);
   } catch (err) {
      console.error(err);
      next(err);
   }
});

// ์„œ๋ฒ„ ์‹คํ–‰
app.listen(app.get('port'), () => {
   console.log(app.get('port'), '๋ฒˆ ํฌํŠธ์—์„œ ๋Œ€๊ธฐ ์ค‘');
});
Executing (default): 
SELECT `comments`.`id`, `comments`.`commenter`, `comments`.`comment`, `comments`.`created_at`, 
`commenter_user`.`id` AS `commenter_user.id`, `commenter_user`.`name` AS `comme` AS `commenter_user.age`, 
`commenter_user`.`married` AS `commenter_user.married`, `commenter_user`.`comment` AS `commenter_user.comment`, 
`commenter_user`.`created_at` AS `commenter_user.creatNNER JOIN `users` AS `commenter_user` 
ON `comments`.`commenter` = `commenter_user`.`id` AND `commenter_user`.`id` = '1';