Add card and character tables

This commit is contained in:
2022-04-17 19:38:50 +02:00
parent 0419733925
commit 9f568dbbdb
5 changed files with 153 additions and 1 deletions

View File

@@ -9,7 +9,8 @@ module.exports = {
type: Sequelize.INTEGER type: Sequelize.INTEGER
}, },
userID: { userID: {
type: Sequelize.STRING type: Sequelize.STRING,
allowNull: false
}, },
banned: { banned: {
type: Sequelize.BOOLEAN type: Sequelize.BOOLEAN

View File

@@ -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');
}
};

View File

@@ -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');
}
};

34
models/card.js Normal file
View File

@@ -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;
};

30
models/character.js Normal file
View File

@@ -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;
};