๐Ÿ“ฆ SeolJaeHyeok / My-shopping-mall

๐Ÿ“„ user-model.js ยท 40 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40import { model } from "mongoose";
import { UserSchema } from "../schemas/user-schema";

const User = model("users", UserSchema);

export class UserModel {
  async findByEmail(email) {
    return await User.findOne({ email });
  }

  async findById(_id) {
    return await User.findOne({ _id });
  }

  async findAll() {
    return await User.find({});
  }

  async create(userInfo) {
    return await User.create(userInfo);
  }

  async update({ userId, update }) {
    const filter = { _id: userId };
    const option = { returnOriginal: false };

    return await User.findOneAndUpdate(filter, update, option);
  }

  async deleteById(_id) {
    return await User.findOneAndDelete({ _id });
  }

  async deleteByEmail(email) {
    return await User.findOneAndDelete({ email });
  }
}

export const userModel = new UserModel();