Skip to content

Commit 723bfdb

Browse files
committed
add comment in code
1 parent ce9560d commit 723bfdb

File tree

5 files changed

+56
-43
lines changed

5 files changed

+56
-43
lines changed

src/config/db.config.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
11
const dotenv = require("dotenv");
22

3+
// Load environment variables from .env file
34
dotenv.config();
45

6+
// Parse integer values for database pool configuration from environment variables
57
const max = parseInt(process.env.DB_POOL_MAX);
68
const min = parseInt(process.env.DB_POOL_MIN);
79
const acquire = parseInt(process.env.DB_POOL_ACQUIRE);
810
const idle = parseInt(process.env.DB_POOL_IDLE);
911

12+
// Database connection configuration object
1013
const config = {
11-
HOST: process.env.DB_HOST,
12-
USER: process.env.DB_USER,
13-
PASSWORD: process.env.DB_PASSWORD,
14-
DB: process.env.DB_DATABASE,
15-
dialect: process.env.DB_DIALECT,
14+
HOST: process.env.DB_HOST, // Database host address
15+
USER: process.env.DB_USER, // Database user
16+
PASSWORD: process.env.DB_PASSWORD, // Database user's password
17+
DB: process.env.DB_DATABASE, // Name of the database
18+
dialect: process.env.DB_DIALECT, // SQL dialect (e.g., 'mysql', 'postgres')
1619
pool: {
17-
max: max,
18-
min: min,
19-
acquire: acquire,
20-
idle: idle,
20+
// Pool configuration for managing database connections
21+
max: max, // Maximum number of connections in pool
22+
min: min, // Minimum number of connections in pool
23+
acquire: acquire, // Maximum time (ms) that pool will try to get connection before throwing error
24+
idle: idle, // Maximum time (ms) that a connection can be idle before being released
2125
},
2226
};
2327

28+
// Export the configuration object for use in other parts of the application
2429
module.exports = config;

src/middlewares/requiredAdmin.js

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/models/index.js

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
const dbConfig = require("../config/db.config.js");
1+
const dbConfig = require("../config/db.config.js"); // Import database configuration
22

33
const Sequelize = require("sequelize");
44

