39434-vm/backend/src/db/models/sections.js
2026-04-02 11:02:09 +00:00

165 lines
2.0 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 sections = sequelize.define(
'sections',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
title: {
type: DataTypes.TEXT,
},
section_number: {
type: DataTypes.TEXT,
},
slug: {
type: DataTypes.TEXT,
},
content: {
type: DataTypes.TEXT,
},
status: {
type: DataTypes.ENUM,
values: [
"draft",
"review",
"published",
"archived"
],
},
sort_order: {
type: DataTypes.INTEGER,
},
published_at: {
type: DataTypes.DATE,
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
sections.associate = (db) => {
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
db.sections.hasMany(db.study_notes, {
as: 'study_notes_section',
foreignKey: {
name: 'sectionId',
},
constraints: false,
});
//end loop
db.sections.belongsTo(db.chapters, {
as: 'chapter',
foreignKey: {
name: 'chapterId',
},
constraints: false,
});
db.sections.hasMany(db.file, {
as: 'attachments',
foreignKey: 'belongsToId',
constraints: false,
scope: {
belongsTo: db.sections.getTableName(),
belongsToColumn: 'attachments',
},
});
db.sections.belongsTo(db.users, {
as: 'createdBy',
});
db.sections.belongsTo(db.users, {
as: 'updatedBy',
});
};
return sections;
};