프론트엔드/javaScript, jQuery

[mongoose] 스키마 Schemas, 모델 Model

celine_e 2023. 2. 1. 14:22

1. 스키마(Schemas) 란 ?

데이터를 매핑하는데 사용되는 하나하나의 정보를 지정해 주는 역할을 합니다.  스키마에서 알 수 있는 정보는 데이터의 형식, 최소 길이, 최대 길이, 공백 등등 데이터의 정보를 알 수 있습니다. 

const userSchema = mongoose.Schema({
    name :{
            type : String,
            maxlength : 50
    },
    email : {
            type: String,
            trim: true,
            unique :1
    },
    password : {
        type: String,
        maxlength : 70
    },
    lastname : {
        type: String,
        maxlength:50
    },
    role : {
        type : Number,
        default : 0
    },
    image : String,
    token : {
        type: String
    },
    //토큰을 이용해서 유효성을 관리 
    tokenExp : {
        type : Number 
    }
    //유효기간, 토큰이 사용할 수 있는 기간을 주는 것 

})

 

 

2. Model 이란 ?

스키마를 감싸는 역할을 합니다. 

index.js 에서 사용할 수 있게 export 시켜줍니다.

const User = mongoose.model('User', userSchema )
module.exports = {User}

 

 

https://mongoosejs.com/docs/guide.html#schemas

 

Mongoose v6.9.0: Schemas

If you haven't yet done so, please take a minute to read the quickstart to get an idea of how Mongoose works. If you are migrating from 5.x to 6.x please take a moment to read the migration guide. Everything in Mongoose starts with a Schema. Each schema ma

mongoosejs.com


DB , 데이터 베이스 누군가 얘기했을 때 알아 들을 수 있게 개념은 알아두도록 하자!