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
๋ณต์ฌ