JS

MongoDB 스키마 정의 및 모델로 감싸고, 외부에서 접근 가능하게 하기

isaac.kim 2021. 6. 14.
728x90
반응형

MongoDB 스키마 정의 및 모델로 감싸고, 외부에서 접근 가능하게 하기

 

사이드 프로젝트에서 다음과 같이 MongoDB 스키마를 정의하고,

모델로 감싼 뒤 외부에서 접근이 가능하도록 처리합니다.

const mongoose = require('mongoose');

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

// -- 스키마 정의

// 모델로 스키마를 감싼다
const User = mongoose.model('User', userSchema);

// 외부에서 사용할 수 있는 모듈로
module.exports = { User };

소스코드에 대한 설명

 

1. mongoose 모듈이 필요

2. 스키마 정의

3. 스키마의 모델화

4. 외부에서 사용 가능한 모듈화

 

 

728x90
반응형