๊ทธ๋์ MySQL์ ์ด์ฉํด ๊ฐ๋ฐํ ๋ Sequelize๋ฅผ ์ฌ์ฉํ์๋๋ฐ
์ด๋ฒ์ ์งํ์ค์ธ Node.js ํ๋ก์ ํธ์์ TypeORM์ ์ฒ์ ์ฌ์ฉํด๋ด์ ๊ณต๋ถ๋ฅผ ํด๋ณด๊ฒ ์ต๋๋ค..
์ฐ์ ORM์ด ๋ญ์ง๋ถํฐ ์์๋ด์ผํ๋๋ฐ
ORM์ด๋ Object-Relational Mapping์ ์ฝ์๋ก, ๊ฐ์ฒด์ ๊ด๊ณํ ๋ฐ์ดํฐ๋ฒ ์ด์ค ๊ฐ์ ๋งคํ์ ์ ๊ณตํ๋ ๊ธฐ์ ์ด๋ค
(๊ฐ๋ฐ์๊ฐ SQL ์ฟผ๋ฆฌ๋ฅผ ์ง์ ์์ฑํ์ง ์๊ณ ๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ฅผ ๋ค๋ฃฐ ์ ์๊ฒ ๋์์ค)
๊ทธ๋ผ ๊ด๊ณํ ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ ๋ ๋ญ๋,,,?
๊ด๊ณํ ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ ๋ฐ์ดํฐ๋ฅผ ํ
์ด๋ธ ํ์์ผ๋ก ์ ์ฅํ๊ณ , ํ
์ด๋ธ ๊ฐ์ ๊ด๊ณ๋ฅผ ์ด์ฉํ์ฌ ๋ฐ์ดํฐ๋ฅผ ์กฐ์งํํ๋
๋ฐ์ดํฐ๋ฒ ์ด์ค ์์คํ
์ด๋ค.
=> ์ด๋ฌํ ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ ๊ด๊ณํ ๋ชจ๋ธ์ ๊ธฐ๋ฐํ๊ณ ์์ผ๋ฉฐ, ๋ฐ์ดํฐ๋ฅผ ํ๊ณผ ์ด์ ํํ๋ก ํํํ๋ ํ
์ด๋ธ์ ์ฌ์ฉํจ!
์ด๋ฐ ๊ด๊ณํ ๋ฐ์ดํฐ๋ฒ ์ด์ค ๊ด๋ฆฌ ์์คํ RDBMS์ ๋ํ์ ์ธ ์์๊ฐ ํ์ฌ ํ๋ก์ ํธ์์ ์ฌ์ฉํ๋ MySQL!
๊ทธ๋์ TypeORM์ด ๋ญ๋๋ํ๋ฉด..
TypeScript์ JavaScript๋ฅผ ์ํ ๊ฐ์ฒด ๊ด๊ณ ๋งคํ(ORM) ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ก,
๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ํธ ์์ฉํ ๋ ๊ฐ์ฒด ์งํฅ์ ์ธ ์ ๊ทผ ๋ฐฉ์์ ์ฌ์ฉํ ์ ์๊ฒ ํด์ฃผ๋ ๋๊ตฌ๋ผ๊ณ ์๊ฐํ๋ฉด ๋๋ค
TypeORM์ ์ํฐํฐ๋ฅผ ์ ์ํ ๋ ํด๋์ค๋ฅผ ์ฌ์ฉํ๋ฉฐ ์ด๋ ์ฝ๋์ ๊ฐ๋ ์ฑ๊ณผ ์ฌ์ฌ์ฉ์ฑ์ ๋์ด๋๋ฐ ๋์์ด ๋๋ค
(๊ฐ ํด๋์ค์ ๋ฉค๋ฒ ๋ณ์๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ด(Column)์ ๋งคํ)
๊ทธ๋ผ ๋ฐ๋ก TypeORM์ ์ฌ์ฉํ๋ ๋ฒ์ ์์๋ณด์~~
module.exports = {
host: "141.164.37.26",
port: 3306,
username: "test",
password: "๋น๋ฐ~",
database: "test",
};
configs/db.config.js
๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฐ๊ฒฐ์ ํ์ํ ์ ๋ณด๋ฅผ ํฌํจํ๋ ์ค์ ํ์ผ์ ๋ง๋ค์ด์ค๋ค
=> ์ด ์ ๋ณด๋ค์ TypeORM๊ณผ ๊ฐ์ ORM(Object-Relational Mapping) ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ์ฌ Node.js์์
MySQL ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ฐ๊ฒฐํ ๋ ํ์ํจ!
module.exports = class User {
constructor(id, username, password, name, studentId) {
this.id = id;
this.username = username;
this.password = password;
this.name = name;
this.studentId = studentId;
}
};
models/user.model.js
์ด๋ ๊ฒ User ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ํ์ผ์ ํ๋ ๋ง๋ค์ด์ฃผ๊ณ
const EntitySchema = require("typeorm").EntitySchema;
const User = require("../models/user.model");
module.exports = new EntitySchema({
name: "User",
target: User,
columns: {
id: {
primary: true,
type: "bigint",
generated: true,
},
username: {
unique: true,
type: "varchar",
nullable: false,
},
password: {
type: "varchar",
nullable: false,
},
name: {
type: "varchar",
nullable: false,
},
studentId: {
name: "student_id",
type: "varchar",
nullable: false,
},
},
});
entities/user.entity.js
(์ฐ์ entitiy๊ฐ ๋ญ์ง ์์์ผํ๋๋ฐ ์ํฐํฐ๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค ํ ์ด๋ธ๊ณผ ๋งคํ๋๋ ๊ฐ์ฒด์ด๋ค!)
EntitySchema๋ TypeORM์์ ์ํฐํฐ๋ฅผ ์ ์ํ๊ธฐ ์ํ ์คํค๋ง๋ฅผ ์์ฑํด์ค๋ค
์์ ์ฝ๋์ฒ๋ผ EntitySchema๋ฅผ ์ฌ์ฉํ์ฌ User ํด๋์ค๋ฅผ ์ํฐํฐ๋ก ์ ์ํ๋ค.
(target ์์ฑ์๋ ๋งคํํ ํด๋์ค๋ฅผ ์ง์ ํ๊ณ , columns ์์ฑ์๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค ํ ์ด๋ธ์ ์ด์ ์ ์)
=> target ์์ฑ์ User ํด๋์ค๋ฅผ ์ง์ ํจ์ผ๋ก์จ, ์ด ์ํฐํฐ๋ User ํด๋์ค์ ๋งคํ๋จ
์ด๋ฅผ ํตํด TypeORM์ ์ํฐํฐ๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ๋ฐ์ดํฐ๋ฒ ์ด์ค ํ ์ด๋ธ์ ์์ฑํ๊ณ , ์ด ์คํค๋ง๋ฅผ ํตํด ์ปฌ๋ผ์ ํ์ ๋ฐ ๋ค์ํ ์ค์ ์ ์ ์ํ ์ ์๋ค!
const { DataSource } = require("typeorm");
const config = require("./configs");
module.exports = new DataSource({
type: "mysql",
synchronize: true,
logging: true,
entities: ["./entities/*.js"],
migrations: [],
subscribers: [],
...config.db,
});
data-source.js
์์ ์ฝ๋๋ TypeORM์์ ๋ฐ์ดํฐ ์์ค๋ฅผ ์ค์ ํ๊ณ ์์ฑํ๊ธฐ ์ํ ์ฝ๋์ด๋ค
DataSource๋ TypeORM์์ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ฐ๊ฒฐํ ๋ ์ฌ์ฉ๋๋ ํด๋์ค์ด๋ฉฐ
(์ฐ๊ฒฐ ์ค์ ์ ๋ด๋น)
์ค์ ๋ถ๋ถ์ ์ดํด๋ณด๋ฉด
1. entities ์์ฑ์ TypeORM์ด ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ๋งคํํ ์ํฐํฐ(๋ชจ๋ธ)๋ค์ ์์น๋ฅผ ๋ํ๋ด๊ณ
2. ... ์ฐ์ฐ์ (JavaScript์์ ์ ๊ฐ ์ฐ์ฐ์)๋ฅผ ์ฌ์ฉํ์ฌ configs/db.config.js ํ์ผ์ ํผ์ณ์ ์ธ์๋ก ์ ๋ฌํ๊ณ ์๋ค
const AppDataSource = require("../data-source");
module.exports = AppDataSource.getRepository(require("../models/user.model"));
repositories/user.repository.js
์ด ์ฝ๋๋ TypeORM์์ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ํธ ์์ฉํ ๋ ์ฌ์ฉ๋๋ Repository๋ฅผ ์ป์ด์ค๋ ๋ถ๋ถ์ด๋ค
TypeORM์์ Repository๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค ํ ์ด๋ธ๊ณผ ์ํธ ์์ฉํ๋๋ฐ ์ฌ์ฉ๋๋ ๊ฐ์ฒด๋ก, ํด๋น ํ ์ด๋ธ์ ๋ ์ฝ๋๋ฅผ ๋ง๋ค๊ฑฐ๋ ์กฐํ, ์ ๋ฐ์ดํธ, ์ญ์ ํ๋ ๋ฑ์ ์์ ์ ์ํํ ์ ์๋ค!
data-source.js ํ์ผ์์ ์ ํ๋ฆฌ์ผ์ด์ ์ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฐ๊ฒฐ์ ๋ํ๋ด๋ DataSource ๊ฐ์ฒด๋ฅผ ๊ฐ์ ธ์ค๊ณ
getRepository๋ฅผ ํตํด ํน์ ์ํฐํฐ์ ๋ํ Repository๋ฅผ ์ป์ด์จ๋ค
module.exports๋ฅผ ์ฌ์ฉํ์ฌ ๋ค๋ฅธ ํ์ผ์์ ์ด Repository๋ฅผ ๊ฐ์ ธ์ ์ฌ์ฉํ ์ ์๋๋ก ๋ด๋ณด๋
=> ์ด์ User ๋ชจ๋ธ์ ๋ํ Repository๋ฅผ ๋ค๋ฅธ ํ์ผ์์ ์ฌ์ฉํ ์ ์๊ฒ ๋จ!
CREATE USER 'test'@'localhost' IDENTIFIED WITH mysql_native_password BY '๋น๋ฐ~';
CREATE USER 'test'@'%' IDENTIFIED WITH mysql_native_password BY '๋น๋ฐ~';
CREATE DATABASE `test`;
GRANT ALL PRIVILEGES ON `test`.* TO 'test'@'localhost';
GRANT ALL PRIVILEGES ON `test`.* TO 'test'@'%';
FLUSH PRIVILEGES;
db.sql
MySQL ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์๋ก์ด ์ฌ์ฉ์๋ฅผ ์์ฑํ๊ณ , ํด๋น ์ฌ์ฉ์์๊ฒ ํน์ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ๋ํ ๋ชจ๋ ๊ถํ์ ๋ถ์ฌํ๋ SQL ์คํฌ๋ฆฝํธ์ด๋ค
=> ์ด๋ฌํ ์คํฌ๋ฆฝํธ๋ฅผ ์คํํ๋ฉด 'test' ์ฌ์ฉ์๊ฐ ์์ฑ๋๊ณ , 'test' ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ๋ํ ๊ถํ์ด ๋ถ์ฌ๋๋ค!
(์ดํ์๋ 'test' ์ฌ์ฉ์๋ฅผ ํตํด ํด๋น ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ ๊ทผํ ์ ์๊ฒ ๋จ)
'๐ซ Backend > Node' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Node.js] Nodemailer๋ฅผ ์ฌ์ฉํ์ฌ ํ์๊ฐ์ ์ ์ด๋ฉ์ผ ์ธ์ฆ ๊ตฌํํ๊ธฐ (0) | 2024.05.07 |
---|---|
AWS๋ฅผ ์ด์ฉํ์ฌ Node.js + MongoDB ๋ฐฐํฌํ๊ธฐ [Co-Code] (0) | 2024.04.06 |
CORS๋ ๋ฌด์์ผ๊น? (1) | 2024.02.04 |
JWT ํ ํฐ (1) | 2024.01.31 |