forked from Flex-Armenian-Fox/kanban-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.js
54 lines (54 loc) · 1.28 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
'use strict';
const {hash} = require('../helpers/brcypt')
const {
Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
class User extends Model {
/**
* Helper method for defining associations.
* This method is not a part of Sequelize lifecycle.
* The `models/index` file will call this method automatically.
*/
static associate(models) {
// define association here
this.hasMany(models.Task, {foreignKey: "UserId"})
}
};
User.init({
email: {
type: DataTypes.STRING,
unique: {
msg: 'email is already taken'
},
validate: {
notEmpty: {msg: "Email cannot be empty"},
isEmail: {msg: "Please use proper email format"}
},
},
password: {
type: DataTypes.STRING,
validate: {
notEmpty: {msg: "Password cannot be empty"},
len: {
args: [4, 32],
msg: "Password must be between 4 to 32 characters"
}
}
},
alias: {
type: DataTypes.STRING
}
}, {
hooks:{
beforeCreate: user =>{
user.password = hash(user.password)
user.email = user.email.toLowerCase()
if (!user.alias) {user.alias = user.email}
}
},
sequelize,
modelName: 'User',
});
return User;
};