generated from TeamMrWeb/graphql-mongoose-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUser.js
76 lines (71 loc) · 1.68 KB
/
User.js
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
const { Schema, model } = require('mongoose')
const { hashPassword } = require('../../services/crypt.service')
const userSchema = new Schema(
{
username: {
type: String,
required: true,
unique: true,
trim: true,
minlength: 3,
maxlength: 24,
},
email: {
type: String,
required: true,
unique: true,
trim: true,
match: [
/^[a-z0-9.]{1,64}@[a-z0-9.]{1,64}$/i,
'Please a valid email address',
],
},
password: {
type: String,
required: true,
trim: true,
minlength: 8,
maxlength: 64,
},
about: {
type: String,
trim: true,
maxlength: 128,
},
role: {
type: String,
enum: ['user', 'admin'],
default: 'user',
},
verified: {
type: Boolean,
default: false,
},
avatar: {
secure_url: {
type: String,
default: null,
},
public_id: {
type: String,
default: null,
},
},
},
{
timestamps: true,
versionKey: false,
},
)
userSchema.virtual('id').get(function () {
return this._id.toHexString()
})
userSchema.set('toJSON', {
virtuals: true,
})
userSchema.pre('save', async function (next) {
if (this.isModified('password'))
this.password = await hashPassword(this.password)
next()
})
module.exports = model('User', userSchema)