34039/backend/src/db/models/stories.js
2025-09-13 00:11:33 +00:00

76 lines
1.5 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 stories = sequelize.define(
'stories',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
title: {
type: DataTypes.TEXT,
},
posted_at: {
type: DataTypes.DATE,
},
expires_at: {
type: DataTypes.DATE,
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
stories.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.stories.belongsTo(db.users, {
as: 'author',
foreignKey: {
name: 'authorId',
},
constraints: false,
});
db.stories.hasMany(db.file, {
as: 'media',
foreignKey: 'belongsToId',
constraints: false,
scope: {
belongsTo: db.stories.getTableName(),
belongsToColumn: 'media',
},
});
db.stories.belongsTo(db.users, {
as: 'createdBy',
});
db.stories.belongsTo(db.users, {
as: 'updatedBy',
});
};
return stories;
};