user.model.ts 744 Bytes
Newer Older
Yoon, Daeki's avatar
Yoon, Daeki committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import { model, Schema, Types } from "mongoose";

export interface IUser {
  email: string;
  name?: string;
  password: string;
  role?: Types.ObjectId;
}

const validateEmail = (email: string) => {
  const re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
  return re.test(email);
};

const schema = new Schema<IUser>({
  email: {
Kim, MinGyu's avatar
Kim, MinGyu committed
17
    type: String,//mongoose type 인 String 으로 일반적인 string 과는 겉으로는 대문자 차이
Yoon, Daeki's avatar
Yoon, Daeki committed
18
19
20
21
22
23
24
25
26
27
    rquired: true,
    unique: true,
    validate: [validateEmail, "이메일을 입력해주세요"],
  },
  name: { type: String },
  password: { type: String, required: true, select: false },
  role: { type: Schema.Types.ObjectId, ref: "Role" },
});

export default model<IUser>("User", schema);