Skip to content

Commit

Permalink
Hot fix database optimization. Remove redundant associations
Browse files Browse the repository at this point in the history
  • Loading branch information
building-u committed Jul 19, 2024
1 parent b88b8cb commit 8e57f24
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 37 deletions.
6 changes: 0 additions & 6 deletions Models/Mentors.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
module.exports = (sequelize, DataTypes) => {
const Mentor = sequelize.define("Mentor", {
id: {
type: DataTypes.INTEGER,
allowNull: false,
autoIncrement: true,
primaryKey: true,
},
email: {
type: DataTypes.STRING,
allowNull: false,
Expand Down
65 changes: 34 additions & 31 deletions Models/index.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,60 @@
const dbConfig = require('../config/db.config')['development'];
const { Sequelize, DataTypes } = require('sequelize');
const dbConfig = require("../config/db.config")["development"];
const { Sequelize, DataTypes } = require("sequelize");

const sequelize = new Sequelize(dbConfig.db, dbConfig.user, dbConfig.password, {
host: dbConfig.host,
dialect: "mysql",
port:dbConfig.port
port: dbConfig.port,
});

const User = require('./User')(sequelize, DataTypes);
const FeedbackRequest = require('./Feedbackrequest')(sequelize, DataTypes);
const Feedbacks = require('./Feedbacks')(sequelize, DataTypes);
const Otptoken = require('./Otptoken')(sequelize, DataTypes);
const ExerciseInfo = require('./ExerciseInfo')(sequelize, DataTypes);
const Mentors = require('./Mentors')(sequelize, DataTypes);
const User = require("./User")(sequelize, DataTypes);
const FeedbackRequest = require("./Feedbackrequest")(sequelize, DataTypes);
const Feedbacks = require("./Feedbacks")(sequelize, DataTypes);
const Otptoken = require("./Otptoken")(sequelize, DataTypes);
const ExerciseInfo = require("./ExerciseInfo")(sequelize, DataTypes);
const Mentors = require("./Mentors")(sequelize, DataTypes);

// Define associations between models
User.hasMany(FeedbackRequest, { foreignKey: 'userId' });
FeedbackRequest.belongsTo(User, { foreignKey: 'userId' });
FeedbackRequest.belongsTo(User, { as: 'mentor', foreignKey: 'mentorId' });


FeedbackRequest.hasMany(Feedbacks, { foreignKey: 'feedbackRequestId' });
Feedbacks.belongsTo(FeedbackRequest, { foreignKey: 'feedbackRequestId' });

ExerciseInfo.belongsTo(User, { foreignKey: 'userId' });
User.hasMany(ExerciseInfo, { foreignKey: 'userId' });

FeedbackRequest.belongsTo(User, { foreignKey: 'userId' });
User.hasMany(ExerciseInfo, { foreignKey: 'userId' });
User.hasMany(FeedbackRequest, { foreignKey: "userId" });
FeedbackRequest.belongsTo(User, { foreignKey: "userId" });

FeedbackRequest.belongsTo(User, { as: "mentor", foreignKey: "mentorId" });
User.hasMany(FeedbackRequest, {
as: "mentoredRequests",
foreignKey: "mentorId",
});

Feedbacks.belongsTo(User, {foreignKey: 'userId'});
User.hasMany(Feedbacks, { foreignKey: 'userId' });
FeedbackRequest.hasMany(Feedbacks, { foreignKey: "feedbackRequestId" });
Feedbacks.belongsTo(FeedbackRequest, { foreignKey: "feedbackRequestId" });

ExerciseInfo.belongsTo(User, { foreignKey: "userId" });
User.hasMany(ExerciseInfo, { foreignKey: "userId" });

async function syncDatabase() {
try {
await sequelize.sync({alter: true });
console.log('Database synchronized.');
await sequelize.sync({ alter: true });
console.log("Database synchronized.");
} catch (error) {
console.error('Error syncing database:', error);
console.error("Error syncing database:", error);
}
}

(async () => {
try {
await sequelize.authenticate();
console.log('Connection has been established successfully.');
console.log("Connection has been established successfully.");
syncDatabase();
} catch (error) {
console.error('Unable to connect to the database:', error);
console.error("Unable to connect to the database:", error);
}
})();

// Export models
module.exports = { sequelize, User, FeedbackRequest, Feedbacks, Otptoken, ExerciseInfo, Mentors };
module.exports = {
sequelize,
User,
FeedbackRequest,
Feedbacks,
Otptoken,
ExerciseInfo,
Mentors,
};

0 comments on commit 8e57f24

Please sign in to comment.