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 listings = sequelize.define( 'listings', { id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true, }, title: { type: DataTypes.TEXT, }, description: { type: DataTypes.TEXT, }, price: { type: DataTypes.DECIMAL, }, condition: { type: DataTypes.ENUM, values: ['New', 'GentlyUsed', 'Refurbished', 'NeedsRepair'], }, stock: { type: DataTypes.INTEGER, }, importHash: { type: DataTypes.STRING(255), allowNull: true, unique: true, }, }, { timestamps: true, paranoid: true, freezeTableName: true, }, ); listings.associate = (db) => { /// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity db.listings.hasMany(db.reviews, { as: 'reviews_listing', foreignKey: { name: 'listingId', }, constraints: false, }); //end loop db.listings.belongsTo(db.categories, { as: 'category', foreignKey: { name: 'categoryId', }, constraints: false, }); db.listings.belongsTo(db.users, { as: 'seller', foreignKey: { name: 'sellerId', }, constraints: false, }); db.listings.hasMany(db.file, { as: 'images', foreignKey: 'belongsToId', constraints: false, scope: { belongsTo: db.listings.getTableName(), belongsToColumn: 'images', }, }); db.listings.belongsTo(db.users, { as: 'createdBy', }); db.listings.belongsTo(db.users, { as: 'updatedBy', }); }; return listings; };