FILOT 채팅앱 만들기 - 1 (Model)

원종서·2021년 9월 16일
0

chattingApp

목록 보기
1/4

FILOT 채팅앱 만들기 -1

컴퓨터 전공으로써 첫번째 프로젝트 실시간 채팅앱 만들기.
본격적으로 프로젝트를 시작한 것은 거의 2달이 다 되어가지만
과거 기억을 되살려 가며 하나하나 기록해 보기로 했다.

back - nodeJs(express) / mongoDB(mongoose) / socketIo

front - flutter


User Model


const userSchema = new mongoose.Schema({
    id: {type: String, required: true, unique: true},
    password: {type: String, required: true},
    name: {type: String, required: true},
    rooms: [{type: mongoose.Schema.Types.ObjectId, ref: "ChatsRoom"}],
    phone_number: {type: String, default: "null"},
    state: {type: Number, default: 0},
    role: {type: String, default: "null"},
    github: {type: String, default: "null"},
    email: {type: String, default: "null"},
});

  • default 값에 고의적으로 "null"을 넣은 이유는 나중 프론트에서 유저 개개인의 상태를 나타날때 null 값으로 인한 오류를 방지하기 위함이다.

  • ChatsRoom 모델과 mongoose로 관계를 맺어주었다. Chat 모델과도 관계를 맺어줄까 고민하다 크게 필요 없을 거 같아 넣지 않았다.


Chat Model


const chatSchema = new mongoose.Schema(
    {
        message: {type: String, required: true},
        chatRoom: {
            type: mongoose.Schema.Types.ObjectId,
            required: true,
            ref: "ChatsRoom",
        },
        user: {type: String, required: true},
    },
    {timestamps: true}
);
  • 추후에 채팅방에서 프로필 사진을 눌러서 유저들의 정보를 얻어오기 쉽게 하기 위해 ChatsRoom 모델과 관계를 맺어주었다.

  • User 모델과 관계를 맺어주지 않고 string으로 name 을 저장해주기로 했다. 이유는 불필요한 데이터베이스 호출이라 생각해서인데, 지금 생각해보면 User 와 관계를 맺어줘도 됐을 것 같다.


ChatsRoom

const chatsRoomSchema = new mongoose.Schema(
    {
        roomNum: String,
        user: [
            {type: mongoose.Schema.Types.ObjectId, required: true, ref: "User"},
        ],
        chats: [
            {type: mongoose.Schema.Types.ObjectId, required: true, ref: "Chat"},
        ],
    },
    {
        timestamps: true,
    }
);
  • 채팅방 모델을 구현하며 timestamps: true 라는 기능을 알게 되었다.

0개의 댓글