190 lines
3.4 KiB
TypeScript
190 lines
3.4 KiB
TypeScript
import { Op } from 'sequelize';
|
|
|
|
import type {
|
|
SequelizeModel,
|
|
SequelizeModelFactory,
|
|
} from '../../types/index.ts';
|
|
|
|
const defineAssetsModel: SequelizeModelFactory = (sequelize, DataTypes) => {
|
|
const assets: SequelizeModel = sequelize.define(
|
|
'assets',
|
|
{
|
|
id: {
|
|
type: DataTypes.UUID,
|
|
defaultValue: DataTypes.UUIDV4,
|
|
primaryKey: true,
|
|
},
|
|
|
|
name: {
|
|
type: DataTypes.TEXT,
|
|
validate: {
|
|
len: {
|
|
args: [0, 255],
|
|
msg: 'Asset name must be at most 255 characters',
|
|
},
|
|
},
|
|
},
|
|
|
|
asset_type: {
|
|
type: DataTypes.ENUM,
|
|
allowNull: false,
|
|
|
|
values: ['image', 'video', 'audio', 'file', 'embed'],
|
|
},
|
|
|
|
type: {
|
|
type: DataTypes.ENUM,
|
|
allowNull: false,
|
|
defaultValue: 'general',
|
|
|
|
values: [
|
|
'icon',
|
|
|
|
'background_image',
|
|
|
|
'audio',
|
|
|
|
'video',
|
|
|
|
'transition',
|
|
|
|
'logo',
|
|
|
|
'favicon',
|
|
|
|
'document',
|
|
|
|
'general',
|
|
|
|
'embed_360',
|
|
|
|
'embed_3d',
|
|
|
|
'embed_iframe',
|
|
],
|
|
},
|
|
|
|
cdn_url: {
|
|
type: DataTypes.TEXT,
|
|
},
|
|
|
|
storage_key: {
|
|
type: DataTypes.TEXT,
|
|
},
|
|
|
|
mime_type: {
|
|
type: DataTypes.TEXT,
|
|
validate: {
|
|
is: {
|
|
args: /^[a-z0-9]+\/[a-z0-9.+-]+$/i,
|
|
msg: 'Invalid MIME type format',
|
|
},
|
|
},
|
|
},
|
|
|
|
size_mb: {
|
|
type: DataTypes.DECIMAL,
|
|
},
|
|
|
|
width_px: {
|
|
type: DataTypes.INTEGER,
|
|
},
|
|
|
|
height_px: {
|
|
type: DataTypes.INTEGER,
|
|
},
|
|
|
|
duration_sec: {
|
|
type: DataTypes.DECIMAL,
|
|
},
|
|
|
|
frame_rate: {
|
|
type: DataTypes.DECIMAL,
|
|
},
|
|
|
|
embed_code: {
|
|
type: DataTypes.TEXT,
|
|
allowNull: true,
|
|
},
|
|
|
|
embed_provider: {
|
|
type: DataTypes.TEXT,
|
|
allowNull: true,
|
|
},
|
|
|
|
checksum: {
|
|
type: DataTypes.TEXT,
|
|
},
|
|
|
|
is_public: {
|
|
type: DataTypes.BOOLEAN,
|
|
|
|
allowNull: false,
|
|
defaultValue: false,
|
|
},
|
|
|
|
importHash: {
|
|
type: DataTypes.STRING(255),
|
|
allowNull: true,
|
|
unique: true,
|
|
},
|
|
},
|
|
{
|
|
timestamps: true,
|
|
paranoid: true,
|
|
freezeTableName: true,
|
|
indexes: [
|
|
{ fields: ['projectId'] },
|
|
{
|
|
fields: ['storage_key'],
|
|
name: 'assets_storage_key_active',
|
|
where: {
|
|
deletedAt: null,
|
|
storage_key: {
|
|
[Op.ne]: null,
|
|
},
|
|
},
|
|
},
|
|
{ fields: ['asset_type'] },
|
|
{ fields: ['type'] },
|
|
{ fields: ['is_public'] },
|
|
{ fields: ['deletedAt'] },
|
|
],
|
|
},
|
|
);
|
|
|
|
assets.associate = (db) => {
|
|
db.assets.hasMany(db.asset_variants, {
|
|
as: 'asset_variants_asset',
|
|
foreignKey: {
|
|
name: 'assetId',
|
|
},
|
|
constraints: true,
|
|
onDelete: 'CASCADE',
|
|
onUpdate: 'CASCADE',
|
|
});
|
|
|
|
db.assets.belongsTo(db.projects, {
|
|
as: 'project',
|
|
foreignKey: {
|
|
name: 'projectId',
|
|
},
|
|
constraints: true,
|
|
onDelete: 'CASCADE',
|
|
onUpdate: 'CASCADE',
|
|
});
|
|
|
|
db.assets.belongsTo(db.users, {
|
|
as: 'createdBy',
|
|
});
|
|
|
|
db.assets.belongsTo(db.users, {
|
|
as: 'updatedBy',
|
|
});
|
|
};
|
|
|
|
return assets;
|
|
};
|
|
|
|
export default defineAssetsModel;
|