5+
// Initialize Sequelize instance with database credentials and pool settings
56
const sequelize = new Sequelize(dbConfig.DB, dbConfig.USER, dbConfig.PASSWORD, {
67
host: dbConfig.HOST,
78
dialect: dbConfig.dialect,
@@ -20,6 +21,7 @@ const db = {};
2021
db.Sequelize = Sequelize;
2122
db.sequelize = sequelize;
2223

24+
// Import and initialize all models
2325
db.useraccount = require("./useraccount.model.js")(sequelize, Sequelize);
2426
db.user = require("./user.model.js")(sequelize, Sequelize);
2527
db.serviceprovider = require("./serviceprovider.model.js")(
@@ -30,40 +32,49 @@ db.profile = require("./profile.model.js")(sequelize, Sequelize);
3032
db.review = require("./review.model.js")(sequelize, Sequelize);
3133
db.transaction = require("./transaction.model.js")(sequelize, Sequelize);
3234

35+
// Define associations between models
36+
37+
// One-to-One relationship: UserAccount -> User
3338
db.useraccount.hasOne(db.user, { foreignKey: "userAccountId" });
3439
db.user.belongsTo(db.useraccount, {
3540
foreignKey: "userAccountId",
3641
onDelete: "CASCADE",
3742
});
3843

39-
db.user.hasMany(db.profile, { foreginKey: "userId" });
40-
db.profile.belongsTo(db.user, { foreginKey: "userId", onDelete: "CASCADE" });
44+
// One-to-Many relationship: User -> Profile
45+
db.user.hasMany(db.profile, { foreignKey: "userId" });
46+
db.profile.belongsTo(db.user, { foreignKey: "userId", onDelete: "CASCADE" });
4147

42-
db.user.hasMany(db.review, { foreginKey: "userId" });
43-
db.review.belongsTo(db.user, { foreginKey: "userId", onDelete: "CASCADE" });
48+
// One-to-Many relationship: User -> Review
49+
db.user.hasMany(db.review, { foreignKey: "userId" });
50+
db.review.belongsTo(db.user, { foreignKey: "userId", onDelete: "CASCADE" });
4451

45-
db.user.hasMany(db.transaction, { foreginKey: "userId" });
52+
// One-to-Many relationship: User -> Transaction
53+
db.user.hasMany(db.transaction, { foreignKey: "userId" });
4654
db.transaction.belongsTo(db.user, {
47-
foreginKey: "userId",
55+
foreignKey: "userId",
4856
onDelete: "CASCADE",
4957
});
5058

51-
db.serviceprovider.hasMany(db.profile, { foreginKey: "serviceProviderId" });
59+
// One-to-Many relationship: ServiceProvider -> Profile
60+
db.serviceprovider.hasMany(db.profile, { foreignKey: "serviceProviderId" });
5261
db.profile.belongsTo(db.serviceprovider, {
5362
foreignKey: "serviceProviderId",
5463
onDelete: "CASCADE",
5564
});
5665

57-
db.serviceprovider.hasMany(db.review, { foreginKey: "serviceProviderId" });
66+
// One-to-Many relationship: ServiceProvider -> Review
67+
db.serviceprovider.hasMany(db.review, { foreignKey: "serviceProviderId" });
5868
db.review.belongsTo(db.serviceprovider, {
5969
foreignKey: "serviceProviderId",
6070
onDelete: "CASCADE",
6171
});
6272

63-
db.serviceprovider.hasMany(db.transaction, { foreginKey: "serviceProviderId" });
73+
// One-to-Many relationship: ServiceProvider -> Transaction
74+
db.serviceprovider.hasMany(db.transaction, { foreignKey: "serviceProviderId" });
6475
db.transaction.belongsTo(db.serviceprovider, {
6576
foreignKey: "serviceProviderId",
6677
onDelete: "CASCADE",
6778
});
6879

69-
module.exports = db;
80+
module.exports = db; // Export the configured `db` object for use in other parts of the application

src/models/useraccount.model.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,24 @@ module.exports = (sequelize, Sequelize) => {
4141
},
4242
});
4343

44+
// Define a beforeCreate hook on the UserAccount model
4445
UserAccount.beforeCreate(async (userAccount, options) => {
46+
// Check if password is provided and hash it using bcrypt
4547
if (userAccount.password) {
46-
const salt = await bcrypt.genSalt(10);
48+
const salt = await bcrypt.genSalt(10); // Generate salt with 10 rounds
4749
userAccount.password = await bcrypt.hash(
4850
userAccount.password,
4951
salt
50-
);
52+
); // Hash the password with the generated salt
5153
}
5254

55+
// Check if securityAnswer is provided and hash it using bcrypt
5356
if (userAccount.securityAnswer) {
54-
const salt = await bcrypt.genSalt(10);
57+
const salt = await bcrypt.genSalt(10); // Generate salt with 10 rounds
5558
userAccount.securityAnswer = await bcrypt.hash(
5659
userAccount.securityAnswer,
5760
salt
58-
);
61+
); // Hash the security answer with the generated salt
5962
}
6063
});
6164

src/server.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,43 @@ const routes = require("./routes/api");
88

99
const app = express();
1010

11+
// Load environment variables from .env file
1112
dotenv.config();
1213

14+
// Enable Cross-Origin Resource Sharing (CORS)
1315
app.use(cors());
16+
17+
// Middleware to parse JSON request bodies
1418
app.use(express.json());
19+
// Middleware to parse URL-encoded request bodies
1520
app.use(express.urlencoded({ extended: true }));
21+
// HTTP request logger middleware for logging requests to the console
1622
app.use(morgan("dev"));
1723

1824
const PORT = process.env.PORT;
1925
const HOST = process.env.HOST;
2026

27+
// Sync the database models with the database.
28+
// If `force: true`, it will drop and recreate tables each time the server starts.
29+
// Commented out alternative sync which only syncs without dropping existing tables.
2130
// db.sequelize.sync().then(() => {
2231
// console.log("⏳ Database connected");
2332
// });
2433

34+
// Force sync all models, which drops existing tables and re-creates them
2535
db.sequelize.sync({ force: true }).then(() => {
2636
console.log("⏳ New Database connected");
2737
});
2838

39+
// Basic route to verify that the server is running
2940
app.get("/", (req, res) => {
3041
res.status(200).json("⏳ Server is running!");
3142
});
3243

44+
// Mount the API routes under "/api/v1" path
3345
app.use("/api/v1", routes);
3446

47+
// Start the server and listen on specified host and port
3548
app.listen(PORT, HOST, () => {
3649
console.log(`⏳ Server is running on http://${HOST}:${PORT}`);
3750
});

0 commit comments

Comments
 (0)