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 bottles = sequelize.define( 'bottles', { id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true, }, name: { type: DataTypes.TEXT, }, distillery: { type: DataTypes.TEXT, }, size: { type: DataTypes.TEXT, }, style: { type: DataTypes.TEXT, }, year: { type: DataTypes.INTEGER, }, description: { type: DataTypes.TEXT, }, barcode: { type: DataTypes.TEXT, }, Proof: { type: DataTypes.TEXT, }, importHash: { type: DataTypes.STRING(255), allowNull: true, unique: true, }, }, { timestamps: true, paranoid: true, freezeTableName: true, }, ); bottles.associate = (db) => { db.bottles.belongsToMany(db.ratings, { as: 'ratings', foreignKey: { name: 'bottles_ratingsId', }, constraints: false, through: 'bottlesRatingsRatings', }); db.bottles.belongsToMany(db.ratings, { as: 'ratings_filter', foreignKey: { name: 'bottles_ratingsId', }, constraints: false, through: 'bottlesRatingsRatings', }); db.bottles.belongsToMany(db.tasting_notes, { as: 'tasting_notes', foreignKey: { name: 'bottles_tasting_notesId', }, constraints: false, through: 'bottlesTasting_notesTasting_notes', }); db.bottles.belongsToMany(db.tasting_notes, { as: 'tasting_notes_filter', foreignKey: { name: 'bottles_tasting_notesId', }, constraints: false, through: 'bottlesTasting_notesTasting_notes', }); /// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity db.bottles.hasMany(db.ratings, { as: 'ratings_bottle', foreignKey: { name: 'bottleId', }, constraints: false, }); db.bottles.hasMany(db.tasting_notes, { as: 'tasting_notes_bottle', foreignKey: { name: 'bottleId', }, constraints: false, }); //end loop db.bottles.hasMany(db.file, { as: 'Front_Image', foreignKey: 'belongsToId', constraints: false, scope: { belongsTo: db.bottles.getTableName(), belongsToColumn: 'Front_Image', }, }); db.bottles.hasMany(db.file, { as: 'Back_Image', foreignKey: 'belongsToId', constraints: false, scope: { belongsTo: db.bottles.getTableName(), belongsToColumn: 'Back_Image', }, }); db.bottles.belongsTo(db.users, { as: 'createdBy', }); db.bottles.belongsTo(db.users, { as: 'updatedBy', }); }; return bottles; };