29843/backend/src/db/models/images.js
2025-03-12 22:38:57 +00:00

84 lines
1.7 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 images = sequelize.define(
'images',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
title: {
type: DataTypes.TEXT,
},
url: {
type: DataTypes.TEXT,
},
category: {
type: DataTypes.TEXT,
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
images.associate = (db) => {
db.images.belongsToMany(db.collections, {
as: 'collections',
foreignKey: {
name: 'images_collectionsId',
},
constraints: false,
through: 'imagesCollectionsCollections',
});
db.images.belongsToMany(db.collections, {
as: 'collections_filter',
foreignKey: {
name: 'images_collectionsId',
},
constraints: false,
through: 'imagesCollectionsCollections',
});
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
db.images.hasMany(db.downloads, {
as: 'downloads_image',
foreignKey: {
name: 'imageId',
},
constraints: false,
});
//end loop
db.images.belongsTo(db.users, {
as: 'createdBy',
});
db.images.belongsTo(db.users, {
as: 'updatedBy',
});
};
return images;
};