import type { Sequelize, Model } from 'sequelize'
import { Employee } from './Employee'
import type { EmployeeAttributes, EmployeeCreationAttributes } from './Employee'
import { Department } from './Department'
import type { DepartmentAttributes, DepartmentCreationAttributes } from './Department'
import { DeptEmp } from './DeptEmp'
import type { DeptEmpAttributes, DeptEmpCreationAttributes } from './DeptEmp'
import { DeptManager } from './DeptManager'
import type { DeptManagerAttributes, DeptManagerCreationAttributes } from './DeptManager'
import { Title } from './Title'
import type { TitleAttributes, TitleCreationAttributes } from './Title'
import { Salary } from './Salary'
import type { SalaryAttributes, SalaryCreationAttributes } from './Salary'
export {
Employee,
Department,
DeptEmp,
DeptManager,
Title,
Salary
}
export type {
EmployeeAttributes,
EmployeeCreationAttributes,
DepartmentAttributes,
DepartmentCreationAttributes,
DeptEmpAttributes,
DeptEmpCreationAttributes,
DeptManagerAttributes,
DeptManagerCreationAttributes,
TitleAttributes,
TitleCreationAttributes,
SalaryAttributes,
SalaryCreationAttributes
}
export function initModels(sequelize: Sequelize) {
Employee.initModel(sequelize)
Department.initModel(sequelize)
DeptEmp.initModel(sequelize)
DeptManager.initModel(sequelize)
Title.initModel(sequelize)
Salary.initModel(sequelize)
Employee.hasMany(Salary, {
foreignKey: 'emp_no'
})
Employee.hasMany(Title, {
foreignKey: 'emp_no'
})
Employee.belongsToMany(Department, {
as: 'employingDepartments',
through: DeptEmp,
foreignKey: 'emp_no',
otherKey: 'dept_no',
onDelete: 'CASCADE'
})
Employee.belongsToMany(Department, {
as: 'managedDepartments',
through: DeptManager,
foreignKey: 'emp_no',
otherKey: 'dept_no',
onDelete: 'CASCADE'
})
Department.belongsToMany(Employee, {
as: 'employees',
through: DeptEmp,
foreignKey: 'departments_id',
otherKey: 'employee_id',
onDelete: 'CASCADE'
})
Department.belongsToMany(Employee, {
as: 'managers',
through: DeptManager,
foreignKey: 'departments_id',
otherKey: 'manager_id',
onDelete: 'CASCADE'
})
Department.hasMany(DeptEmp, {
foreignKey: 'dept_no'
})
Department.hasMany(DeptManager, {
foreignKey: 'dept_no'
})
DeptEmp.belongsTo(Employee, {
foreignKey: 'emp_no'
})
DeptEmp.belongsTo(Department, {
foreignKey: 'dept_no'
})
DeptManager.belongsTo(Employee, {
foreignKey: 'emp_no'
})
DeptManager.belongsTo(Department, {
foreignKey: 'dept_no'
})
Title.belongsTo(Employee, {
foreignKey: 'emp_no'
})
Salary.belongsTo(Employee, {
foreignKey: 'emp_no'
})
return {
Employee,
Department,
DeptEmp,
DeptManager,
Title,
Salary
}
}