30872/backend/src/db/models/sports.js
2025-04-20 09:08:54 +00:00

72 lines
1.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 sports = sequelize.define(
'sports',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
name: {
type: DataTypes.TEXT,
},
description: {
type: DataTypes.TEXT,
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
sports.associate = (db) => {
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
db.sports.hasMany(db.grounds, {
as: 'grounds_sport',
foreignKey: {
name: 'sportId',
},
constraints: false,
});
//end loop
db.sports.hasMany(db.file, {
as: 'image',
foreignKey: 'belongsToId',
constraints: false,
scope: {
belongsTo: db.sports.getTableName(),
belongsToColumn: 'image',
},
});
db.sports.belongsTo(db.users, {
as: 'createdBy',
});
db.sports.belongsTo(db.users, {
as: 'updatedBy',
});
};
return sports;
};