diff --git a/migrations/20220417132721-create-user.js b/migrations/20220417132721-create-user.js index 15c9419..1be8460 100644 --- a/migrations/20220417132721-create-user.js +++ b/migrations/20220417132721-create-user.js @@ -9,7 +9,8 @@ module.exports = { type: Sequelize.INTEGER }, userID: { - type: Sequelize.STRING + type: Sequelize.STRING, + allowNull: false }, banned: { type: Sequelize.BOOLEAN diff --git a/migrations/20220417155941-create-character.js b/migrations/20220417155941-create-character.js new file mode 100644 index 0000000..6b7efbe --- /dev/null +++ b/migrations/20220417155941-create-character.js @@ -0,0 +1,39 @@ +'use strict'; +module.exports = { + async up(queryInterface, Sequelize) { + await queryInterface.createTable('Characters', { + id: { + allowNull: false, + autoIncrement: true, + primaryKey: true, + type: Sequelize.INTEGER + }, + name: { + type: Sequelize.STRING + }, + rarity: { + type: Sequelize.INTEGER + }, + description: { + type: Sequelize.STRING + }, + imageURL: { + type: Sequelize.STRING + }, + imageHash: { + type: Sequelize.STRING + }, + createdAt: { + allowNull: false, + type: Sequelize.DATE + }, + updatedAt: { + allowNull: false, + type: Sequelize.DATE + } + }); + }, + async down(queryInterface, Sequelize) { + await queryInterface.dropTable('Characters'); + } +}; \ No newline at end of file diff --git a/migrations/20220417160003-create-card.js b/migrations/20220417160003-create-card.js new file mode 100644 index 0000000..9f799c8 --- /dev/null +++ b/migrations/20220417160003-create-card.js @@ -0,0 +1,48 @@ +'use strict'; +module.exports = { + async up(queryInterface, Sequelize) { + await queryInterface.createTable('Cards', { + id: { + allowNull: false, + autoIncrement: true, + primaryKey: true, + type: Sequelize.INTEGER + }, + identifier: { + allowNull: false, + type: Sequelize.STRING + }, + characterID: { + allowNull: false, + type: Sequelize.INTEGER, + references: { + model: 'Characters', + key: 'id' + } + }, + ownerID: { + type: Sequelize.INTEGER, + allowNull: false, + references: { + model: 'Users', + key: 'id' + } + }, + enabled: { + allowNull: false, + type: Sequelize.BOOLEAN + }, + createdAt: { + allowNull: false, + type: Sequelize.DATE + }, + updatedAt: { + allowNull: false, + type: Sequelize.DATE + } + }); + }, + async down(queryInterface, Sequelize) { + await queryInterface.dropTable('Cards'); + } +}; \ No newline at end of file diff --git a/models/card.js b/models/card.js new file mode 100644 index 0000000..f21b0b7 --- /dev/null +++ b/models/card.js @@ -0,0 +1,34 @@ +'use strict'; +const { + Model +} = require('sequelize'); +module.exports = (sequelize, DataTypes) => { + class Card extends Model { + /** + * Helper method for defining associations. + * This method is not a part of Sequelize lifecycle. + * The `models/index` file will call this method automatically. + */ + static associate(models) { + Card.belongsTo(models.Character, { + foreignKey: 'characterID', + as: 'character' + }); + Card.belongsTo(models.User, { + foreignKey: 'ownerID', + key: 'userId', + as: 'owner' + }); + } + } + Card.init({ + ownerID: DataTypes.STRING, + identifier: DataTypes.STRING, + characterID: DataTypes.INTEGER, + enabled: { type: DataTypes.BOOLEAN, defaultValue: true } + }, { + sequelize, + modelName: 'Card', + }); + return Card; +}; \ No newline at end of file diff --git a/models/character.js b/models/character.js new file mode 100644 index 0000000..2c773d5 --- /dev/null +++ b/models/character.js @@ -0,0 +1,30 @@ +'use strict'; +const { + Model +} = require('sequelize'); +module.exports = (sequelize, DataTypes) => { + class Character extends Model { + /** + * Helper method for defining associations. + * This method is not a part of Sequelize lifecycle. + * The `models/index` file will call this method automatically. + */ + static associate(models) { + Character.hasMany(models.Card, { + foreignKey: 'characterID', + as: 'cards' + }); + } + } + Character.init({ + name: DataTypes.STRING, + rarity: DataTypes.INTEGER, + description: DataTypes.STRING, + imageURL: DataTypes.STRING, + imageHash: DataTypes.STRING + }, { + sequelize, + modelName: 'Character', + }); + return Character; +}; \ No newline at end of file