Skip to content
Snippets Groups Projects
Commit d8244142 authored by tpgus2603's avatar tpgus2603
Browse files

feat 엔티티모델 정의 및 DB연결 테스트완료 (#3)

parent b5a119e7
Branches
No related tags found
2 merge requests!5[#1#2#3] DB모델 객체 및 로그인 ,회원가입 구현,!2Feature/#3
const mongoose = require('mongoose');
// MongoDB 연결
mongoose.connect('mongodb://localhost:27017/chatDB', {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => console.log('MongoDB 연결 성공'))
.catch(err => console.error('MongoDB 연결 실패:', err));
// 위에서 정의한 ChatRoom, ChatRoomParticipant, Message 모델을 사용
const ChatRoom = require('./models/ChatRoom');
const ChatRoomParticipant = require('./models/ChatRoomParticipant');
const Message = require('./models/Message');
// 예시: 채팅방 생성
async function createChatRoom(data) {
try {
const chatRoom = new ChatRoom(data);
await chatRoom.save();
console.log('채팅방 생성 완료:', chatRoom);
} catch (error) {
console.error('채팅방 생성 실패:', error);
}
}
// 예시 함수 호출
createChatRoom({
name: '일반 채팅방',
type: 'OPEN',
created_by: 1, // 관계형 DB의 Users.id와 일치
});
sync.js 0 → 100644
// sync.js
require('dotenv').config(); // 환경 변수 로드
const sequelize = require('./config/sequelize');
const model=require('./models'); // 모델들을 가져옴 (사이드 이펙트로 모델들이 등록됨)
async function syncDatabase() {
try {
// 데이터베이스 연결 테스트
await sequelize.authenticate();
console.log('데이터베이스 연결 성공.');
// 모든 모델 동기화
await sequelize.sync({ force: true });
console.log('모든 모델이 성공적으로 동기화되었습니다.');
// 연결 종료
await sequelize.close();
console.log('데이터베이스 연결이 종료되었습니다.');
} catch (error) {
console.error('데이터베이스 연결 실패:', error);
}
}
syncDatabase();
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment