Conversation
config/db.js
Outdated
| export default { | ||
| development: { | ||
| host: process.env.DB_HOST, | ||
| username: process.env.DB_USER, | ||
| password: process.env.DB_PASSWORD, | ||
| database: process.env.DB_NAME, | ||
| dialect: process.env.DB_DIALECT, | ||
| }, |
There was a problem hiding this comment.
const {DB_HOST,DB_USER,DB_PASSWORD,DB_NAME,DB_DIALECT} = process.env
then use it like this way :
`export default {
development: {
host: DB_HOST,
username: DB_USER,
password: DB_PASSWORD,
database: DB_NAME,
dialect:DB_DIALECT,
},
config/sequelize.js
Outdated
|
|
||
| sequelize.authenticate() | ||
| .then(() => { | ||
| console.log('Database connected successfully'); |
There was a problem hiding this comment.
if possible then please used the Pino logger rather than console log : https://www.npmjs.com/package/pino
There was a problem hiding this comment.
@dipali-23
Replace all console.log statements with the pino logger wherever they're used
.gitignore
Outdated
| @@ -0,0 +1,5 @@ | |||
| # Node.js dependencies | |||
There was a problem hiding this comment.
| # Node.js dependencies | |
| # dependencies |
.gitignore
Outdated
| # Node.js dependencies | ||
| node_modules/ | ||
|
|
||
| # Environment variables (sensitive information) |
There was a problem hiding this comment.
| # Environment variables (sensitive information) | |
| # env variables |
| import { config } from 'dotenv'; | ||
| config(); | ||
|
|
||
| export default { | ||
| development: { | ||
| host: process.env.DB_HOST, | ||
| username: process.env.DB_USER, | ||
| password: process.env.DB_PASSWORD, | ||
| database: process.env.DB_NAME, | ||
| dialect: process.env.DB_DIALECT, | ||
| }, |
There was a problem hiding this comment.
| import { config } from 'dotenv'; | |
| config(); | |
| export default { | |
| development: { | |
| host: process.env.DB_HOST, | |
| username: process.env.DB_USER, | |
| password: process.env.DB_PASSWORD, | |
| database: process.env.DB_NAME, | |
| dialect: process.env.DB_DIALECT, | |
| }, | |
| import { config } from 'dotenv'; | |
| config(); | |
| const { DB_HOST, DB_USER, DB_PASSWORD, DB_NAME, DB_DIALECT } = process.env; | |
| const DBclient = { | |
| host: DB_HOST, | |
| username: DB_USER, | |
| password: DB_PASSWORD, | |
| database: DB_NAME, | |
| dialect: DB_DIALECT | |
| } | |
| export default DBclient; |
config/sequelize.js
Outdated
|
|
||
| sequelize.authenticate() | ||
| .then(() => { | ||
| console.log('Database connected successfully'); |
There was a problem hiding this comment.
@dipali-23
Replace all console.log statements with the pino logger wherever they're used
controllers/student.js
Outdated
| import studentValidationSchema from '../utils/studentValidation.js'; | ||
| import { HTTP_STATUS_CODES } from '../utils/statusCodes.js'; // Import status codes |
There was a problem hiding this comment.
| import studentValidationSchema from '../utils/studentValidation.js'; | |
| import { HTTP_STATUS_CODES } from '../utils/statusCodes.js'; // Import status codes | |
| import studentValidationSchema from '../utils/validators/student.js'; | |
| import { HTTP_STATUS_CODES } from '../utils/constants/common.js'; |
- Try to follow file naming conventions in a snake case
status-code.jsrather thanstatusCodes.js - Grouping files with the generic folder name.
-
For Example :
-
Without Grouping
utils/studentValidation.js❌utils/subjectValidation.js❌utils/professorValidation.js❌
-
Grouping of all validators
utils/validators/student.js✅utils/validators/subject.js✅utils/validators/professor.js✅
-
services/student.js
Outdated
| const student = await Student.findByPk(id); | ||
| return student; | ||
| } catch (error) { | ||
| throw new Error('Error fetching student: ' + error.message); |
There was a problem hiding this comment.
| throw new Error('Error fetching student: ' + error.message); | |
| throw new Error('An error occurred while fetching student (service): ' + error.message); |
services/student.js
Outdated
| await student.update(data); | ||
| return student; | ||
| } catch (error) { | ||
| throw new Error('Error updating student: ' + error.message); |
There was a problem hiding this comment.
| throw new Error('Error updating student: ' + error.message); | |
| throw new Error('An error occurred while updating student (service): ' + error.message); |
services/student.js
Outdated
| await student.destroy(); | ||
| return { message: 'Student deleted successfully' }; | ||
| } catch (error) { | ||
| throw new Error('Error deleting student: ' + error.message); |
There was a problem hiding this comment.
| throw new Error('Error deleting student: ' + error.message); | |
| throw new Error('An error occurred while deleting student (service) : ' + error.message); |
| // Success Response Helper | ||
| export const sendResponse = (res, statusCode, message, data = null) => { | ||
| return res.status(statusCode).json({ | ||
| success: statusCode >= 200 && statusCode < 300, | ||
| message: message, | ||
| data: data, | ||
| }); | ||
| }; | ||
|
|
||
| // Error Response Helper | ||
| export const sendErrorResponse = (res, error, statusCode = 500) => { | ||
| return res.status(statusCode).json({ | ||
| success: false, | ||
| message: error || "something went wrong", | ||
| }); | ||
| }; |
There was a problem hiding this comment.
| // Success Response Helper | |
| export const sendResponse = (res, statusCode, message, data = null) => { | |
| return res.status(statusCode).json({ | |
| success: statusCode >= 200 && statusCode < 300, | |
| message: message, | |
| data: data, | |
| }); | |
| }; | |
| // Error Response Helper | |
| export const sendErrorResponse = (res, error, statusCode = 500) => { | |
| return res.status(statusCode).json({ | |
| success: false, | |
| message: error || "something went wrong", | |
| }); | |
| }; | |
| export const sendResponse = ( | |
| res: MedusaResponse, | |
| success: boolean, | |
| message: string, | |
| statusCode: TStatusCode, | |
| status: TStatus, | |
| data?: any // server error message or data : {} | |
| ): void => { | |
| res.status(statusCode).json({ | |
| success, | |
| message, | |
| status, | |
| ...(data ? { data } : {}), | |
| }) | |
| } |
We can make it single generic function to send the success and error responses both.
| export const HTTP_STATUS_CODES = { | ||
| OK: 200, | ||
| CREATED: 201, | ||
| BAD_REQUEST: 400, | ||
| NOT_FOUND: 404, | ||
| INTERNAL_SERVER_ERROR: 500, | ||
| }; |
There was a problem hiding this comment.
| export const HTTP_STATUS_CODES = { | |
| OK: 200, | |
| CREATED: 201, | |
| BAD_REQUEST: 400, | |
| NOT_FOUND: 404, | |
| INTERNAL_SERVER_ERROR: 500, | |
| }; |
- We can use the http-status code npm package : https://www.npmjs.com/package/http-status-codes (I am preferring this)
- And even if you'll need to manage this yourself. First, create constants and then export them for use.
const SUCCESS = 200
const CREATED = 201
const BAD_REQUEST = 400
const UNAUTHORIZED = 401
const FORBIDDEN = 403
const NOT_FOUND = 404
const CONFLICT = 409
const INTERNAL_SERVER_ERROR = 500
const SERVICE_UNAVAILABLE = 503
const SUCCESS_STATUS = "SUCCESS"
const CREATED_STATUS = "CREATED"
const BAD_REQUEST_STATUS = "BAD_REQUEST"
const UNAUTHORIZED_STATUS = "UNAUTHORIZED"
const FORBIDDEN_STATUS = "FORBIDDEN"
const NOT_FOUND_STATUS = "NOT_FOUND"
const CONFLICT_STATUS = "CONFLICT"
const INTERNAL_SERVER_ERROR_STATUS = "INTERNAL_SERVER_ERROR"
const SERVICE_UNAVAILABLE_STATUS = "SERVICE_UNAVAILABLE"
export const STATUS_CODE = {
SUCCESS,
CREATED,
BAD_REQUEST,
UNAUTHORIZED,
FORBIDDEN,
NOT_FOUND,
CONFLICT,
INTERNAL_SERVER_ERROR,
SERVICE_UNAVAILABLE,
}
export const STATUS = {
SUCCESS_STATUS,
CREATED_STATUS,
BAD_REQUEST_STATUS,
UNAUTHORIZED_STATUS,
FORBIDDEN_STATUS,
NOT_FOUND_STATUS,
CONFLICT_STATUS,
INTERNAL_SERVER_ERROR_STATUS,
SERVICE_UNAVAILABLE_STATUS,
}
No description provided.