mirror of
https://github.com/ION606/selmerBot.git
synced 2026-05-14 21:26:54 +00:00
48 lines
1.5 KiB
JavaScript
48 lines
1.5 KiB
JavaScript
const Sequelize = require('sequelize');
|
|
|
|
/*
|
|
* Make sure you are on at least version 5 of Sequelize! Version 4 as used in this guide will pose a security threat.
|
|
* You can read more about this issue on the [Sequelize issue tracker](https://github.com/sequelize/sequelize/issues/7310).
|
|
*/
|
|
|
|
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', {
|
|
/* eslint-disable-next-line func-name-matching */
|
|
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', {
|
|
/* eslint-disable-next-line func-name-matching */
|
|
value: function getItems() {
|
|
return UserItems.findAll({
|
|
where: { user_id: this.user_id },
|
|
include: ['item'],
|
|
});
|
|
},
|
|
});
|
|
|
|
module.exports = { Users, CurrencyShop, UserItems };
|