Shortly MVC pattern(1)

1. Sequelize ์„ค์น˜

https://sequelize.org/

npm install --save sequelize

2. Sequelize CLI ์„ค์น˜

๋งˆ์ด๊ทธ๋ ˆ์ด์…˜์„ ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋•๋Š” ํˆด์ด๊ณ  CLI ์—์„œ ๋ชจ๋ธ์„ ์ƒ์„ฑํ•ด์ฃผ๊ฑฐ๋‚˜, ์Šคํ‚ค๋งˆ ์ ์šฉ์„ ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋•๋Š”๋‹ค๊ณ  ํ•œ๋‹ค.

npm install --save-dev sequelize-cli

3. ํ”„๋กœ์ ํŠธ ์ดˆ๊ธฐ ๋‹จ๊ณ„๋ฅผ ์ž๋™์œผ๋กœ ์„ค์ •ํ•˜๋Š” bootstraping

npx sequelize-cli init

์ด ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ์•„๋ž˜์™€ ๊ฐ™์€ ํด๋”๋“ค์ด ์ƒ์„ฑ๋œ๋‹ค.

  • config, contains config file, which tells CLI how to connect with database
  • models, contains all models for your project
  • migrations, contains all migration files
  • seeders, contains all seed files

https://sequelize.org/master/manual/migrations.html

4. MySQL ์ ‘์† ์„ค์ •ํ•˜๊ธฐ

config/config.json ์„ ํ™•์ธํ•ด๋ณด๋ฉด ์•„๋ž˜ ์ฝ”๋“œ์™€ ๊ฐ™๋‹ค.

๊ทธ์ค‘ development ์˜ password ์™€ database ๋ฅผ ์ˆ˜์ •ํ•ด ์ฃผ์—ˆ๋‹ค.

database ๋Š” ์Šคํ”„๋ฆฐํŠธ์— ์“ฐ์ผ ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค๋ฅผ ์ƒ์„ฑํ•ด ์ฃผ๋ฉด ๋œ๋‹ค.

{
  "development": {
    "username": "root",
    "password": "****",
    "database": "prac_mvc",
    "host": "127.0.0.1",
    "dialect": "mysql"
  },
  "test": {
    "username": "root",
    "password": null,
    "database": "database_test",
    "host": "127.0.0.1",
    "dialect": "mysql"
  },
  "production": {
    "username": "root",
    "password": null,
    "database": "database_production",
    "host": "127.0.0.1",
    "dialect": "mysql"
  }
}

๊ฐœ๋ฐœ ํ™˜๊ฒฝ, ํ…Œ์ŠคํŠธ ํ™˜๊ฒฝ, ๊ฐ๊ฐ์˜ ํ™˜๊ฒฝ์— ๋”ฐ๋ผ ๋‹ค๋ฅธ host ์™€ ํŒจ์Šค์›Œ๋“œ ๋“ฑ์„ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํ™˜๊ฒฝ์„ ์„ธ๊ฐ€์ง€๋กœ ๋‚˜๋ˆˆ๋‹ค๊ณ  ํ•œ๋‹ค.

models/index.js ์—์„œ๋งŒ ๋ณด๋”๋ผ๋„

const env = process.env.NODE_ENV || 'development'

โ€˜developmentโ€™ ๊ฐ€ ๊ธฐ๋ณธ ํ™˜๊ฒฝ์œผ๋กœ ์„ค์ •๋˜์–ด ์žˆ์Œ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

5. ๋ชจ๋ธ ์ƒ์„ฑํ•˜๊ธฐ

npx sequelize-cli model:generate --name url --attributes url:string,title:string,visits:integer

๋ชจ๋ธ (ํ…Œ์ด๋ธ”์˜ ์ด๋ฆ„) ์ด๋ฆ„์€ url ๋กœ ์ง€์—ˆ๋Š”๋ฐ ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์ž๋™์ ์œผ๋กœ s ๋ฅผ ๋ถ™์—ฌ ๋ณต์ˆ˜ํ˜•์œผ๋กœ ๋ฐ”๊ฟ” ์ค€๋‹ค.

