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

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