node.JS
[Node.js] Mongoose schema 만들기
worri-pi
2021. 12. 28. 18:08
mongoose는 테이블이 없어 오류가 많이 생기는 mongoDB나 nosql을 위해 schema를 도입했다. 사용자가 작성한 schema를 기준으로 데이터를 DB에 넣기 전에 먼저 검사를 하여 기준과 다른 데이터가 있으면 에러를 발생시킨다.
1.schema 만들기
const mongoose = require('mongoose'); //mongoose module 가져오기
const userSchema = mongoose.Schema({ //mongoose를 이용하여 schema 생성
name:{
type: String,
maxlength: 50
},
email:{
type: String,
trim: true, //빈칸 제거
unique: 1
},
password: {
type: String,
maxlength: 5
},
role: {
type: Number,
default: 0
},
image: String,
token:{
type: String
},
tokenExp: {
type: Number
}
})
const User = mongoose.model('User', userSchema) //스키마 등록 //model(model이름,schema)
module.exports = { User } //model 을 다른 파일에서도 쓰기 위해 export하기
출처 : https://www.youtube.com/watch?v=c92IF8Dtu0o
728x90