39520-vm/backend/src/db/models/download_bundles.js
2026-04-08 11:29:29 +00:00

221 lines
3.1 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 download_bundles = sequelize.define(
'download_bundles',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
title: {
type: DataTypes.TEXT,
},
bundle_type: {
type: DataTypes.ENUM,
values: [
"single_story",
"collection",
"all_stories",
"urban_legends",
"mixed"
],
},
format: {
type: DataTypes.ENUM,
values: [
"txt",
"pdf",
"epub",
"zip"
],
},
status: {
type: DataTypes.ENUM,
values: [
"queued",
"building",
"ready",
"failed"
],
},
requested_at: {
type: DataTypes.DATE,
},
ready_at: {
type: DataTypes.DATE,
},
download_count: {
type: DataTypes.INTEGER,
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
download_bundles.associate = (db) => {
db.download_bundles.belongsToMany(db.scary_stories, {
as: 'stories',
foreignKey: {
name: 'download_bundles_storiesId',
},
constraints: false,
through: 'download_bundlesStoriesScary_stories',
});
db.download_bundles.belongsToMany(db.scary_stories, {
as: 'stories_filter',
foreignKey: {
name: 'download_bundles_storiesId',
},
constraints: false,
through: 'download_bundlesStoriesScary_stories',
});
db.download_bundles.belongsToMany(db.urban_legends, {
as: 'legends',
foreignKey: {
name: 'download_bundles_legendsId',
},
constraints: false,
through: 'download_bundlesLegendsUrban_legends',
});
db.download_bundles.belongsToMany(db.urban_legends, {
as: 'legends_filter',
foreignKey: {
name: 'download_bundles_legendsId',
},
constraints: false,
through: 'download_bundlesLegendsUrban_legends',
});
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
//end loop
db.download_bundles.belongsTo(db.users, {
as: 'requested_by',
foreignKey: {
name: 'requested_byId',
},
constraints: false,
});
db.download_bundles.hasMany(db.file, {
as: 'bundle_file',
foreignKey: 'belongsToId',
constraints: false,
scope: {
belongsTo: db.download_bundles.getTableName(),
belongsToColumn: 'bundle_file',
},
});
db.download_bundles.belongsTo(db.users, {
as: 'createdBy',
});
db.download_bundles.belongsTo(db.users, {
as: 'updatedBy',
});
};
return download_bundles;
};