forked from syxolk/euro2024
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.js
69 lines (59 loc) · 2.34 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
63
64
65
66
67
68
69
const Sequelize = require('sequelize');
const config = require('./config');
const instance = new Sequelize(config.db, {
define: {
freezeTableName: true
},
logging: (process.env.NODE_ENV === "production" ? false : console.log)
});
module.exports.instance = instance;
// Model definitions
const User = instance.define('User', {
facebookId: {type: Sequelize.STRING, unique: true},
googleId: {type: Sequelize.STRING, unique: true},
name: {type: Sequelize.STRING, allowNull: false, validate: {len: [3, 40]}},
email: {type: Sequelize.STRING, unique: true, validate: {isEmail: true}},
password: {type: Sequelize.STRING},
emailConfirmed: {type: Sequelize.BOOLEAN},
emailConfirmToken: {type: Sequelize.UUID, unique: true},
admin: {type: Sequelize.BOOLEAN, allowNull: false, defaultValue: false}
});
const Team = instance.define('Team', {
name: {type: Sequelize.STRING, allowNull: false, validate: {len: [1, 100]}},
code: {type: Sequelize.STRING, allowNull: false, validate: {len: [1,10]}}
}, {
timestamps: false
});
const Match = instance.define('Match', {
goalsHome: Sequelize.INTEGER,
goalsAway: Sequelize.INTEGER,
when: {type: Sequelize.DATE, allowNull: false}
});
const MatchType = instance.define('MatchType', {
code: {type: Sequelize.STRING, allowNull: false, validate: {len: [1, 10]}},
name: {type: Sequelize.STRING, allowNull: false, validate: {len: [1, 100]}}
}, {
timestamps: false
});
const Bet = instance.define('Bet', {
goalsHome: {type: Sequelize.INTEGER, allowNull: false, validate: {min: 0, max: 20}},
goalsAway: {type: Sequelize.INTEGER, allowNull: false, validate: {min: 0, max: 20}}
}, {
indexes : [
{
unique: true,
fields: ['UserId', 'MatchId']
}
]
});
const News = instance.define('News', {
headline: {type: Sequelize.TEXT, allowNull: false}
});
// Associations
Bet.belongsTo(User, {foreignKey: {allowNull: false}, onDelete: 'CASCADE'});
User.hasMany(Bet);
Bet.belongsTo(Match, {foreignKey: {allowNull: false}, onDelete: 'CASCADE'});
Match.hasMany(Bet);
Match.belongsTo(Team, {as: 'HomeTeam', foreignKey: {allowNull: false}, onDelete: 'CASCADE'});
Match.belongsTo(Team, {as: 'AwayTeam', foreignKey: {allowNull: false}, onDelete: 'CASCADE'});
Match.belongsTo(MatchType, {foreignKey: {allowNull: false}, onDelete: 'CASCADE'});