-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbObjects.js
56 lines (45 loc) · 1.58 KB
/
dbObjects.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
const Sequelize = require('sequelize');
const sequelize = new Sequelize('database', 'username', 'password', {
host: 'localhost',
dialect: 'sqlite',
logging: false,
storage: 'database.sqlite',
});
const Users = require('./models/users.js')(sequelize, Sequelize.DataTypes);
const Games = require('./models/games.js')(sequelize, Sequelize.DataTypes);
const LoggedGames = require('./models/beatenGames.js')(sequelize, Sequelize.DataTypes);
const Changelog = require('./models/changelog.js')(sequelize, Sequelize.DataTypes);
// Create userId foreignKey in LoggedGames
Users.hasMany(LoggedGames);
LoggedGames.belongsTo(Users);
// Create gameId foreignKey in LoggedGames
Games.hasMany(LoggedGames);
LoggedGames.belongsTo(Games);
// Create userId foreignKey in Changelog
Users.hasMany(Changelog);
Changelog.belongsTo(Users);
// Create gameId foreignKey in Changelog
Games.hasMany(Changelog);
Changelog.belongsTo(Games);
Reflect.defineProperty(Users.prototype, 'addUser', {
value: async function addUser(userData) {
const user = await Users.findOne({
where: { user_id: userData.id },
});
if (!user) {
return Users.create({ user_id: userData.discord_id, username: userData.username });
}
},
});
Reflect.defineProperty(Users.prototype, 'getUser', {
value: function getUser(userData) {
return Users.findAll({
where: { user_id: userData.discord_id },
});
},
});
sequelize.sync({ alter: true })
.catch((err) => {
console.log(err);
});
module.exports = { sequelize, Users, Games, LoggedGames, Changelog };