๊ทธ๋ฆฌ๊ณ  attributes ์— ๋“ค์–ด๊ฐ€์•ผ ํ•  ์นผ๋Ÿผ์ธ url, title, visits ์™€ ํ•ด๋‹น ์ปฌ๋Ÿผ์˜ ํƒ€์ž…์„ ์ ์–ด์ฃผ๊ณ  ์ƒ์„ฑํ•œ๋‹ค.

6. ์ปฌ๋Ÿผ์˜ ๊ธฐ๋ณธ๊ฐ’ ์„ค์ •ํ•˜๊ธฐ

visits ์˜ ๊ธฐ๋ณธ๊ฐ’์„ 0์œผ๋กœ ์„ค์ •ํ•˜๊ณ  ์‹ถ๋‹ค.

์ฒซ๋ฒˆ์งธ๋กœ url.js ๋‚ด์—์„œ๋Š”

url.init(
  {
    url: DataTypes.STRING,
    title: DataTypes.STRING,
    visits: { type: DataTypes.INTEGER, defaultValue: 0 },
  },
  {
    sequelize,
    modelName: 'url',
  }
)

๋‘๋ฒˆ์งธ๋กœ migrations/20201208XXXXXXXX-create-url.js ์—์„œ๋Š”

visits: {
  type: Sequelize.INTEGER,
  defaultValue: 0,
},

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ORM ๊ณผ SCHEMA ๋ชจ๋‘์— visits ์˜ ๊ธฐ๋ณธ๊ฐ’์ด 0์œผ๋กœ ๋ฐ˜์˜๋œ๋‹ค.

7. ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ํ•˜๊ธฐ

์ˆ˜์ •์‚ฌํ•ญ์„ ๋ฐ˜์˜ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜์„ ํ•ด์ค˜์•ผ ํ•œ๋‹ค.

npx sequelize-cli db:migrate

์ด์ œ MySQL ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค์—์„œ describe urls ๋ฅผ ํ†ตํ•ด ์ปฌ๋Ÿผ๋“ค์ด ์ œ๋Œ€๋กœ ๋“ค์–ด์™”๋Š”์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

๊ทธ๋Ÿฐ๋ฐ ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜์„ ๋‹ค ํ•ด๋†“๊ณ ์„œ ๋ญ”๊ฐ€ ์ˆ˜์ •์„ ํ•ด์•ผ ํ•  ๋•Œ๊ฐ€ ์žˆ๋‹ค.

์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•˜๊ณ  ์ €์žฅํ•˜๋ฉด ๋˜์ง€ ์•Š์„๊นŒ ์‹ถ์€๋ฐ ๋ฐ˜์˜์ด ๋˜์ง€ ์•Š์•˜๋‹ค.

์ด๋•Œ๋Š”

๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ์ทจ์†Œ - (์ฝ”๋“œ)์ˆ˜์ •ํ•˜๊ธฐ - ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ํ•˜๊ธฐ

์˜ ๊ณผ์ •์„ ๊ฑฐ์ณ์•ผ ํ•œ๋‹ค.

8. ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ์ทจ์†Œํ•˜๊ธฐ

You can use db:migrate:undo, this command will revert most recent migration.

npx sequelize-cli db:migrate:undo

You can revert back to initial state by undoing all migrations with

db: migrate: undo: all

command.

You can also revert back to a specific migration by passing its name in โ€”to option.

npx sequelize-cli db:migrate:undo:all --to XXXXXXXXXXXXXX-create-posts.js

Written by@[DotoriMook]
ํ”„๋ก ํŠธ์—”๋“œ ์ฃผ๋‹ˆ์–ด ๊ฐœ๋ฐœ์ž ๋„ํ† ๋ฆฌ๋ฌต์˜ ๊ธฐ์ˆ ๊ฐœ๋ฐœ ๋ธ”๋กœ๊ทธ ์ž…๋‹ˆ๋‹ค.

GitHubMediumTwitterFacebook