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 aircrafts = sequelize.define( 'aircrafts', { id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true, }, brand: { type: DataTypes.TEXT, }, model: { type: DataTypes.TEXT, }, year: { type: DataTypes.INTEGER, }, serial_number: { type: DataTypes.TEXT, }, registration: { type: DataTypes.TEXT, }, last_service_date: { type: DataTypes.DATE, }, importHash: { type: DataTypes.STRING(255), allowNull: true, unique: true, }, }, { timestamps: true, paranoid: true, freezeTableName: true, }, ); aircrafts.associate = (db) => { /// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity db.aircrafts.hasMany(db.flights, { as: 'flights_aircraft', foreignKey: { name: 'aircraftId', }, constraints: false, }); //end loop db.aircrafts.belongsTo(db.organizations, { as: 'organizations', foreignKey: { name: 'organizationsId', }, constraints: false, }); db.aircrafts.hasMany(db.file, { as: 'airworthiness_certificate', foreignKey: 'belongsToId', constraints: false, scope: { belongsTo: db.aircrafts.getTableName(), belongsToColumn: 'airworthiness_certificate', }, }); db.aircrafts.belongsTo(db.users, { as: 'createdBy', }); db.aircrafts.belongsTo(db.users, { as: 'updatedBy', }); }; return aircrafts; };