ํฐ์คํ ๋ฆฌ ๋ทฐ
์ด ํ๊ฒฝ์ค์ ์ ํ๋๋ฐ ํ์ฐธ ์๊ฐ์ด ๊ฑธ๋ ธ์๋ค ใ ๋ค์ํ๋ฒ ์ฐจ๊ทผ์ฐจ๊ทผ ๋ฆฌ๋ทฐํด๋ณด์..
1. npm ์ค์น
1 | npm install sequelize |
1 | npm install mysql | cs |
=> ๋ ์ด ๋จ๊ณ๊น์ง๋ ๋๋ฌด ์์กฐ๋กญ๋ค. sequelize ์ค์น์ ๋ด๊ฐ ์ฌ์ฉํ๋ ค๋ mysql์ ์ค์นํด์ค๋ค.
(๊ณต์์ ์ผ๋ก PostgreSQL, MySQL, MariaDB, SQLite, MS-SQL์ ์ง์ํ๋ค.)
์ค์น์ต์ ์ ์์๋ ์ฐ์ง ์์์ง๋ง --save๋ก ํด์ฃผ๋๊ฒ ์ข์ ๋ฏ ํ๋ค.
2. ๋๋ ํ ๋ฆฌ(ํด๋) ์์ฑ
๋๋ ์ด์ฌํ ๊ตฌ๊ธ๋งํ๋ฉด์ ๋ฐ๋ผํ ๊ฒ์ด๋ผ ์ด๋ฐ ๋๋ ํ ๋ฆฌ ๋ค์ด๋ฐ์ด ๋ฌด์กฐ๊ฑด ๋ง๋์ง๋ ๋ชจ๋ฅด๊ฒ ์ง๋ง ํ์ฌ ๋ด๊ฐ ๋ฐ๋ฅด๊ณ ์๋ ๋ฐฉ๋ฒ์ด๋ค.
1) config
=> DB์ ์ ๋ณด๋ฅผ ๋ด๊ณ ์๋ json ํ์ผ์ ๋ด๊ณ ์๋ ๋๋ ํ ๋ฆฌ์ด๋ค. ํด๋น ๋๋ ํ ๋ฆฌ์ sequelize.json ํ์ผ์ ์์ฑํด์ค๋ค. ํด๋น ํ์ผ์
๋ด์ฉ์ ์ด๋ ๋ค.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | { "development": { "username": "root", "password": "1234", "database": "test", "host": "127.0.0.1", "dialect": "mysql" }, "test": { "username": "root", "password": "1234", "database": "scott", "host": "127.0.0.1", "dialect": "mysql" }, "production": { "username": "root", "password": "1234", "database": "movie", "host": "127.0.0.1", "dialect": "mysql" } } | cs |
๋ด๊ฐ ๋ ธ๋์์ผ๋ก ์น ํ ๋ถ๋ถ์ด ์ฌ์ฉ์๊ฐ ๊ฐ์ง๊ณ ์๋ DB ์ ๋ณด์ ๋ฐ๋ผ ๋ฌ๋ฆฌ ์์ฑํด์ค์ผ ํ๋ ๋ถ๋ถ์ด๋ค. ์ฌ์ฉ์ธ์ด ๋ถ๋ถ์ mysql์ ์ฐ๊ธฐ๋ก ํ์ผ๋ฏ๋ก ๋ฐ๋ก ์์น ํ์ง๋ ์์๋ค. ๊ธฐ๋ณธ์ ์ผ๋ก mysql์ด ์ปดํจํฐ์ ์ค์น๋์ด์๋ค๊ณ ๊ฐ์ ํ๊ฒ ์งํํ๊ณ ์๊ธฐ ๋๋ฌธ์ ๊ทธ๋ถ๋ถ์ ์๋ตํ๊ณ ๋งจ ์๋ถํฐ ์ฐจ๋ก๋ก DB ์ ์ ๋ช , ์ ์ ํจ์ค์๋, DB๋ช ์์ด๋ค.
์ด๋ ๊ฒํด์ ์ผ๋จ DB๊น์ง๋ ์ ๊ทผ์ ํ์๋ค.
2) models
=> ํ ์ด๋ธ์ ์ ๋ณด๋ฅผ ๋ด๊ณ ์๋ js ํ์ผ์ด ์๋ ๋๋ ํ ๋ฆฌ์ด๋ค. ํ ์ด๋ธ์ด ์์ฑ๋ ๋ ํด๋น ๋๋ ํ ๋ฆฌ์ ์๋ ์ ๋ณด๋ฅผ ํ ๋๋ก ํ ์ด๋ธ์ ์์ฑํ๋ค.
์ผ๋จ ๋ด test DB์ table ์ ๋ณด์ด๋ค. ๋ณด๋ค์ํผ test๋ผ๋ DB์ 3๊ฐ์ ํ ์ด๋ธ์ด ์กด์ฌํ๊ณ ๊ทธ ์ค์ code ๋ผ๋ ํ ์ด๋ธ์ ์ด์ฉํ ๊ฒ์ด๋ค.
๋ฐ๋ก ์ด์ ๋จ๊ณ์ธ config ๋จ๊ณ์์ ์ ๋ ฅํ DB ๊ตฌ์กฐ์ ์ผ์นํด์ผํ๋ค. ์ฆ, database ํญ๋ชฉ์ test๋ผ๋ DB๋ช ์ ์ป๋ค๋ฉด ํด๋น DB๊ฐ ์ค์ ๋ก ์กด์ฌํด์ผํ๋ค๋ ๊ฒ์ด๋ค.
code ํ ์ด๋ธ์ ์ ๋ณด๋ฅผ ๋ด๊ณ ์๋ code.js ํ์ผ์ ์์ฑํ๊ฒ ๋ค.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | module.exports = function (sequelize, DataTypes) { const code = sequelize.define('Code', { title: { field: 'title', type: DataTypes.STRING(10), allowNull: false }, code: { field: 'code', type: DataTypes.STRING(100), allowNull: false }, }, { // don't use camelcase for automatically added attributes but underscore style // so updatedAt will be updated_at underscored: true, // disable the modification of tablenames; By default, sequelize will automatically // transform all passed model names (first parameter of define) into plural. // if you don't want that, set the following freezeTableName: true, // define the table's name tableName: 'code' }); return code; }; | cs |
ํ ์ด๋ธ์ ์ ๋ณด๋ฅผ ๋ด๊ณ ์๋ ํ์ผ๋ก, ์ฌ๊ธฐ์ 2๊ฐ์ ํ๋ (title๊ณผ code) ๋ฅผ ๊ฐ์ง๋ค. ์ธ์ธํ ํ ์ด๋ธ ์ ๋ณด (๋ฐ์ดํฐ ํ์ ์ด๋ allowNull ๊ณผ ๊ฐ์ ๋ด์ฉ์ค๋ช ์ ์๋ตํ๋ค.)
์กฐ๊ธ ์ต์์น ์์ ๊ฒ๋ค์ด ์ฒ์๋ถํฐ ๋ณด์ธ๋ค. ์ฌ์ฉ์ ๋ชจ๋์ ๋ง๋๋ ๊ฒ์ธ๋ฐ ๊ฒฐ๋ก ๋ถํฐ ๋งํ์๋ฉด ํด๋น ํ์ผ์ด ๋ฆฌํดํ๋ ๊ฐ์ฒด code๋ฅผ ๋ค๋ฅธํ์ผ์์ ์ฐ๊ธฐ ์ํจ์ด๋ค.
define ์ด๋ผ๋ ๋ฉ์๋๋ก ํ ์ด๋ธ์ ์ ์ํ๊ฒ ๋๋ค. define์ ๋งค๊ฐ๋ณ์๋ก ๋ค์ด๊ฐ๋ ํ ์ด๋ธ๋ช (๋๋ 'Code' ๋ผ๊ณ ์ ์) ์ ๋์๋ฌธ์๋ฅผ ๊ตฌ๋ถํ๋ฏ๋ก ๋์ค์ ์ฌ์ฉํ๋ ค ํ ๋ models์์ ์ ์๋ ๊ฐ์ผ๋ก ์จ์ค์ผํ๋ค.
3. app.js ํ์ผ์์ ํด๋น ๋ชจ๋ ์ฌ์ฉ
DB ์ ์์ ํ ์ด๋ธ ์ ์๊น์ง ๋๋์ผ๋ ํด๋น ์ ๋ณด๋ฅผ ๋ณธ์ธ์ ์น ์ดํ๋ฆฌ์ผ์ด์ ์์ ์ฌ์ฉํ ์ฐจ๋ก์ด๋ค. ๋ฐ๋ก ์ด์ ์ ์ฌ์ฉ์ ๋ชจ๋์ด๋ผ๊ณ ๋ง๋ค์๋ ๊ฒ์ ์ฌ์ฉํด์ผํ๊ธฐ ๋๋ฌธ์ ํด๋น ๋ผ์ธ์ด ํ์ํ๋ค.
1 | var models = require('./models'); | cs |
require ๋์ด์๋ ๊ฒ์ models ๋ผ๋ ๋๋ ํ ๋ฆฌ๋ฅผ ์๊ธฐํ๋ ๊ฒ์ด๋ค.
๋๊ฐ์ ๊ฒฝ์ฐ๋ ์์๊ฐ์ด app.js ํ์ผ๊ณผ ๋์ผํ ๋๋ ํ ๋ฆฌ์ models ๋๋ ํ ๋ฆฌ๋ฅผ ๋ง๋ค์๊ธฐ ๋๋ฌธ์ ./models ๋ผ๊ณ ์ฌ์ฉํ ๊ฒ์ด๋ค.
์ฌ์ฉ์ ๋ชจ๋์ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ด๋ค. ๊ฒฐ๊ณผ๋ก models ๋ณ์์ ๊ฐ์ฒด๋ฅผ ์ ๋ฌํ๊ฒ ๋๋ค.
๊ทธ๋ผ ๋ฐ๋ก ํ๋จ์ ์ด ์ฝ๋๋ฅผ ์์ฑํด๋ณด์.
1 2 3 4 5 6 7 8 9 10 | models.sequelize.sync() .then(() => { console.log('โ DB connection success.'); console.log(' Press CTRL-C to stop\n'); }) .catch(err => { console.error(err); console.log('โ DB connection error. Please make sure DB is running.'); process.exit(); }); | cs |
models ๋ณ์์ ์ ๋๋ก ์ฐ๊ฒฐ๋ DB๊ฐ ๋ค์ด๊ฐ ์๋ค๋ฉด success ๋ฌธ์ด ๋์ฌํ ๊ณ ์๋ฌ๊ฐ ๋๋ค๋ฉด catch๋ฌธ์ ๊ฑธ๋ ค์ ์๋์ ๋ก๊ทธ๊ฐ ์ฐํ ๊ฒ์ด๋ค.
๊ฐ์ฅ ํ๋จ์ 2์ค์ด ๋์ค๋ฉด ์ผ๋จ DB์ฐ๊ฒฐ์ ์ฑ๊ณตํ ๊ฒ์ด๋ค. ์์ ๋์ค๋ ๋ก๊ทธ๋ค์ ์ models ๋๋ ํ ๋ฆฌ์์ ํ
์ด๋ธ์ ๋ง๋ค ๋ ๋ญ๊ฐ ์ข์ง ์์ ๋ฐฉ๋ฒ์ผ๋ก ๋ง๋ค์ด์ ๋์ค๋ ๊ฒฝ๊ณ ๋ฌธ? ๊ฐ๋ค. ์ผ๋จ ์ฌ๊ธฐ๊น์ง ๋์๋ค๋ฉด models ๋๋ ํ ๋ฆฌ์ ๋ง๋ค์๋ ํ
์ด๋ธ๋ค์ ์ ๊ทผ์ด ๊ฐ๋ฅํ๋ค.
๋ค์ ํฌ์คํ ์์๋ ํํ DB ๋ถ์ผ์์ ๋งํ๋ ๋ฐ์ดํฐ์กฐ์์ด(DDL) ์ ๋ฐ์ดํฐ์ ์์ด(DML) ๋ฅผ ๋ค๋ค๋ณด๋๋ก ํ๊ฒ ๋ค.
'Node.js > Node.js ๊ณต๋ถ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
8.4 Sequelize ๋ก DML ๊ตฌํํด๋ณด๊ธฐ (0) | 2018.10.06 |
---|---|
8.3 Sequelize foreignKey ์ง์ ํ๊ธฐ(cascade ์ ์ฉ) (0) | 2018.09.27 |
8.1 Node js ORM ( Sequelize.js ) ์๊ฐ (0) | 2018.06.28 |
7. ํ์ผ ์ ๋ก๋ (multer ๋ชจ๋ ์ด์ฉ) (0) | 2018.05.26 |
6. express ์ฟผ๋ฆฌ์คํธ๋ง ์ฌ์ฉ (0) | 2018.04.25 |
- Total
- Today
- Yesterday
- ํ๋ก๊ทธ๋๋จธ์ค
- Spring Async
- ๋น๋๊ธฐ ์์ฒญ
- JavaScript
- sequelize
- docker mysql
- nestjs doc
- node.js
- node.js backend
- nestjs typeorm
- backend-framework
- typeorm ์ฐ๊ฒฐ
- android
- NestJS
- @nestjs/config
- Spring
- typeorm
- foreignkey
- nestjs config
- ์๊ณ ๋ฆฌ์ฆ
- Promise bulk
- nestjs/cli
- Promise error
- nestjs project
- nestjs configService
- ๊ธฐ์์ฒญAPI
- Request_Limit_Exceeded
- nestjs module
- DeferredResult
- nestjs directory
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |