Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 44 additions & 42 deletions src/api/Models/department.js
Original file line number Diff line number Diff line change
@@ -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;
};
};
49 changes: 30 additions & 19 deletions src/api/Models/index.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
53 changes: 25 additions & 28 deletions src/api/Models/instructor.js
Original file line number Diff line number Diff line change
@@ -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;
};
};
85 changes: 41 additions & 44 deletions src/api/Models/programcourse.js
Original file line number Diff line number Diff line change
@@ -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;
};
};
Loading
Loading