Nodejs Mysql ๊ณผ Serialize

๋“ค์–ด๊ฐ€๋ฉด์„œ

MySQL์€ ์„ธ๊ณ„์—์„œ ๊ฐ€์žฅ ๋งŽ์ด ์“ฐ์ด๋Š” ์˜คํ”ˆ ์†Œ์Šค์˜ ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ด€๋ฆฌ ์‹œ์Šคํ…œ์ด๋‹ค. MariaDB๋Š” ์˜คํ”ˆ ์†Œ์Šค์˜ ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ด€๋ฆฌ ์‹œ์Šคํ…œ์ด๋‹ค. MySQL๊ณผ ๋™์ผํ•œ ์†Œ์Šค ์ฝ”๋“œ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•˜๋ฉฐ, GPL v2 ๋ผ์ด์„ ์Šค๋ฅผ ๋”ฐ๋ฅธ๋‹ค. ์˜ค๋ผํด ์†Œ์œ ์˜ ํ˜„์žฌ ๋ถˆํ™•์‹คํ•œ MySQL์˜ ๋ผ์ด์„ ์Šค ์ƒํƒœ์— ๋ฐ˜๋ฐœํ•˜์—ฌ ๋งŒ๋“ค์–ด์กŒ์œผ๋ฉฐ, ๋ฐฐํฌ์ž๋Š” ๋ชฌํ‹ฐ ํ”„๋กœ๊ทธ๋žจ AB์™€ ์ €์ž‘๊ถŒ์„ ๊ณต์œ ํ•ด์•ผ ํ•œ๋‹ค. ๋งˆ๋ฆฌ์•„DB๋Š” MySQL๊ณผ ์†Œ์Šค์ฝ”๋“œ๋ฅผ ๊ฐ™์ด ํ•˜๋ฏ€๋กœ ์‚ฌ์šฉ๋ฐฉ๋ฒ•๊ณผ ๊ตฌ์กฐ๊ฐ€ MySQL๊ณผ ๋™์ผํ•˜๋‹ค. ๊ทผ๋ณธ์ ์œผ๋ก  ์˜ค๋ผํด์—์„œ ์ž์œ ๋กญ๋‹ค.

MySQL Setting

mysql> CREATE DATABASE  MY_DB;
Query OK, 1 row affected (0.01 sec)

mysql> use my_db;
Database changed

mysql> create table users(
    -> id varchar(45) not null,
    -> password varchar(45) not null,
    -> primary key (id));
Query OK, 0 rows affected (0.01 sec)

mysql> Insert into Users(id, password) values ('song','1234');
Query OK, 1 row affected (0.01 sec)

mysql> select * from users;
+------+----------+
| id   | password |
+------+----------+
| song | 1234     |
+------+----------+
1 row in set (0.00 sec)

Node JS

Nodejs ๋Š” ์ด๋ฒคํŠธ ๊ธฐ๋ฐ˜์˜ ํ”Œ๋žซํผ. ๋…ธ๋“œ์—์„œ ์ผ์–ด๋‚˜๋Š” ๋ชจ๋“  ์ผ์€ ์–ด๋–ค ์ด๋ฒคํŠธ์— ๋Œ€ํ•œ ๋ฐ˜์‘. ๋ชจ๋‘ ์ผ๋ จ์˜ ์ฝœ๋ฐฑ์ด๋‹ค. libuv๋ผ๋Š” ์ถ”์ƒํ™”๋œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์ด๋ฒคํŠธ ๋ฃจํ”„ ๊ธฐ๋Šฅ์„ ์ œ๊ณต. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‹คํ–‰ํ•˜๋Š” ์Šค๋ ˆ๋“œ๋Š” ๋‹จ ํ•˜๋‚˜, ์ด ์Šค๋ ˆ๋“œ๊ฐ€ ๋ฐ”๋กœ ์ด๋ฒคํŠธ ๋ฃจํ”„๊ฐ€ ์‹คํ–‰๋˜๋Š” ์Šค๋ ˆ๋“œ.

๋ชจ๋“  CPU ํ™œ์šฉ

