Skip to content

Commit

Permalink
[init] 시퀄라이즈 기초 세팅 완료
Browse files Browse the repository at this point in the history
  • Loading branch information
ezi-s-u committed Feb 5, 2024
1 parent aa79322 commit b2cb505
Show file tree
Hide file tree
Showing 7 changed files with 2,293 additions and 0 deletions.
41 changes: 41 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const express = require('express');
const session = require('express-session');
const cors = require('cors');
const multer = require('multer');
const sequelize = require("./config/config");

const path = require('path');
const bodyParse = require('body-parser')

const app = express();
const port = 3000;

// routes
const userRouter = require('./routes/user')

// app.get('/', (req, res) => {
// return res.send("홀로서기");
// })

sequelize.sync()
.then(() => {
console.log('Database synced')
})
.catch((err) => {
console.error('Error syncing database:', err)
});

// 미들웨어
app.use(cors());
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(require('cookie-parser')());
app.use(bodyParse.json())

// router - 진입할 엔드포인트 + 진입할 라우터
app.use('/users', userRouter)

app.listen(port, () => {
console.log(port, '번 포트에서 대기 중');
});
21 changes: 21 additions & 0 deletions config/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// db랑 시퀄라이즈랑 연결하는 역할
const Sequelize = require('sequelize');
require('dotenv').config();

const sequelize = new Sequelize(process.env.MYSQL_DATABASE, process.env.MYSQL_USERNAME, process.env.MYSQL_PASSWORD, {
host: process.env.MYSQL_HOST,
port: process.env.MYSQL_PORT,
dialect: 'mysql',
})

// 연결 테스트
sequelize
.authenticate()
.then(() => {
console.log('Connected to the config');
})
.catch((err) => {
console.error('Unable to connect to the config:', err);
});

module.exports = sequelize;
9 changes: 9 additions & 0 deletions models/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// module export
// const로 상수 선언
const sequelize = require('../config/config')

const User = require('./user')(sequelize);// sequelize가 db 정보를 가지고 있으므로 전달해주면 얘가 만들어봄

module.exports = {
User,
}
36 changes: 36 additions & 0 deletions models/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// models/User.js
const { DataTypes } = require('sequelize');

const User = (sequelize) => sequelize.define('users', {
id: {
type: DataTypes.BIGINT,
primaryKey: true,
allowNull: false,
autoIncrement: true,
unique: true,
},
user_id: {
type: DataTypes.STRING,
allowNull: false,
unique: true,
},
name: {
type: DataTypes.STRING,
allowNull: false,
unique: true,
},
password: {
type: DataTypes.STRING,
allowNull: false,
},
email: {
type: DataTypes.STRING,
allowNull: false,
unique: true,
validate: {
isEmail: true,
},
}
});

module.exports = User;
Loading

0 comments on commit b2cb505

Please sign in to comment.