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 recipes = sequelize.define( 'recipes', { id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true, }, title: { type: DataTypes.TEXT, }, instructions: { type: DataTypes.TEXT, }, suggested_by: { type: DataTypes.TEXT, }, importHash: { type: DataTypes.STRING(255), allowNull: true, unique: true, }, }, { timestamps: true, paranoid: true, freezeTableName: true, }, ); recipes.associate = (db) => { db.recipes.belongsToMany(db.food_inventory, { as: 'ingredients', foreignKey: { name: 'recipes_ingredientsId', }, constraints: false, through: 'recipesIngredientsFood_inventory', }); db.recipes.belongsToMany(db.food_inventory, { as: 'ingredients_filter', foreignKey: { name: 'recipes_ingredientsId', }, constraints: false, through: 'recipesIngredientsFood_inventory', }); db.recipes.belongsToMany(db.food_inventory, { as: 'tags', foreignKey: { name: 'recipes_tagsId', }, constraints: false, through: 'recipesTagsFood_inventory', }); db.recipes.belongsToMany(db.food_inventory, { as: 'tags_filter', foreignKey: { name: 'recipes_tagsId', }, constraints: false, through: 'recipesTagsFood_inventory', }); /// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity //end loop db.recipes.belongsTo(db.users, { as: 'createdBy', }); db.recipes.belongsTo(db.users, { as: 'updatedBy', }); }; return recipes; };