-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Hot fix database optimization. Remove redundant associations
- Loading branch information
1 parent
b88b8cb
commit 8e57f24
Showing
2 changed files
with
34 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |