user.model.ts 939 Bytes
Newer Older
1
2
3
4
5
6
7
import { model, Schema, Types } from "mongoose";

export interface IUser {
  email: string;
  name?: string;
  password: string;
  role?: Types.ObjectId;
Yoon, Daeki's avatar
Yoon, Daeki committed
8
  avatar?: Types.ObjectId;
Jiwon Yoon's avatar
Jiwon Yoon committed
9
  socialType?: string;
10
11
12
13
14
15
16
}

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

17
18
19
20
const schema = new Schema<IUser>(
  {
    email: {
      type: String,
Yoon, Daeki's avatar
Yoon, Daeki committed
21
      required: true,
22
23
24
25
      unique: true,
      validate: [validateEmail, "이메일을 입력해주세요"],
    },
    name: { type: String },
Jiwon Yoon's avatar
change    
Jiwon Yoon committed
26
    password: { type: String, select: false },
27
    role: { type: Schema.Types.ObjectId, ref: "Role" },
28
    avatar: { type: Schema.Types.ObjectId, ref: "FileInfo" },
Jiwon Yoon's avatar
Jiwon Yoon committed
29
    socialType: { type: String },
30
  },
31
32
33
34
35
36
37
38
39
  {
    toJSON: {
      versionKey: false,
      transform(doc, ret, options) {
        delete ret.password;
      },
    },
  }
);
40
41

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