Node.js ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์€ ์‹ฑ๊ธ€ ์Šค๋ ˆ๋“œ๋กœ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค. ๋ฉ€ํ‹ฐ์ฝ”์–ด ํ™˜๊ฒฝ์—์„œ 1๊ฐœ์˜ Node.js ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์€ ํšจ์œจ์ ์œผ๋กœ ์ž‘๋™ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋‚ญ๋น„๋˜๋Š” CPU๊ฐ€ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. Cluster Module์„ ์‚ฌ์šฉํ•˜๋ฉด, CPU ๋งˆ๋‹ค child ํ”„๋กœ์„ธ์Šค๋ฅผ ์‰ฝ๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ๊ฐ์˜ child ํ”„๋กœ์„ธ์Šค๋Š” ๊ฐ์ž ์ž์‹ ๋งŒ์˜ ์ด๋ฒคํŠธ๋ฃจํ”„๊ฐ€ ์กด์žฌํ•˜๊ณ  master ํ”„๋กœ์„ธ์Šค๋Š” ๋ชจ๋“  ์ž์‹๋“ค์—๊ฒŒ ์š”์ฒญ์„ ๋ถ„์‚ฐ์‹œ์ผœ ์ค๋‹ˆ๋‹ค.

์Šค๋ ˆ๋“œ ํ’€ ์กฐ์ •

์•ž์„œ ์–ธ๊ธ‰ํ–ˆ๋“ฏ์ด, libuv๋Š” ์Šค๋ ˆ๋“œ 4๊ฐœ๋กœ ์Šค๋ ˆ๋“œ ํ’€์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์Šค๋ ˆ๋“œ ํ’€์˜ ๊ธฐ๋ณธ ํฌ๊ธฐ๋Š” UV_THREADPOOL_SIZE ํ™˜๊ฒฝ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•ด์„œ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ฐฉ๋ฒ•์€ I/O ์ž‘์—…์ด ๋งŽ์€ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ๋„์›€์ด ๋  ์ˆ˜ ์žˆ๊ฒ ์ง€๋งŒ, ํฐ ์Šค๋ ˆ๋“œ ํ’€์€ ๋ฉ”๋ชจ๋ฆฌ๋‚˜ CPU๋ฅผ ๊ณ ๊ฐˆ์‹œํ‚ฌ ์ˆ˜ ์žˆ์Œ์„ ๊ธฐ์–ตํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์ž‘์—…์„ ๋‹ค๋ฅธ์„œ๋น„์Šค์— ๋งก๊ธฐ๊ธฐ

๋งŒ์•ฝ Node.js๊ฐ€ CPU์‚ฌ์šฉ์ด ๊ณผ๋„ํ•˜๊ฒŒ ํ•„์š”ํ•œ ์ž‘์—…์—์„œ ์‚ฌ์šฉ๋œ๋‹ค๋ฉด, ์ด ํŠน์ • ์ž‘์—…์— ๋” ์ž˜๋งž๋Š” ๋‹ค๋ฅธ ์–ธ์–ด๋ฅผ ์„ ํƒํ•ด์„œ ๊ทธ ์ชฝ์œผ๋กœ ์ฒ˜๋ฆฌ๋ฅผ ์˜ฎ๊ฒจ ์ž‘์—…๋Ÿ‰์„ ์ค„์ด๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•œ ๋ฐฉ๋ฒ•์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

package.json์€ ํ”„๋กœ์ ํŠธ ์ •๋ณด์™€ ์˜์กด์„ฑ(dependencies)์„ ๊ด€๋ฆฌํ•˜๋Š” ๋ฌธ์„œ ์ด๋ฏธ ์ž‘์„ฑ๋œ package.json ๋ฌธ์„œ๋Š” ์–ด๋Š ๊ณณ์—์„œ๋„ ๋™์ผํ•œ ๊ฐœ๋ฐœ ํ™˜๊ฒฝ์„ ๊ตฌ์ถ•ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค€๋‹ค. JSON ํฌ๋งท์œผ๋กœ ์ž‘์„ฑํ•ด์•ผ ํ•˜๋ฉฐ, ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์˜ต์…˜๋“ค์ด ์ถ”๊ฐ€๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

$ npm init
  • package name

  • version

  • description

  • entry point

  • test command

  • git repository

  • keywords

  • author

  • license

$ npm init -y
โžœ  mysql npm init

Press ^C at any time to quit.
package name: (mysql) nodejs-mysql
version: (1.0.0) 
description: mysql-nodejs exercise
entry point: (index.js) 
test command: node index.js
git repository: 
keywords: 
author: rumblekat
license: (ISC) 
About to write to /Users/songmyeongjin/Desktop/mysql/package.json:

{
  "name": "nodejs-mysql",
  "version": "1.0.0",
  "description": "mysql-nodejs exercise",
  "main": "index.js",
  "scripts": {
    "test": "node index.js"
  },
  "author": "rumblekat",
  "license": "ISC"
}


Is this OK? (yes) 

ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ์ž‘์„ฑ ํ›„ npm test ์‹คํ–‰

