ํฐ์คํ ๋ฆฌ ๋ทฐ
๊ฒ์ํ DB ์ ํ ํ๊ธฐ (Sequelize ์ฌ์ฉ)
๋๋์ด๋ฐ๐พ 2018. 11. 9. 15:51๊ฒ์ํ์ ๊ธ์ DB๋ฅผ ํตํด์ ์กฐ์ํ๊ธฐ ๋๋ฌธ์ ์ผ๋จ DB์ค์ ์ด ํ์ํฉ๋๋ค.
์ ๋ Sequelize๋ฅผ ์ด์ฉํ์ฌ DB๋ฅผ ํ์ฉํ๋ คํ๊ณ , SQL๋ฌธ ์์ฑ ์์ด Javascript๋ก ์งํ๋ฉ๋๋ค.
http://bubobubo003.tistory.com/32
ํด๋น ๋งํฌ์ Sequelize๋ฅผ settingํ๋ ๋ฐฉ๋ฒ์ ํฌ์คํ ํ ์ ์ด ์์ต๋๋ค. DB ํ๊ฒฝ์ setting ํ๋ค๋ ๊ฐ์ ํ์ ๋ด์ฉ์ ์งํํ๊ฒ ์ต๋๋ค.
[User] ํ ์ด๋ธ
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | NO | | NULL | |
| nick | varchar(20) | NO | | NULL | |
| user_id | varchar(50) | NO | UNI | NULL | |
| password | varchar(256) | NO | | NULL | |
| created_at | datetime | NO | | NULL | |
| updated_at | datetime | NO | | NULL | |
+------------+--------------+------+-----+---------+----------------+
[board] ํ ์ด๋ธ
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| subject | varchar(30) | NO | | NULL | |
| content | varchar(100) | NO | | NULL | |
| count | int(11) | NO | | 0 | |
| created_at | datetime | NO | | NULL | |
| updated_at | datetime | NO | | NULL | |
| fk_userId | int(11) | YES | MUL | NULL | |
+------------+--------------+------+-----+---------+----------------+
ํ ์ด๋ธ์ ์ด๋ ๊ฒ ๊ตฌ์ฑ๋์ด ์์ต๋๋ค. ์ฌ์ค user ํ ์ด๋ธ์ ํ๋ ๋ค์ด ์ฉ ๋ง์ ๋ค์ง ์์ง๋ง ๊ฐ์ธ ์ทจํฅ ๊ป ๋ฐ๊พธ์ ๋ ๋ฉ๋๋ค.
ํด๋น ํ ์ด๋ธ์ ๋ง๋๋ js์ model ํ์ผ์ ๋ณด๊ฒ ์ต๋๋ค.
models/user.js
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | var models = require('../models'); module.exports = function (sequelize, DataTypes) { const user = sequelize.define('User', { name: { field: 'name', type: DataTypes.STRING(20), allowNull:false }, nick: { field: 'nick', type: DataTypes.STRING(20), allowNull:false }, user_id: { field: 'user_id', type: DataTypes.STRING(50), allowNull: false, unique: true }, password: { field: 'password', type: DataTypes.STRING(256), allowNull:false }, }, { charset: 'utf8', collate: 'utf8_unicode_ci', underscored: true, freezeTableName: true, // define the table's name tableName: 'user' }); user.associate = function(models) { models.User.hasMany(models.Code, { foreignKey: 'fk_userId', onDelete: 'cascade' }); models.User.hasMany(models.Board,{ foreignKey: 'fk_userId', onDelete: 'cascade' }); }; return user; }; | cs |
models/board.js
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 | module.exports = function (sequelize, DataTypes) { const board = sequelize.define('Board', { subject: { field: 'subject', type: DataTypes.STRING(30), allowNull: false }, content: { field: 'content', type: DataTypes.STRING(100), allowNull: false }, count:{ field:'count', type:DataTypes.INTEGER, allowNull:false, defaultValue:0 } }, { charset: 'utf8', collate: 'utf8_unicode_ci', underscored: true, freezeTableName: true, tableName: 'board' }); return board; }; | cs |
๊ฐ๊ฐ ํ๋๋ฅผ ์ ์ํ๋ sequelize.define ๊ตฌ๋ฌธ์ ์ฝ๊ฒ ์ดํด๊ฐ ๊ฐ์ค ๊ฒ๋๋ค. ๊ทผ๋ฐ ๋ ํ ์ด๋ธ ์ฌ์ด์ ๊ด๊ณ๊ฐ ์กด์ฌํ๊ฒ ๋ฉ๋๋ค.
๊ฒ์ํ์ ๊ธ์ user์ค ๋๊ตฐ๊ฐ๊ฐ ์์ฑํ ๊ฒ์ด๊ณ , ๊ทธ๋ ๋ค๋ฉด ๊ฐ๊ฐ์ ๊ฒ์๋ฌผ์ ์ฃผ์ธ์ด ์์ด์ผ ํ๋ ๊ด๊ณ๊ฐ ์๊ธฐ๊ฒ ๋๊ฒ ์ฃ ?
์ฆ, ํ ๋ช ์ ์ ์ ๊ฐ ์ฌ๋ฌ๊ฐ์ ๊ฒ์๋ฌผ์ ๊ฐ์ง๊ฒ ๋๋ 1:n์ ๊ตฌ์กฐ๋ฅผ ๊ฐ๊ฒ ๋ฉ๋๋ค.
๊ทธ๋์ user.jsํ์ผ์์ ์ธ๋ํค ์ ์์ cascade ์์ฑ์ ๋ถ์ฌํ์ฌ user์ ์ ๋ณด๊ฐ ๋ณ๊ฒฝ๋์ ๋,
๊ทธ์ ๊ด๋ จ๋ ๋ชจ๋ ๊ฒ์๋ฌผ๋ค์ ์ ๋ณด๋ ์ผ๊ด์ ์ผ๋ก ๋ณ๊ฒฝ๋ ์ ์๊ฒ ๋ง๋ค์ด์ค๋๋ค.
Sequelize์ ํ ์ด๋ธ ์ ์ ํ์ผ์ ๋ค ์์ฑํ๋ค๋ฉด ์ ๋๋ก ํ ์ด๋ธ์ด ๋ง๋ค์ด ์ง๋ ์ง ํ์ธ์ ํด๋ด ๋๋ค.
routes/index.js
1 2 3 4 5 6 7 | var express = require('express') var router = express.Router() router.get('/',function(req,res){ res.send('hi') }) module.exports = router; | cs |
app.js
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 31 32 33 34 35 36 37 38 39 40 41 42 43 | var express = require('express'); var path = require('path'); var models = require('./models') var app = express(); //app.get var indexRouter = require('./routes/index'); //sequelize models.sequelize.sync() .then(function() { console.log('โ DB connection success.'); console.log(' Press CTRL-C to stop\n'); }) .catch(function(err) { console.error(err); console.log('โ DB connection error. Please make sure DB is running.'); process.exit(); }); // view engine setup app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'pug'); //dir path app.use(express.static(__dirname)); //get app.use('/', indexRouter); app.use(function(req, res, next) { next(createError(404)); }); // error handler app.use(function(err, req, res, next) { // set locals, only providing error in development res.locals.message = err.message; res.locals.error = req.app.get('env') === 'development' ? err : {}; // render the error page res.status(err.status || 500); res.render('error'); }); module.exports = app; | cs |
์ด๋ ๊ฒ ์์ฑ ํ app.js ํ์ผ์ ์คํ์์ผฐ์ ๋ console์ด ๋์ค๋ฉด ์ฑ๊ณต์ ๋๋ค.
javascript๋ก ์์ฑํ ํ ์ด๋ธ ์ ๋ณด๊ฐ SQL๋ฌธ์ผ๋ก ๋ณํ๋์ด์ console์ ์ฐํ๊ฒ ๋ฉ๋๋ค. ์ค์ ๋ก DB๋ฅผ ํ์ธํด๋ณด์๋ฉด ํ ์ด๋ธ์ด ์๊ธด ๊ฒ์ ๋ณผ ์ ์์ต๋๋ค.
'Node.js > ๊ฒ์ํ ๋ง๋ค๊ธฐ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๊ฒ์ํ ๊ธ ์์ฑํ๊ธฐ (0) | 2018.11.18 |
---|---|
DB์ ์๋ ๋ฐ์ดํฐ ๊ฒ์ํ ํ์ด์ง๋ก ๋๊ธฐ๊ธฐ (3) | 2018.11.12 |
Node.js๋ก ๊ฒ์ํ ๋ง๋ค๊ธฐ [์์] (0) | 2018.11.05 |
- Total
- Today
- Yesterday
- nestjs typeorm
- ๋น๋๊ธฐ ์์ฒญ
- node.js backend
- typeorm ์ฐ๊ฒฐ
- JavaScript
- Spring Async
- nestjs/cli
- Spring
- foreignkey
- nestjs config
- android
- ์๊ณ ๋ฆฌ์ฆ
- Promise error
- Promise bulk
- nestjs configService
- nestjs doc
- nestjs module
- docker mysql
- nestjs project
- sequelize
- nestjs directory
- typeorm
- NestJS
- DeferredResult
- @nestjs/config
- backend-framework
- Request_Limit_Exceeded
- ๊ธฐ์์ฒญAPI
- ํ๋ก๊ทธ๋๋จธ์ค
- node.js
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 | 31 |