38122-vm/backend/src/db/models/publications.js
2026-02-02 20:42:56 +00:00

146 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 publications = sequelize.define(
'publications',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
title: {
type: DataTypes.TEXT,
},
abstract: {
type: DataTypes.TEXT,
},
publication_date: {
type: DataTypes.DATE,
},
doi: {
type: DataTypes.TEXT,
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
publications.associate = (db) => {
db.publications.belongsToMany(db.users, {
as: 'authors',
foreignKey: {
name: 'publications_authorsId',
},
constraints: false,
through: 'publicationsAuthorsUsers',
});
db.publications.belongsToMany(db.users, {
as: 'authors_filter',
foreignKey: {
name: 'publications_authorsId',
},
constraints: false,
through: 'publicationsAuthorsUsers',
});
db.publications.belongsToMany(db.research_domains, {
as: 'topics',
foreignKey: {
name: 'publications_topicsId',
},
constraints: false,
through: 'publicationsTopicsResearch_domains',
});
db.publications.belongsToMany(db.research_domains, {
as: 'topics_filter',
foreignKey: {
name: 'publications_topicsId',
},
constraints: false,
through: 'publicationsTopicsResearch_domains',
});
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
//end loop
db.publications.hasMany(db.file, {
as: 'file',
foreignKey: 'belongsToId',
constraints: false,
scope: {
belongsTo: db.publications.getTableName(),
belongsToColumn: 'file',
},
});
db.publications.belongsTo(db.users, {
as: 'createdBy',
});
db.publications.belongsTo(db.users, {
as: 'updatedBy',
});
};
return publications;
};