2026-04-05 20:39:04 +00:00

135 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 news = sequelize.define(
'news',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
title: {
type: DataTypes.TEXT,
},
text: {
type: DataTypes.TEXT,
},
published_at: {
type: DataTypes.DATE,
},
moderation_status: {
type: DataTypes.ENUM,
values: [
"pending",
"approved",
"rejected"
],
},
moderation_comment: {
type: DataTypes.TEXT,
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
news.associate = (db) => {
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
//end loop
db.news.belongsTo(db.users, {
as: 'submitted_by',
foreignKey: {
name: 'submitted_byId',
},
constraints: false,
});
db.news.hasMany(db.file, {
as: 'image',
foreignKey: 'belongsToId',
constraints: false,
scope: {
belongsTo: db.news.getTableName(),
belongsToColumn: 'image',
},
});
db.news.belongsTo(db.users, {
as: 'createdBy',
});
db.news.belongsTo(db.users, {
as: 'updatedBy',
});
};
return news;
};