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




์˜ค๋žœ๋งŒ์— ๋‹ค์‹œ ๊ธ€์„ ์“ฐ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. Sequelize๋ฅผ ์“ธ ์ผ์ด ๊ฐ‘์ž๊ธฐ ์ƒ๊ธฐ๊ฒŒ ๋˜์—ˆ๋„ค์š”~ 

์ด์ „๊นŒ์ง€๋Š” sequelize๋ฅผ ์ด์šฉํ•ด define ๋œ ํ…Œ์ด๋ธ” ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ์ง„ํ–‰์„ ํ–ˆ์—ˆ๋Š”๋ฐ, ๋งŒ์•ฝ Models ๋””๋ ‰ํ† ๋ฆฌ์— ์ œ๊ฐ€ ์ •์˜ํ•˜์ง€ ์•Š์€ ํ…Œ์ด๋ธ”์ด ์žˆ๋‹ค๋ฉด ๊ทธ ํ…Œ์ด๋ธ”์„ sequelize๋ฅผ ํ†ตํ•ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์„๊นŒ์š”?


ํ”ํžˆ ๋งํ•˜๋Š” DML๊ตฌ๋ฌธ์€ sequelize ์—์„œ๋Š” Model ๊ฐ์ฒด์—์„œ๋งŒ ์žˆ๋Š” ๋ฉ”์†Œ๋“œ ์ž…๋‹ˆ๋‹ค. ์ด ๋ง์€ ์ฆ‰, models ๋””๋ ‰ํ† ๋ฆฌ์— ํ…Œ์ด๋ธ” ์ •๋ณด ํŒŒ์ผ์ด ์žˆ์„ ๋•Œ findOne์ด๋‚˜ update ๋“ฑ๊ณผ ๊ฐ™์€ ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


๊ทผ๋ฐ ๋‚ด๋ถ€ DB์—๋Š” ์žˆ๋Š”๋ฐ ์ œ models ๋””๋ ‰ํ† ๋ฆฌ์— ๊ทธ ํ…Œ์ด๋ธ”์— ๋Œ€ํ•œ js ํŒŒ์ผ์ด ์—†๋‹ค๋ฉด ๊ฒฐ๊ตญ ๊ทธ ํ…Œ์ด๋ธ”์€ ์กฐ์ž‘์ด ๋ถˆ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.



์˜ˆ๋ฅผ๋“ค๋ฉด models  ๋””๋ ‰ํ† ๋ฆฌ์•ˆ์— ์ด ๋‘๊ฐœ์˜ ํŒŒ์ผ์ด ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. index.js ํŒŒ์ผ์€ ํ…Œ์ด๋ธ”์— ๋Œ€ํ•œ ํŒŒ์ผ์ด ์•„๋‹ˆ๋‹ˆ๊นŒ ์ œ์ณ๋‘๊ณ , ์ด๋ ‡๊ฒŒ ๋”ฐ์ง€๋ฉด sequelize ๋ชจ๋“ˆ์„ ํ†ตํ•ด ์ œ์–ดํ•  ์ˆ˜ ์žˆ๋Š” ํ…Œ์ด๋ธ”์€ mytable์ด๋ผ๋Š” ํ•œ ๊ฐœ ๋ฟ์ž…๋‹ˆ๋‹ค.


๋งŒ์•ฝ ์ œ DB์— user๋ผ๋Š” ํ…Œ์ด๋ธ”์ด ์‹ค์ œ ์žˆ์—ˆ๋‹ค๊ณ  ํ•ด๋„, ์ œ models ๋””๋ ‰ํ† ๋ฆฌ์•ˆ์— ๊ทธ DB์— ๋Œ€ํ•œ ํŒŒ์ผ์ด ์—†๋‹ค๋ฉด ์ œ์–ด๊ฐ€ ๋ถˆ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์‹ค ๊ธฐ์ค€์ด ๋˜๋Š”๊ฑด ์ง„์งœ ๋ฌผ๋ฆฌ์ ์ธ ๋กœ์ปฌ DB์ธ๋ฐ sequelize ๋ฌธ๋ฒ• ๋”ฐ๋กœ, SQL๋ฌธ ๋”ฐ๋กœ ํ…Œ์ด๋ธ”์„ ์ •์˜ํ•˜๋Š”๊ฒŒ ๊ต‰์žฅํžˆ ๋น„ํšจ์œจ์ ์ด์ฃ .


