29714/backend/src/db/models/forums.js
2025-03-08 07:22:41 +00:00

84 lines
1.7 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 forums = sequelize.define(
'forums',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
subject: {
type: DataTypes.TEXT,
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
forums.associate = (db) => {
db.forums.belongsToMany(db.posts, {
as: 'posts',
foreignKey: {
name: 'forums_postsId',
},
constraints: false,
through: 'forumsPostsPosts',
});
db.forums.belongsToMany(db.posts, {
as: 'posts_filter',
foreignKey: {
name: 'forums_postsId',
},
constraints: false,
through: 'forumsPostsPosts',
});
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
db.forums.hasMany(db.posts, {
as: 'posts_forum',
foreignKey: {
name: 'forumId',
},
constraints: false,
});
//end loop
db.forums.belongsTo(db.companies, {
as: 'companies',
foreignKey: {
name: 'companiesId',
},
constraints: false,
});
db.forums.belongsTo(db.users, {
as: 'createdBy',
});
db.forums.belongsTo(db.users, {
as: 'updatedBy',
});
};
return forums;
};