-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbObjects.js
40 lines (32 loc) · 1.11 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
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 CurrencyShop = require("./models/CurrencyShop.js")(sequelize, Sequelize.DataTypes);
const UserItems = require("./models/UserItems.js")(sequelize, Sequelize.DataTypes);
UserItems.belongsTo(CurrencyShop, { foreignKey: "item_id", as: "item" });
Reflect.defineProperty(Users.prototype, "addItem", {
value: async function addItem(item) {
const userItem = await UserItems.findOne({
where: { user_id: this.user_id, item_id: item.id },
});
if (userItem) {
userItem.amount += 1;
return userItem.save();
}
return UserItems.create({ user_id: this.user_id, item_id: item.id, amount: 1 });
},
});
Reflect.defineProperty(Users.prototype, "getItems", {
value: function getItems() {
return UserItems.findAll({
where: { user_id: this.user_id },
include: ["item"]
});
},
});
module.exports = { Users, CurrencyShop, UserItems };