๊ทธ๋ž˜์„œ ๋งจ ์œ„์˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ๋กœ์ปฌ DB์— ์žˆ๋Š” ์ •๋ณด๋ฅผ Sequelize ๋ฌธ๋ฒ•์œผ๋กœ ์ž๋™ ๋ณ€ํ™˜ํ•ด์ฃผ์–ด์„œ ํ…Œ์ด๋ธ”์„ ์ •์˜ํ•ด์ฃผ๋Š” ๋ชจ๋“ˆ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ฐธ ๋‹คํ–‰์ด์ฃ ~


$ npm install sequelize-auto 
์ด ๋ช…๋ น์–ด๋ฅด ํ†ตํ•ด ํ•ด๋‹น ๋ชจ๋“ˆ์„ ์„ค์น˜ํ•ด์ค๋‹ˆ๋‹ค.



๋งŒ์•ฝ ์ œ๊ฐ€ ์ด์™€๊ฐ™์€ ํ…Œ์ด๋ธ”์„ ํ•˜๋‚˜ ๊ฐ€์ง€๊ณ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. 

์ด ํ…Œ์ด๋ธ”์„ sequelize ๋ชจ๋“ˆ์„ ์ด์šฉํ•ด์„œ ์‚ฌ์šฉํ•˜๋ ค๋ฉด Model ๊ฐ์ฒด๋ฅผ ์–ป์–ด์•ผํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰ ์ด ํ…Œ์ด๋ธ”์„ sequelize ๋ฌธ๋ฒ•์œผ๋กœ defineํ•œ js ํŒŒ์ผ์ด ํ•„์š”ํ•œ ๊ฒƒ์ด์ฃ . auto ๋ชจ๋“ˆ์€ ์ด๊ฑธ ํ•œ๋ฒˆ์— ํ•ด๊ฒฐํ•ด์ค๋‹ˆ๋‹ค.



//app.js
const Sequelize = require('sequelize');
const SequelizeAuto = require('sequelize-auto');
const auto = new SequelizeAuto('test','root','123',{
  host:'localhost',
  port:'3306'
});
auto.run((err)=>{
  if(err) throw err;
})

๊ต‰์žฅํžˆ ์งง์ง€๋งŒ ํ•ต์‹ฌ์€ run ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค. ์ € ๋ฉ”์„œ๋“œ๊ฐ€ ์ •์ƒ์‹คํ–‰์‹œ์— models ๋””๋ ‰ํ† ๋ฆฌ ์•ˆ์— mytables.js ํŒŒ์ผ์ด ์ž๋™ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ํŒŒ์ผ์˜ ๋‚ด์šฉ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

/* jshint indent: 1 */

module.exports = function(sequelize, DataTypes) {
	return sequelize.define('mytable', {
		id: {
			type: DataTypes.INTEGER(11),
			allowNull: false,
			primaryKey: true,
			autoIncrement: true
		},
		name: {
			type: DataTypes.STRING(10),
			allowNull: false
		}
	}, {
		tableName: 'mytable'
	});
};

์ด๋ ‡๊ฒŒ ๋งŒ๋“ค์–ด์ง€๋ฉด ๊ธฐ์กด์— ํ–ˆ๋˜ ๊ฒƒ๊ณผ ๊ฐ™์ด models.mytable.findOne ๊ณผ ๊ฐ™์ด ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜๊ฒ ์ฃ . ๋ชจ๋“  ํ…Œ์ด๋ธ”์ด ์ •ํ™•ํ•œ ๊ตฌ๋ฌธ์œผ๋กœ ๋งŒ๋“ค์–ด ์ง€๋Š” ์ง€๋Š” ์•„์ง ์™„๋ฒฝํ•˜๊ฒŒ ํ…Œ์ŠคํŠธํ•ด๋ณด์ง„ ๋ชปํ–ˆ์ง€๋งŒ, ์ œ๊ฐ€ ๊ตณ์ด ํ…Œ์ด๋ธ” ์ •์˜ ํŒŒ์ผ์„ ๋งŒ๋“ค์ง€ ์•Š์•„๋„ ๋œ๋‹ค๋Š” ๊ฒƒ์ด ๋งค์šฐ ํŽธ๋ฆฌํ•œ ๋ชจ๋“ˆ์ž…๋‹ˆ๋‹ค.

๋Œ“๊ธ€
๊ณต์ง€์‚ฌํ•ญ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€
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
๊ธ€ ๋ณด๊ด€ํ•จ