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; };