38237-vm/backend/src/db/models/articles.js
2026-02-06 05:28:28 +00:00

162 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 articles = sequelize.define(
'articles',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
title: {
type: DataTypes.TEXT,
},
body: {
type: DataTypes.TEXT,
},
is_published: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
},
published_at: {
type: DataTypes.DATE,
},
last_modified_at: {
type: DataTypes.DATE,
},
is_blocked_by_uk: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
},
uk_comment: {
type: DataTypes.TEXT,
},
reading_time_minutes: {
type: DataTypes.INTEGER,
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
articles.associate = (db) => {
db.articles.belongsToMany(db.article_topics, {
as: 'topics',
foreignKey: {
name: 'articles_topicsId',
},
constraints: false,
through: 'articlesTopicsArticle_topics',
});
db.articles.belongsToMany(db.article_topics, {
as: 'topics_filter',
foreignKey: {
name: 'articles_topicsId',
},
constraints: false,
through: 'articlesTopicsArticle_topics',
});
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
//end loop
db.articles.belongsTo(db.users, {
as: 'author',
foreignKey: {
name: 'authorId',
},
constraints: false,
});
db.articles.hasMany(db.file, {
as: 'cover_image',
foreignKey: 'belongsToId',
constraints: false,
scope: {
belongsTo: db.articles.getTableName(),
belongsToColumn: 'cover_image',
},
});
db.articles.belongsTo(db.users, {
as: 'createdBy',
});
db.articles.belongsTo(db.users, {
as: 'updatedBy',
});
};
return articles;
};