โžœ  mysql npm test

> nodejs-mysql@1.0.0 test
> node index.js

hello world

NodeJS์™€ MySQL ์—ฐ๋™

createConnection ๋ฉ”์†Œ๋“œ์˜ ์ธ์ž๋กœ ์ „๋‹ฌ๋˜๋Š” ๊ฐ์ฒด์— ์ž์‹ ์˜ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ •๋ณด(์œ ์ €๋ช…๊ณผ ํŒจ์Šค์›Œ๋“œ ๋“ฑ)์„ ์ž…๋ ฅํ•ด์•ผํ•œ๋‹ค.

> node index.js

internal/modules/cjs/loader.js:968
  throw err;
  ^

Error: Cannot find module 'mysql2' ---> ๋ชจ๋“ˆ์„ ์ฐพ์„ ์ˆ˜ ์—†์„ ๊ฒฝ์šฐ, npm install <Module> --save ๋กœ ํ•ด๋‹น ์˜์กด์„ฑ์„ ๋ถˆ๋Ÿฌ์˜จ๋‹ค.
Require stack:
- /Users/songmyeongjin/Desktop/mysql/index.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:965:15)
    at Function.Module._load (internal/modules/cjs/loader.js:841:27)
    at Module.require (internal/modules/cjs/loader.js:1025:19)
    at require (internal/modules/cjs/helpers.js:72:18)
    at Object.<anonymous> (/Users/songmyeongjin/Desktop/mysql/index.js:1:15)
    at Module._compile (internal/modules/cjs/loader.js:1137:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
    at Module.load (internal/modules/cjs/loader.js:985:32)
    at Function.Module._load (internal/modules/cjs/loader.js:878:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ '/Users/songmyeongjin/Desktop/mysql/index.js' ]
}
โžœ $mysql npm install mysql2 --save
const mysql = require("mysql2");
const connection = mysql.createConnection({
    host : 'localhost',
    user : 'root',
    port : 3306,
    password : 'Init123$',
    database : 'my_db'
});

connection.connect();

connection.query('select * from users',(err,rows,fields)=>{
    if(err) throw err;
    console.log('User info is ...');
    console.table(rows);
});

connection.end();

Mysql ์—ฐ๋™ ํ™•์ธ

โžœ $mysql npm test

> nodejs-mysql@1.0.0 test
> node index.js

User info is ...
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ (index) โ”‚   id   โ”‚ password โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚    0    โ”‚ 'song' โ”‚  '1234'  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Nodejs ์›น์„œ๋ฒ„

Express vs Koa

  • Express ๋งŒ๋“  ํŒ€์ด 2016๋…„์— Koa๋ฅผ ๋งŒ๋“ฌ

  • Express๋Š” ๋ผˆ๋Œ€๋กœ ์„ค์น˜๋˜๋Š” ๋ชจ๋“ˆ์ด ๋งŽ๊ณ , ๋ฏธ๋“ค์›จ์–ด๋ฅผ ๋ถ™์ผ ๋•Œ ๊ผญ Express ๊ธฐ๋ฐ˜

  • Koa๋Š” ๋ผˆ๋Œ€๋กœ ์„ค์น˜๋˜๋Š” ๋ชจ๋“ˆ์ด ์ ๊ณ , ์ปค์Šคํ„ฐ๋งˆ์ด์ง•์ด ์ž์œ ๋กญ๋‹ค.

  • Koa๋Š” ํƒœ์ƒ์ด ES6, Async/Await์„ ์ง€์›

  • Express๋Š” community๊ฐ€ ๊ฐ•

Express

