Search

์‹œํ€„๋ผ์ด์ฆˆ

Sequelize

Node.js ํ™˜๊ฒฝ์—์„œ ์‚ฌ์šฉ๋˜๋Š” ORM(Object-Relational Mapping) ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ
MySQL, PostgreSQL, SQLite, MSSQL ๋“ฑ ๋‹ค์–‘ํ•œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. Sequelize๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด JavaScript ์ฝ”๋“œ๋ฅผ ํ†ตํ•ด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ ์ƒํ˜ธ ์ž‘์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์Šคํ‚ค๋งˆ๋ฅผ ๋ชจ๋ธ๋กœ ์ •์˜ํ•˜์—ฌ ์‰ฝ๊ฒŒ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ฃผ์š” ์†์„ฑ ๋ฐ ๋ฉ”์†Œ๋“œ

์†์„ฑ ๋ฐ ๋ฉ”์†Œ๋“œ
์„ค๋ช…
sequelize
Sequelize ์ธ์Šคํ„ด์Šค๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.
Sequelize.DataTypes
๋‹ค์–‘ํ•œ ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ์ œ๊ณตํ•˜๋Š” ์†์„ฑ์ž…๋‹ˆ๋‹ค.
sequelize.define()
๋ชจ๋ธ์„ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.
belongsTo()
๋‹ค์–‘ํ•œ ๊ด€๊ณ„๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. (๋‹ค๋ฅธ ๋ชจ๋ธ๊ณผ์˜ 1:N ๊ด€๊ณ„ ๋“ฑ)
hasMany()
๋‹ค์–‘ํ•œ ๊ด€๊ณ„๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. (๋‹ค๋ฅธ ๋ชจ๋ธ๊ณผ์˜ N:1 ๊ด€๊ณ„ ๋“ฑ)
hasOne()
๋‹ค์–‘ํ•œ ๊ด€๊ณ„๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. (๋‹ค๋ฅธ ๋ชจ๋ธ๊ณผ์˜ 1:1 ๊ด€๊ณ„ ๋“ฑ)
sequelize.sync()
๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ ๋ชจ๋ธ์„ ๋™๊ธฐํ™”ํ•ฉ๋‹ˆ๋‹ค.
sequelize.sync({ force: true })
๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ๊ฐ•์ œ๋กœ ์žฌ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
create(values, options)
์ƒˆ๋กœ์šด ๋ ˆ์ฝ”๋“œ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
findAll(options)
๋ชจ๋“  ๋ ˆ์ฝ”๋“œ๋ฅผ ๊ฒ€์ƒ‰ํ•ฉ๋‹ˆ๋‹ค.
findByPk(id, options)
์ฃผ์–ด์ง„ ๊ธฐ๋ณธ ํ‚ค๋กœ ๋ ˆ์ฝ”๋“œ๋ฅผ ๊ฒ€์ƒ‰ํ•ฉ๋‹ˆ๋‹ค.
update(values, options)
๋ ˆ์ฝ”๋“œ๋ฅผ ์—…๋ฐ์ดํŠธํ•ฉ๋‹ˆ๋‹ค.
destroy(options)
๋ ˆ์ฝ”๋“œ๋ฅผ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค.

์‚ฌ์šฉ ๋ชฉ์ 

Sequelize๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€์˜ ์ƒํ˜ธ ์ž‘์šฉ์„ ์ถ”์ƒํ™”ํ•˜์—ฌ ๊ฐœ๋ฐœ์ž๊ฐ€ SQL ์ฟผ๋ฆฌ๋ฅผ ์ง์ ‘ ์ž‘์„ฑํ•˜์ง€ ์•Š๊ณ ๋„ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ ํ†ต์‹ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ์ฝ”๋“œ์˜ ๊ฐ€๋…์„ฑ์„ ๋†’์ด๊ณ , ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์Šคํ‚ค๋งˆ์˜ ๋ณ€๊ฒฝ์— ์œ ์—ฐํ•˜๊ฒŒ ๋Œ€์ฒ˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ Sequelize๋Š” ๋‹ค์–‘ํ•œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์ง€์›ํ•˜๋ฏ€๋กœ, ํ”„๋กœ์ ํŠธ์˜ ์š”๊ตฌ ์‚ฌํ•ญ์— ๋งž๊ฒŒ ์„ ํƒํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

MySQL ์„ค์น˜

MySQL ์—ฐ๊ฒฐ

1.
๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค ์ƒ์„ฑ
2.
๋ชจ๋“ˆ ์„ค์น˜
3.
config.json (๋ฐ์ดํ„ฐ ์†Œ์Šค ์„ค์ •)
4.
model ์„ค์ •
5.
app ์„ค์ •

๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค ์ƒ์„ฑ

CREATE SCHEMA `test` ;
Plain Text
๋ณต์‚ฌ

package.json ์ƒ์„ฑ

npm init
Shell
๋ณต์‚ฌ

๋ชจ๋“ˆ ์„ค์น˜

