-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.js
68 lines (58 loc) · 1.43 KB
/
example.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
const Sequelize = require('sequelize');
const bcrypt = require('bcrypt');
const {DataTypes, Op} = Sequelize
const sequelize = new Sequelize('demo', 'root', 'Sunday40@', {
dialect: 'mysql'
})
const Students = sequelize.define( 'student', {
student_id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
studentName: {
type: DataTypes.STRING,
},
department: {
type: DataTypes.STRING,
},
course: {
type: DataTypes.STRING
},
level: {
type: DataTypes.INTEGER,
validate: {
set(value){
if (value < 300) {
console.log('You are undergraduate!!!')
} else {
console.log('You will soon graduate... work hard')
}
}
}
},
password: {
type: DataTypes.STRING,
validate: {
set(value){
const salt = bcrypt.genSaltSync(10);
const hash = bcrypt.hashSync(value, salt);
this.setDataValue('password', hash)
}
}
}
}, {
timestamps: true,
freezeTypeName: true,
paranoid: true,
deletedAt: 'timeDestroyed'
})
sequelize.sync({ alter: true }).then(() => {
return Students.destroy({ where: {
student_id: 16,
}})
}).then((data) => {
console.log(data)
}).catch((e) => {
console.log(e)
})