const express = require('express'); 
const app = express(); 
const PORT = process.env.PORT || 3000; 
const server = app.listen(PORT, () => { 
	console.log(`Express is listening to <http://localhost>:${PORT}); 
});

Koa

const koa = require('koa'); 
const app = express(); 
const PORT = process.env.PORT || 3000; 
const server = app.listen(PORT, () => { 
	console.log(`Koa is listening to <http://localhost>:${PORT}); 
});

Middleware

Express

app.use((req, res, next) => { 
	console.log(`Time : ${Date.now()}`); 
    next(); 
});

Koa

app.use(async (ctx, next) => { 
	console.log(`Time: ${Date.now()}`); 
    await next(); 
});

Express

Express๋Š” ์›น ๋ฐ ๋ชจ๋ฐ”์ผ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์œ„ํ•œ ์ผ๋ จ์˜ ๊ฐ•๋ ฅํ•œ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๋Š” ๊ฐ„๊ฒฐํ•˜๊ณ  ์œ ์—ฐํ•œ Node.js ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ํ”„๋ ˆ์ž„์›Œํฌ.

  1. require๋Š” nodeJS์—์„œ ๋‹ค๋ฅธ ํŒจํ‚ค์ง€๋ฅผ ๋ถˆ๋Ÿฌ์˜ฌ ๋•Œ ์‚ฌ์šฉ๋˜๋Š” ํ‚ค์›Œ๋“œ. NODE_PATH ํ™˜๊ฒฝ ๋ณ€์ˆ˜์— ์„ค์ •ํ•œ ์œ„์น˜์—์„œ express๋ผ๋Š” ๋ชจ๋“ˆ์„ ์ฐพ๋Š”๋‹ค.

  2. app์ด๋ผ๋Š” ๋ณ€์ˆ˜์— express ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜๊ฐ’์„ ์ €์žฅ / REST End Point ์ƒ์„ฑ

  3. Process.env๋Š” nodejs์—์„œ ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ๊ฐ€์ ธ์˜ฌ๋•Œ ์‚ฌ์šฉ๋จ, ํ˜„์žฌ default๋Š” 3000

  4. app.get / get ์š”์ฒญ์œผ๋กœ ์ •์˜ ์—”๋“œํฌ์ธํŠธ๋ฅผ ์ž‘์„ฑ

    • ์—”๋“œํฌ์ธํŠธ ์ƒ์„ฑ์‹œ ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” ๋‘๊ฐ€์ง€๋ฅผ ๋ฐ›๋Š”๋ฐ, ์ฒซ๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” URL ์ •์˜, ๋‘๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” ํ•ด๋‹น url์—์„œ ์ˆ˜ํ–‰ํ•  ์ž‘์—… ๋ฐ ์‘๋‹ต์„ ์ •์˜

    • req(์š”์ฒญ)/ res(์‘๋‹ต)

module.exports = {
    host : 'localhost',
    user : 'root',
    port : 3306,
    password : 'Init123$',
    database : 'my_db'
}
const mysql = require("mysql2");
const express = require('express');
const dbconfig = require('./config/database');
const connection = mysql.createConnection(dbconfig);

const app = express();

const port = process.env.PORT || 3000;

app.get('/', (req, res) => {
    res.json({
        success: true,
    });
});

app.get('/users',(req,res)=>{
    connection.query('SELECT * from users',(error, rows)=>{
        if(error) throw error;
        res.json(rows);
    })
})


app.listen(port, () => {
    console.log(`server is listening at localhost:${process.env.PORT}`);
});

POST Insert ๊ตฌ๋ฌธ ์ถ”๊ฐ€

const bodyParser = require('body-parser')
app.use(bodyParser.json())

app.post('/users',(req,res)=>{
    const { id , pw } = req.body;
    const sql = 'INSERT INTO users (id,password) VALUES(?,?)';

    connection.query(sql,[id,pw],(err,rows,fields)=>{
        if(err) return console.log(err);
        return console.log(rows.insertId);
    });

    console.log(id, pw);
    res.json({success: "s"});
});
mysql> select * from users;
+---------+----------+
| id      | password |
+---------+----------+
| song    | 1234     |
| song11  | 1234     |
| song112 | 1234     |
+---------+----------+
3 rows in set (0.00 sec)

Sequelize

ORM์ด๋ž€ ๊ฐ์ฒด์™€ ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ž๋™์œผ๋กœ ๋งคํ•‘(์—ฐ๊ฒฐ)ํ•ด์ฃผ๋Š” ๊ฒƒ์„ ๋งํ•œ๋‹ค. ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์€ ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋Š” ํ…Œ์ด๋ธ”์„ ์‚ฌ์šฉํ•œ๋‹ค. ์ฆ‰ ์—ฌ๊ธฐ์„œ Sequelize๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ์ฒด์™€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ๋ฆด๋ ˆ์ด์…˜์„ ๋งคํ•‘ํ•ด์ฃผ๋Š” ์œ ์šฉํ•œ ๋„๊ตฌ๋ผ๋Š” ์˜๋ฏธ์ด๋‹ค! ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ตฌ๋ฌธ์„ ์•Œ์•„์„œ SQL๋ฌธ์œผ๋กœ ๋ณ€๊ฒฝํ•ด์ค€๋‹ค.

$ npm i sequelize mysql2
$ npm i -g sequelize-cli
$ sequelize init
โžœ  $mysql npm i sequelize --save 

added 17 packages, and audited 83 packages in 3s

โžœ  $mysql npm i -g sequelize-cli

added 81 packages, and audited 82 packages in 5s

โžœ  $mysql sequelize init

Sequelize CLI [Node: 12.18.3, CLI: 6.2.0, ORM: 6.6.2]

Created "config/config.json"
Successfully created models folder at "/Users/songmyeongjin/Desktop/mysql/models".
Successfully created migrations folder at "/Users/songmyeongjin/Desktop/mysql/migrations".
Successfully created seeders folder at "/Users/songmyeongjin/Desktop/mysql/seeders".

config/config.json

DB ์—ฐ๊ฒฐ ๊ด€๋ จ config ์ •๋ณด๋Š” ๋ชจ๋‘ config.json์—์„œ ๊ด€๋ฆฌํ•œ๋‹ค.

{
  "development": {
    "username": "root",
    "password": "Init123$",
    "database": "database_development",
    "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"
  }
}

Sequelize DB Create

Sequelize-cli๋ฅผ ์ด์šฉํ•ด์„œ DB๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์กฐ์ž‘ํ•  ์ˆ˜ ์žˆ์Œ

โžœ  $mysql sequelize db:create

Sequelize CLI [Node: 12.18.3, CLI: 6.2.0, ORM: 6.6.2]

Loaded configuration file "config/config.json".
Using environment "development".
Database database_development created.
mysql> show databases;
+----------------------+
| Database             |
+----------------------+
| database_development | <-- 
| information_schema   |
| MY_DB                |
| mysql                |
| performance_schema   |
| sys                  |
+----------------------+
6 rows in set (0.01 sec)

Table ์ƒ์„ฑ

Sequelize๋ฅผ ์ด์šฉํ•˜์—ฌ DDL์„ ์•„๋ž˜์™€ ๊ฐ™์ด ์ž‘์„ฑํ• ์ˆ˜ ์žˆ๋‹ค. (์ฃผ์˜ํ•ด์•ผํ•  ์ ์€, --attribute๋’ค์— ์ปฌ๋Ÿผ์„ ์ฝค๋งˆ๋กœ ๊ตฌ๋ถ„ํ•œ๋‹ค.๋„์–ด์“ฐ๊ธฐ์—†์ด)

$sequelize model:generate --name Point --attributes pid:integer,pntamt:integer,userId:string  

model ํŒŒ์ผ๊ณผ, migration ํŒŒ์ผ์ด ์ƒ์„ฑ

์ƒ์„ฑํ•œ Models์€ /models ํด๋” ์•ˆ์— ์ €์žฅ๋˜๊ณ , migrations๋Š” /migrations ํด๋” ์•ˆ์— ์ €์žฅ๋œ๋‹ค. MigrationsํŒŒ์ผ ์•ž์— ๋ถ™์€ ์ˆซ์ž๋Š” ์‹œ๊ฐ„์— ๋Œ€ํ•œ ์ •๋ณด๋กœ sequelize๋Š” ์ด๋ฅผ ์ธ์‹ํ•ด ์–ด๋–ค ๋งˆ์ด๊ทธ๋ ˆ์…˜์ด ๋จผ์ €์ด๊ณ , ์–ด๋–ค ์ˆœ์„œ๋กœ ์ƒ์„ฑ๋˜์–ด์•ผ ํ•  ์ง€์— ๋Œ€ํ•ด ํŒ๋‹จํ•œ๋‹ค. (์ง€๊ธˆ์€ ๋ณ„๋กœ ์ค‘์š”ํ•˜์ง€ ์•Š์ง€๋งŒ, ๋‚˜์ฃผ์—ฅ columns๊ณผ tables๋ฅผ ์ถ”๊ฐ€ํ•  ๋•Œ ์ค‘์š”)

Sequelize CLI [Node: 12.18.3, CLI: 6.2.0, ORM: 6.6.2]

New model was created at /Users/songmyeongjin/Desktop/mysql/models/point.js .
New migration was created at /Users/songmyeongjin/Desktop/mysql/migrations/20210326125441-create-point.js .
mysql> desc points;
+-----------+--------------+------+-----+---------+----------------+
| Field     | Type         | Null | Key | Default | Extra          |
+-----------+--------------+------+-----+---------+----------------+
| id        | int          | NO   | PRI | NULL    | auto_increment |
| pid       | int          | YES  |     | NULL    |                |
| pntamt    | int          | YES  |     | NULL    |                |
| userId    | varchar(255) | YES  |     | NULL    |                |
| createdAt | datetime     | NO   |     | NULL    |                |
| updatedAt | datetime     | NO   |     | NULL    |                |
+-----------+--------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)
$ sequelize db:migrate --env development

Last updated