29870/backend/src/db/models/foodimals.js
2025-03-13 22:48:16 +00:00

118 lines
2.4 KiB
JavaScript

const config = require('../../config');
const providers = config.providers;
const crypto = require('crypto');
const bcrypt = require('bcrypt');
const moment = require('moment');
module.exports = function (sequelize, DataTypes) {
const foodimals = sequelize.define(
'foodimals',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
name: {
type: DataTypes.TEXT,
},
description: {
type: DataTypes.TEXT,
},
elemental_power: {
type: DataTypes.ENUM,
values: ['Fire', 'Water', 'Earth', 'Air'],
},
health: {
type: DataTypes.INTEGER,
},
attack: {
type: DataTypes.INTEGER,
},
defense: {
type: DataTypes.INTEGER,
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
foodimals.associate = (db) => {
db.foodimals.belongsToMany(db.abilities, {
as: 'abilities',
foreignKey: {
name: 'foodimals_abilitiesId',
},
constraints: false,
through: 'foodimalsAbilitiesAbilities',
});
db.foodimals.belongsToMany(db.abilities, {
as: 'abilities_filter',
foreignKey: {
name: 'foodimals_abilitiesId',
},
constraints: false,
through: 'foodimalsAbilitiesAbilities',
});
db.foodimals.belongsToMany(db.moves, {
as: 'moves',
foreignKey: {
name: 'foodimals_movesId',
},
constraints: false,
through: 'foodimalsMovesMoves',
});
db.foodimals.belongsToMany(db.moves, {
as: 'moves_filter',
foreignKey: {
name: 'foodimals_movesId',
},
constraints: false,
through: 'foodimalsMovesMoves',
});
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
//end loop
db.foodimals.hasMany(db.file, {
as: 'image',
foreignKey: 'belongsToId',
constraints: false,
scope: {
belongsTo: db.foodimals.getTableName(),
belongsToColumn: 'image',
},
});
db.foodimals.belongsTo(db.users, {
as: 'createdBy',
});
db.foodimals.belongsTo(db.users, {
as: 'updatedBy',
});
};
return foodimals;
};