d-_-s 2023. 2. 12. 12:50

Model이란?

Schema를 감싸주는 역할이다.

 

Schema란?

하나하나의 정보를 지정해 줄 수 있는 것이다.

 

User.js 생성

프로젝트에 models라는 이름의 폴더를 만들고 User.js라는 파일을 생성한다.

// mongoose를 요청함
const mongoose = require('mongoose');

// Schema 설정
const userSchema = mongoose.Schema({
    name: {
        type: String,
        maxlength: 50
    },
    email: {
        type: String,
        trim: true,
        unique: 1
    },
    password: {
        type: String,
        minlength: 5
    },
    lastname: {
        tpye: String,
        maxlength: 50
    },
    role: {
        type: Number,
        default: 0
    },
    image: String,
    token: {
        type: String
    },
    tokenExp: {
        type: Number
    }
})

// Schema를 모듈로 감싸 줌
const User = mongoose.model('User', userSchema)

// 모듈을 다른 곳에서도 사용할 수 있도록 하는 코드
module.exports = {User}