diff --git a/src/api/Models/department.js b/src/api/Models/department.js index 702375e..94be5e1 100644 --- a/src/api/Models/department.js +++ b/src/api/Models/department.js @@ -1,54 +1,56 @@ -'use strict'; -const { Model } = require('sequelize'); +import { Model, DataTypes } from 'sequelize'; -module.exports = (sequelize, DataTypes) => { +export default (sequelize) => { class Department extends Model { static associate(models) { - // 2. One department optionally has one department head (an instructor) + // One department optionally has one department head (an instructor) Department.belongsTo(models.Instructor, { foreignKey: 'head_id', - as: 'head' // This lets you do department.getHead() + as: 'head', // Allows department.getHead() }); } } - Department.init({ - department_id: { - type: DataTypes.INTEGER, - primaryKey: true, - autoIncrement: true + Department.init( + { + department_id: { + type: DataTypes.INTEGER, + primaryKey: true, + autoIncrement: true, + }, + department_name: { + type: DataTypes.STRING(100), + allowNull: false, + }, + department_code: { + type: DataTypes.STRING(20), + allowNull: false, + unique: true, + }, + head_id: { + type: DataTypes.INTEGER, + allowNull: true, + }, + email: { + type: DataTypes.STRING(150), + allowNull: true, + }, + phone_number: { + type: DataTypes.STRING(20), + allowNull: true, + }, + building: { + type: DataTypes.STRING(100), + allowNull: true, + }, }, - department_name: { - type: DataTypes.STRING(100), - allowNull: false - }, - department_code: { - type: DataTypes.STRING(20), - allowNull: false, - unique: true - }, - head_id: { - type: DataTypes.INTEGER, - allowNull: true - }, - email: { - type: DataTypes.STRING(150), - allowNull: true - }, - phone_number: { - type: DataTypes.STRING(20), - allowNull: true - }, - building: { - type: DataTypes.STRING(100), - allowNull: true - }, - }, { - sequelize, - modelName: 'Department', - tableName: 'Departments', - timestamps: false // uses created_at and updated_at - }); + { + sequelize, + modelName: 'Department', + tableName: 'Departments', + timestamps: false, + } + ); return Department; -}; \ No newline at end of file +}; diff --git a/src/api/Models/index.js b/src/api/Models/index.js index a8b2997..5907d80 100644 --- a/src/api/Models/index.js +++ b/src/api/Models/index.js @@ -1,35 +1,46 @@ -const fs = require('fs'); -const path = require('path'); -const Sequelize = require('sequelize'); +import fs from 'fs'; +import path from 'path'; +import Sequelize, { DataTypes } from 'sequelize'; +import configFile from '../../config/config.js'; + +// __dirname replacement for ES modules +const __dirname = path.dirname(new URL(import.meta.url).pathname); const basename = path.basename(__filename); -// Adjust the config path to point to src/config/config.json -const configPath = path.resolve(__dirname, '../../config/config.js'); -const config = require(configPath)['development']; +// Use the correct environment (you can switch to process.env.NODE_ENV if needed) +const config = configFile.development; const db = {}; +// Initialize Sequelize const sequelize = new Sequelize(config.database, config.username, config.password, config); -fs +// Dynamically import all models in this folder +const files = fs .readdirSync(__dirname) - .filter(file => { - return ( + .filter( + (file) => file.indexOf('.') !== 0 && file !== basename && - file.slice(-3) === '.js' - ); - }) - .forEach(file => { - const model = require(path.join(__dirname, file))(sequelize, Sequelize.DataTypes); - db[model.name] = model; - }); - -Object.keys(db).forEach(modelName => { + file.endsWith('.js') + ); + +for (const file of files) { + const { default: modelDefiner } = await import(path.join(__dirname, file)); + const model = modelDefiner(sequelize, DataTypes); + db[model.name] = model; +} + +// Set up associations +for (const modelName of Object.keys(db)) { if (db[modelName].associate) { db[modelName].associate(db); } -}); +} + +db.sequelize = sequelize; + +export default db; db.sequelize = sequelize; // db.Sequelize = Sequelize; diff --git a/src/api/Models/instructor.js b/src/api/Models/instructor.js index 3dcbc61..33929b1 100644 --- a/src/api/Models/instructor.js +++ b/src/api/Models/instructor.js @@ -1,35 +1,32 @@ -'use strict'; -const { - Model -} = require('sequelize'); -const department = require('./department'); -module.exports = (sequelize, DataTypes) => { +import { Model, DataTypes } from 'sequelize'; + +export default (sequelize) => { class Instructor extends Model { - /** - * Helper method for defining associations. - * This method is not a part of Sequelize lifecycle. - * The `models/index` file will call this method automatically. - */ static associate(models) { - // define association here + // Define association here Instructor.belongsTo(models.Department, { - foreignKey: 'department_id', - as: 'department' + foreignKey: 'department_id', + as: 'department', }); } } - Instructor.init({ - first_name: DataTypes.STRING, - last_name: DataTypes.STRING, - email: DataTypes.STRING, - phone_number: DataTypes.STRING, - hire_date: DataTypes.DATE, - department: DataTypes.INTEGER, - is_active: DataTypes.BOOLEAN - }, { - sequelize, - modelName: 'Instructor', - timestamps: true - }); + + Instructor.init( + { + first_name: DataTypes.STRING, + last_name: DataTypes.STRING, + email: DataTypes.STRING, + phone_number: DataTypes.STRING, + hire_date: DataTypes.DATE, + department: DataTypes.INTEGER, + is_active: DataTypes.BOOLEAN, + }, + { + sequelize, + modelName: 'Instructor', + timestamps: true, + } + ); + return Instructor; -}; \ No newline at end of file +}; diff --git a/src/api/Models/programcourse.js b/src/api/Models/programcourse.js index e9220b7..9e09bbc 100644 --- a/src/api/Models/programcourse.js +++ b/src/api/Models/programcourse.js @@ -1,60 +1,57 @@ -'use strict'; -const { - Model -} = require('sequelize'); -module.exports = (sequelize, DataTypes) => { +import { Model, DataTypes } from 'sequelize'; + +export default (sequelize) => { class ProgramCourse extends Model { - /** - * Helper method for defining associations. - * This method is not a part of Sequelize lifecycle. - * The `models/index` file will call this method automatically. - */ static associate(models) { - // define association here - + // Self-referencing association ProgramCourse.belongsTo(models.ProgramCourse, { - as: 'parent', - foreignKey: 'parent_id' + as: 'parent', + foreignKey: 'parent_id', }); ProgramCourse.hasMany(models.ProgramCourse, { - as: 'children', - foreignKey: 'parent_id' + as: 'children', + foreignKey: 'parent_id', }); // Department association ProgramCourse.belongsTo(models.Department, { foreignKey: 'department_id', - as: 'department' + as: 'department', }); } } - ProgramCourse.init({ - parent_id: DataTypes.INTEGER, - type: { - type:DataTypes.ENUM('PROGRAM','COURSE'), - allowNull:false, - defaultValue:null, - }, - code: DataTypes.STRING, - name: DataTypes.STRING, - department_id: DataTypes.INTEGER, - credit_hours: DataTypes.INTEGER, - semester: DataTypes.INTEGER, - level: { - type: DataTypes.ENUM('DIPLOMA','DEGREE','MASTER','DOCTORATE'), - allowNull:false + + ProgramCourse.init( + { + parent_id: DataTypes.INTEGER, + type: { + type: DataTypes.ENUM('PROGRAM', 'COURSE'), + allowNull: false, + defaultValue: null, + }, + code: DataTypes.STRING, + name: DataTypes.STRING, + department_id: DataTypes.INTEGER, + credit_hours: DataTypes.INTEGER, + semester: DataTypes.INTEGER, + level: { + type: DataTypes.ENUM('DIPLOMA', 'DEGREE', 'MASTER', 'DOCTORATE'), + allowNull: false, + }, + is_active: DataTypes.BOOLEAN, + mode: { + type: DataTypes.ENUM('FULLTIME', 'PARTTIME', 'ONLINE', 'HYBRID', 'DISTANCE'), + allowNull: false, + defaultValue: '', + }, }, - is_active: DataTypes.BOOLEAN, - mode: { - type: DataTypes.ENUM('FULLTIME', 'PARTTIME', 'ONLINE', 'HYBRID', 'DISTANCE'), - allowNull: false, // or false if it's required - defaultValue:'' -} - }, { - sequelize, - modelName: 'ProgramCourse', - timestamps: true - }); + { + sequelize, + modelName: 'ProgramCourse', + timestamps: true, + } + ); + return ProgramCourse; -}; \ No newline at end of file +}; diff --git a/src/api/Models/student.js b/src/api/Models/student.js index cb647fa..e978be8 100644 --- a/src/api/Models/student.js +++ b/src/api/Models/student.js @@ -1,39 +1,37 @@ -'use strict'; -const { - Model -} = require('sequelize'); -module.exports = (sequelize, DataTypes) => { +import { Model, DataTypes } from 'sequelize'; + +export default (sequelize) => { class Student extends Model { - /** - * Helper method for defining associations. - * This method is not a part of Sequelize lifecycle. - * The `models/index` file will call this method automatically. - */ static associate(models) { - // define association - Student.belongsTo(models.User, { - foreignKey: 'userId', + // Define association + Student.belongsTo(models.User, { + foreignKey: 'user_id', as: 'user', }); } } - Student.init({ - user_id: DataTypes.INTEGER, - student_number: DataTypes.STRING, - first_name: DataTypes.STRING, - last_name: DataTypes.STRING, - email: DataTypes.STRING, - phone_number: DataTypes.STRING, - date_of_birth: DataTypes.DATE, - gender: DataTypes.STRING, - enrollment_year: DataTypes.INTEGER, - department_id: DataTypes.INTEGER, - is_active: DataTypes.BOOLEAN, - created_at: DataTypes.DATE, - updated_at: DataTypes.DATE - }, { - sequelize, - modelName: 'Student', - }); + + Student.init( + { + user_id: DataTypes.INTEGER, + student_number: DataTypes.STRING, + first_name: DataTypes.STRING, + last_name: DataTypes.STRING, + email: DataTypes.STRING, + phone_number: DataTypes.STRING, + date_of_birth: DataTypes.DATE, + gender: DataTypes.STRING, + enrollment_year: DataTypes.INTEGER, + department_id: DataTypes.INTEGER, + is_active: DataTypes.BOOLEAN, + created_at: DataTypes.DATE, + updated_at: DataTypes.DATE, + }, + { + sequelize, + modelName: 'Student', + } + ); + return Student; -}; \ No newline at end of file +}; diff --git a/src/api/Models/user.js b/src/api/Models/user.js index d4686e7..8e6ade1 100644 --- a/src/api/Models/user.js +++ b/src/api/Models/user.js @@ -1,49 +1,46 @@ -'use strict'; -const { Model, DataTypes} = require('sequelize'); - -module.exports = (sequelize, DataTypes) => { +import { Model, DataTypes } from 'sequelize'; +export default (sequelize) => { class User extends Model { - /** - * Helper method for defining associations. - * This method is not a part of Sequelize lifecycle. - * The `models/index` file will call this method automatically. - */ static associate(models) { - // define association here + // Define associations here (if needed) } } - User.init({ - username: { - allowNull: false, - unique: true, - type: DataTypes.STRING, - }, - email: { - allowNull: false, - unique: true, - type: DataTypes.STRING, - }, - password: { - type: DataTypes.STRING, - }, - role: { - type: DataTypes.ENUM("STUDENT", "ADMIN", "INSTRUCTOR"), - allowNull: false, - defaultValue: "STUDENT" - }, - is_active: { - type: DataTypes.BOOLEAN, - }, - lastLogin: { - type: DataTypes.DATE, - allowNull: true, + + User.init( + { + username: { + allowNull: false, + unique: true, + type: DataTypes.STRING, + }, + email: { + allowNull: false, + unique: true, + type: DataTypes.STRING, + }, + password: { + type: DataTypes.STRING, + }, + role: { + type: DataTypes.ENUM('STUDENT', 'ADMIN', 'INSTRUCTOR'), + allowNull: false, + defaultValue: 'STUDENT', + }, + is_active: { + type: DataTypes.BOOLEAN, + }, + lastLogin: { + type: DataTypes.DATE, + allowNull: true, + }, }, - }, { - sequelize, - modelName: 'User', - timestamps: true, - }); + { + sequelize, + modelName: 'User', + timestamps: true, + } + ); return User; };