user.model.ts 1.12 KB
Newer Older
1
import { model, Schema, Types, version } from "mongoose";
Yoon, Daeki's avatar
Yoon, Daeki committed
2
3
4
5
6
7

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

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

19
20
21
22
23
24
25
26
27
const schema = new Schema<IUser>(
  {
    email: {
      type: String, //mongoose type인 String으로 일반적인 string과는 겉으로는 대문자 차이
      rquired: true,
      unique: true,
      validate: [validateEmail, "이메일을 입력해주세요"],
    },
    name: { type: String },
28
29
30
31
    originalfilename: { type: String },
    newfilename: { type: String },
    nickname: { type: String },
    picturepath: { type: String },
32
33
    password: { type: String, required: true, select: false },
    role: { type: Schema.Types.ObjectId, ref: "Role" },
Yoon, Daeki's avatar
Yoon, Daeki committed
34
  },
35
36
37
38
39
40
41
42
43
  {
    toJSON: {
      versionKey: false,
      transform(doc, ret, options) {
        delete ret.password;
      },
    },
  }
);
Yoon, Daeki's avatar
Yoon, Daeki committed
44
45

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