40065-vm/backend/src/db/models/channels.js
2026-05-25 02:49:14 +00:00

145 lines
2.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 channels = sequelize.define(
'channels',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
name: {
type: DataTypes.TEXT,
},
youtube_handle: {
type: DataTypes.TEXT,
},
youtube_channel_url: {
type: DataTypes.TEXT,
},
description: {
type: DataTypes.TEXT,
},
is_active: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
channels.associate = (db) => {
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
db.channels.hasMany(db.playlists, {
as: 'playlists_channel',
foreignKey: {
name: 'channelId',
},
constraints: false,
});
db.channels.hasMany(db.videos, {
as: 'videos_channel',
foreignKey: {
name: 'channelId',
},
constraints: false,
});
//end loop
db.channels.hasMany(db.file, {
as: 'logo',
foreignKey: 'belongsToId',
constraints: false,
scope: {
belongsTo: db.channels.getTableName(),
belongsToColumn: 'logo',
},
});
db.channels.hasMany(db.file, {
as: 'banner',
foreignKey: 'belongsToId',
constraints: false,
scope: {
belongsTo: db.channels.getTableName(),
belongsToColumn: 'banner',
},
});
db.channels.belongsTo(db.users, {
as: 'createdBy',
});
db.channels.belongsTo(db.users, {
as: 'updatedBy',
});
};
return channels;
};