# ๋ชจ๋“ˆ ์„ค์น˜ npm install express morgan pug sequelize sequelize-cli mysql2 dotenv # ๊ฐœ๋ณ„ ์„ค์น˜ npm install express npm install morgan npm install sequelize npm install sequelize-cli npm install mysql2 npm install dotenv --- # sequelize ์ดˆ๊ธฐํ™” (sequelize-cli ๋ชจ๋“ˆ ์ด์šฉ) npx sequelize init ### sequelize ์„ค์ • ํŒŒ์ผ๋“ค์ด ์ƒ์„ฑ๋จ ### ์ž๋™ ์ƒ์„ฑ๋œ models/index.js ์— ์—๋Ÿฌ ๋ฐœ์ƒ์ด์Šˆ ๋“ฑ์œผ๋กœ ์ฝ”๋“œ ์ˆ˜์ •
Markdown
๋ณต์‚ฌ

๋ชจ๋“ˆ ์„ค์น˜

npm install express morgan sequelize sequelize-cli mysql2 dotenv
Shell
๋ณต์‚ฌ

์‹œํ€„๋ผ์ด์ฆˆ ์ดˆ๊ธฐํ™”

npx sequelize init
Shell
๋ณต์‚ฌ

config.json (๋ฐ์ดํ„ฐ ์†Œ์Šค ์„ค์ •)

{ "development": { "username": "joeun", "password": "123456", "database": "test", "host": "127.0.0.1", "dialect": "mysql" }, "test": { "username": "joeun", "password": "123456", "database": "test", "host": "127.0.0.1", "dialect": "mysql" }, "production": { "username": "joeun", "password": "123456", "database": "test", "host": "127.0.0.1", "dialect": "mysql" } }
JSON
๋ณต์‚ฌ

model ์„ค์ •

index.js

const Sequelize = require('sequelize'); // โœ… ๋ชจ๋ธ import const Board = require('./board'); const env = process.env.NODE_ENV || 'development'; const config = require('../config/config')[env]; const db = {}; const sequelize = new Sequelize(config.database, config.username, config.password, config); db.sequelize = sequelize; // ๋ชจ๋ธ ๋“ฑ๋ก ๋ฐ ์„ค์ • db.Board = Board; // ๋ชจ๋ธ ์ดˆ๊ธฐํ™” ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ Board.initiate(sequelize); // ์—ฐ๊ด€ ๊ด€๊ณ„ ์„ค์ • ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ (ํ•„์š”์— ๋”ฐ๋ผ ์‚ฌ์šฉ) Board.associate(db); module.exports = db;
JavaScript
๋ณต์‚ฌ

board.js

const { DataTypes, Model, Sequelize } = require('sequelize'); class Board extends Model { static initiate(sequelize) { Board.init( { board_no: { type: DataTypes.INTEGER, autoIncrement: true, primaryKey: true, }, title: { type: DataTypes.STRING, allowNull: false, }, writer: { type: DataTypes.STRING, allowNull: false, }, content: { type: DataTypes.TEXT, allowNull: false, }, reg_date: { type: 'TIMESTAMP', // defaultValue: 'now()', }, upd_date: { type: 'TIMESTAMP', // defaultValue: 'now()', }, }, { sequelize, modelName: 'Board', tableName: 'board', timestamps: false, // true โžก createdAt, updatedAt ์ปฌ๋Ÿผ ์ž๋™์ƒ์„ฑ } ); } static associate(db) { // Add associations if needed } } module.exports = Board;
JavaScript
๋ณต์‚ฌ

app ์„ค์ •

.env

# ๋ชจ๋“œ (development, test, production) NODE_ENV=development # PORT ์„ค์ • PORT=3000
Plain Text
๋ณต์‚ฌ

app.js

const express = require('express'); const path = require('path'); const morgan = require('morgan'); const dotenv = require('dotenv'); const { sequelize } = require('./models'); dotenv.config(); // dotenv ์„ค์ • const app = express(); app.set('port', process.env.PORT || 3001); sequelize.sync({ force: false }) .then(() => { console.log('๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—ฐ๊ฒฐ ์„ฑ๊ณต'); }) .catch((err) => { console.error(err); }); app.use(morgan('dev')); app.use(express.static(path.join(__dirname, 'public'))); app.use(express.json()); app.use(express.urlencoded({ extended: false })); app.use((req, res, next) => { const error = new Error(`${req.method} ${req.url} ๋ผ์šฐํ„ฐ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.`); error.status = 404; next(error); }); app.use((err, req, res, next) => { res.locals.message = err.message; res.locals.error = process.env.NODE_ENV !== 'production' ? err : {}; res.status(err.status || 500); res.render('error'); }); app.listen(app.get('port'), () => { console.log(app.get('port'), '๋ฒˆ ํฌํŠธ์—์„œ ๋Œ€๊ธฐ ์ค‘'); });
JavaScript
๋ณต์‚ฌ

CRUD ๊ฒŒ์‹œํŒ ์‹ค์Šต