-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.js
62 lines (55 loc) · 1.45 KB
/
models.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
module.exports = async function () {
const chalk = require('chalk')
const MySQL = require('./database/db')
const db = new MySQL()
// creating user model
class User extends db.Model { }
User.init({
username: {
type: db.DataTypes.STRING,
allowNull: false,
unique: !0,
validate: {
len: {
args: [3, 15],
msg: 'Username must be between 3-15 characters!'
}
}
},
highScore: {
type: db.DataTypes.MEDIUMINT,
defaultValue: 0
}
}, {
tableName: 'users',
sequelize: db.sequelize,
freezeTableName: true
})
// creating game model
class Game extends db.Model { }
Game.init({
correct: {
type: db.DataTypes.INTEGER,
defaultValue: 0
},
incorrect: {
type: db.DataTypes.INTEGER,
defaultValue: 0
},
percentage: {
type: db.DataTypes.FLOAT,
defaultValue: 0
}
}, {
tableName: 'games',
sequelize: db.sequelize,
freezeTableName: true
})
// one to many relationship
// a user can have many game records
User.hasMany(Game, { constraints: false })
Game.belongsTo(User)
await db.sequelize.sync()
console.log(chalk.greenBright('synced all models'))
return { User, Game, db }
}