39948-vm/backend/src/db/models/transitions.js
2026-03-19 20:13:16 +04:00

169 lines
2.6 KiB
JavaScript

module.exports = function(sequelize, DataTypes) {
const transitions = sequelize.define(
'transitions',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
environment: {
type: DataTypes.ENUM,
allowNull: false,
defaultValue: 'dev',
values: [
"dev",
"stage",
"production"
],
},
source_key: {
type: DataTypes.TEXT,
},
name: {
type: DataTypes.TEXT,
allowNull: false,
validate: {
notEmpty: { msg: 'Transition name is required' },
len: { args: [1, 255], msg: 'Transition name must be between 1 and 255 characters' },
},
},
slug: {
type: DataTypes.TEXT,
allowNull: false,
validate: {
notEmpty: { msg: 'Slug is required' },
is: { args: /^[a-z0-9_-]+$/i, msg: 'Slug can only contain letters, numbers, dashes, and underscores' },
len: { args: [1, 255], msg: 'Slug must be between 1 and 255 characters' },
},
},
video_url: {
type: DataTypes.TEXT,
},
audio_url: {
type: DataTypes.TEXT,
},
supports_reverse: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
},
duration_sec: {
type: DataTypes.DECIMAL,
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
indexes: [
{ fields: ['projectId'] },
{ fields: ['projectId', 'environment', 'slug'], unique: true },
{ fields: ['deletedAt'] },
],
},
);
transitions.associate = (db) => {
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
db.transitions.hasMany(db.page_links, {
as: 'page_links_transition',
foreignKey: {
name: 'transitionId',
},
constraints: true,
onDelete: 'SET NULL',
onUpdate: 'CASCADE',
});
//end loop
db.transitions.belongsTo(db.projects, {
as: 'project',
foreignKey: {
name: 'projectId',
},
constraints: true,
onDelete: 'CASCADE',
onUpdate: 'CASCADE',
});
db.transitions.belongsTo(db.users, {
as: 'createdBy',
});
db.transitions.belongsTo(db.users, {
as: 'updatedBy',
});
};
return transitions;
};