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 fish_species = sequelize.define( 'fish_species', { id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true, }, common_name: { type: DataTypes.TEXT, }, scientific_name: { type: DataTypes.TEXT, }, conservation_status: { type: DataTypes.ENUM, values: [ "least_concern", "near_threatened", "vulnerable", "endangered", "critically_endangered", "data_deficient", "not_evaluated" ], }, habitat: { type: DataTypes.TEXT, }, description: { type: DataTypes.TEXT, }, fun_facts: { type: DataTypes.TEXT, }, average_length_cm: { type: DataTypes.DECIMAL, }, average_weight_kg: { type: DataTypes.DECIMAL, }, diet: { type: DataTypes.TEXT, }, distribution: { type: DataTypes.TEXT, }, is_featured: { type: DataTypes.BOOLEAN, allowNull: false, defaultValue: false, }, published_at: { type: DataTypes.DATE, }, importHash: { type: DataTypes.STRING(255), allowNull: true, unique: true, }, }, { timestamps: true, paranoid: true, freezeTableName: true, }, ); fish_species.associate = (db) => { /// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity //end loop db.fish_species.belongsTo(db.users, { as: 'author', foreignKey: { name: 'authorId', }, constraints: false, }); db.fish_species.hasMany(db.file, { as: 'gallery_images', foreignKey: 'belongsToId', constraints: false, scope: { belongsTo: db.fish_species.getTableName(), belongsToColumn: 'gallery_images', }, }); db.fish_species.hasMany(db.file, { as: 'attachments', foreignKey: 'belongsToId', constraints: false, scope: { belongsTo: db.fish_species.getTableName(), belongsToColumn: 'attachments', }, }); db.fish_species.belongsTo(db.users, { as: 'createdBy', }); db.fish_species.belongsTo(db.users, { as: 'updatedBy', }); }; return fish_species; };