7. MySQL

mac์€ brew๋กœ ์„ค์น˜. mysqlworkbench๋กœ ์—ฐ๊ฒฐํ•ด์„œ ํ™•์ธ.

7.6. Sequelize ์‚ฌ์šฉ

ย 
npx sequelize init
โ†’ config, models, migrations, seeders ํด๋”๊ฐ€ ์ƒ์„ฑ
ย 
connection
const db = {}; const sequelize = new Sequelize(config.database, config.username, config.password, config); db.seqeulize = sequelize; module.exports = db;
ย 
์ดํ›„ sequelize.sync({ force: false })๋กœ ์—ฐ๊ฒฐ.
ย 
config/config.json ํŒŒ์ผ์— ์—ฐ๊ฒฐ ๋น„๋ฐ€๋ฒˆํ˜ธ ์ž…๋ ฅ.
ย 

๋ชจ๋ธ ์ •์˜ํ•˜๊ธฐ

MySQL์—์„œ ์ •์˜ํ•˜ ํ…Œ์ด๋ธ”์„ ์‹œํ€„๋ผ์ด์ฆˆ์—์„œ๋„ ์ •์˜ํ•ด์•„ํ•จ.
ย 
๊ฐ ์—ด์— ๋Œ€ํ•œ ๋ฐ์ดํ„ฐํƒ€์ž…์„ ์ •์˜ํ•˜๊ณ  allowNull, unique, defaultValue ๋“ฑ ์„ค์ •ํ•ด๋†“์„์ˆ˜ ์žˆ๋‹ค.
ย 
initiate์—์„œ ๋‘๋ฒˆ์งธ ์ธ์ž๋กœ๋Š” ํ…Œ์ด๋ธ” ์˜ต์…˜์„ ๋„ฃ๋Š”๋‹ค. (ํ…Œ์ด๋ธ”๋ช… ๋ฌธ์ž ์ •์˜ ๋“ฑ๋“ฑ)
ย 
associate๋ฅผ ํ†ตํ•ด์„œ๋Š” ํ…Œ์ด๋ธ”๊ฐ„์˜ ๊ด€๊ณ„๋ฅผ ์„ค์ •ํ•จ.
ย 
์ผ๋Œ€๋‹ค์ธ ๊ฒฝ์šฐ .hasMany ์ผ๋Œ€์ผ์€ hasOne ๋ฉ”์จ๋“œ ๋‹ค๋Œ€๋‹ค๋Š” belongsToMany
ย 
sequelize๋ฅผ ํ†ตํ•ด์„œ๋Š” SQL์—์„œ select, where, order ํ•˜๋“ฏ ๊ฐ์ฒด๋กœ ์ด๊ฒƒ์„ ์ •์˜ํ•ด์„œ ์กฐํšŒ, ์ˆ˜์ •, ์ƒ์„ฑ, ์‚ญ์ œ๋ฅผ ํ•œ๋‹ค.
ย 
์ƒ์œ„ ํ…Œ์ด๋ธ”์—์„œ ์—ฎ์ธ ํ…Œ์ด๋ธ”์˜ ๊ฐ’๋„ ๊ฐ™์ด ๊ฐ€์ ธ์˜ค๋ ค๋ฉด include์— ๊ทธ ๋ชจ๋ธ์„ ํฌํ•จ์‹œ์ผœ์•ผํ•œ๋‹ค.
ย 
CRUD๋Š” router์—์„œ :id ๋“ฑ์œผ๋กœ path parameter๋กœ ๋ฐ›๊ณ  ๊ทธ id๋ฅผ where ์กฐ๊ฑด์— ๋„ฃ์–ด์„œ ์กฐํšŒ๋‚˜ ์‚ญ์ œ๋ฅผ ํ•˜๋ฉด ๊ฐ„๋‹จํ•˜๋‹ค.