DB: Add models and migration for badges
This commit is contained in:
41
migrations/20230226202413-create-badge.js
Normal file
41
migrations/20230226202413-create-badge.js
Normal file
@@ -0,0 +1,41 @@
|
||||
'use strict';
|
||||
/** @type {import('sequelize-cli').Migration} */
|
||||
module.exports = {
|
||||
async up(queryInterface, Sequelize) {
|
||||
await queryInterface.createTable('Badges', {
|
||||
id: {
|
||||
allowNull: false,
|
||||
autoIncrement: true,
|
||||
primaryKey: true,
|
||||
type: Sequelize.INTEGER
|
||||
},
|
||||
name: {
|
||||
type: Sequelize.STRING
|
||||
},
|
||||
description: {
|
||||
type: Sequelize.STRING
|
||||
},
|
||||
image: {
|
||||
type: Sequelize.STRING,
|
||||
allowNull: true
|
||||
},
|
||||
createdAt: {
|
||||
allowNull: false,
|
||||
type: Sequelize.DATE,
|
||||
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP')
|
||||
},
|
||||
updatedAt: {
|
||||
allowNull: false,
|
||||
type: Sequelize.DATE,
|
||||
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP')
|
||||
}
|
||||
});
|
||||
},
|
||||
async down(queryInterface, Sequelize) {
|
||||
await queryInterface.sequelize.query('ALTER TABLE BadgeCharacter DROP FOREIGN KEY BadgeCharacter_ibfk_1;');
|
||||
await queryInterface.sequelize.query('ALTER TABLE BadgeCharacter DROP FOREIGN KEY BadgeCharacter_ibfk_2;');
|
||||
await queryInterface.sequelize.query('ALTER TABLE BadgeUser DROP FOREIGN KEY BadgeUser_ibfk_1;');
|
||||
await queryInterface.sequelize.query('ALTER TABLE BadgeUser DROP FOREIGN KEY BadgeUser_ibfk_2;');
|
||||
await queryInterface.dropTable('Badges');
|
||||
}
|
||||
};
|
||||
24
models/badge.js
Normal file
24
models/badge.js
Normal file
@@ -0,0 +1,24 @@
|
||||
'use strict';
|
||||
const {
|
||||
Model
|
||||
} = require('sequelize');
|
||||
module.exports = (sequelize, DataTypes) => {
|
||||
class Badge extends Model {
|
||||
static associate(models) {
|
||||
// A badge can belong to many cards
|
||||
Badge.belongsToMany(models.Character, { through: 'BadgeCharacter' });
|
||||
|
||||
// A badge can belong to many users
|
||||
Badge.belongsToMany(models.User, { through: 'BadgeUser' });
|
||||
}
|
||||
}
|
||||
Badge.init({
|
||||
name: DataTypes.STRING,
|
||||
description: DataTypes.STRING,
|
||||
image: DataTypes.STRING
|
||||
}, {
|
||||
sequelize,
|
||||
modelName: 'Badge',
|
||||
});
|
||||
return Badge;
|
||||
};
|
||||
@@ -11,6 +11,8 @@ module.exports = (sequelize, DataTypes) => {
|
||||
*/
|
||||
static associate(models) {
|
||||
Character.belongsTo(models.Group, { foreignKey: 'groupId', });
|
||||
// A character can belong to many badges
|
||||
Character.belongsToMany(models.Badge, { through: 'BadgeCharacter' });
|
||||
}
|
||||
}
|
||||
Character.init({
|
||||
|
||||
@@ -17,6 +17,8 @@ module.exports = (sequelize, DataTypes) => {
|
||||
User.hasMany(models.Card);
|
||||
User.hasMany(models.CurrencyHistory);
|
||||
User.hasOne(models.Profile);
|
||||
// A user can have many badges
|
||||
User.belongsToMany(models.Badge, { through: 'BadgeUser' });
|
||||
}
|
||||
async addExperience(amount, source='unknown') {
|
||||
console.log(`Adding ${amount} experience to user ${this.id}`);
|
||||
|
||||
Reference in New Issue
Block a user