Skip to content

CRUD API with Sequelize and Joi Validation#1

Open
dipali-23 wants to merge 5 commits intomainfrom
backend
Open

CRUD API with Sequelize and Joi Validation#1
dipali-23 wants to merge 5 commits intomainfrom
backend

Conversation

@dipali-23
Copy link
Owner

No description provided.

config/db.js Outdated
Comment on lines +4 to +11
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,
},

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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,
},


sequelize.authenticate()
.then(() => {
console.log('Database connected successfully');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if possible then please used the Pino logger rather than console log : https://www.npmjs.com/package/pino

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dipali-23
Replace all console.log statements with the pino logger wherever they're used

.gitignore Outdated
@@ -0,0 +1,5 @@
# Node.js dependencies

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Node.js dependencies
# dependencies

.gitignore Outdated
# Node.js dependencies
node_modules/

# Environment variables (sensitive information)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Environment variables (sensitive information)
# env variables

Comment on lines +1 to +11
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,
},

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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;


sequelize.authenticate()
.then(() => {
console.log('Database connected successfully');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dipali-23
Replace all console.log statements with the pino logger wherever they're used

Comment on lines +3 to +4
import studentValidationSchema from '../utils/studentValidation.js';
import { HTTP_STATUS_CODES } from '../utils/statusCodes.js'; // Import status codes
Copy link

@vrajpatelTH vrajpatelTH Feb 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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';
  1. Try to follow file naming conventions in a snake case status-code.js rather than statusCodes.js
  2. 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

const student = await Student.findByPk(id);
return student;
} catch (error) {
throw new Error('Error fetching student: ' + error.message);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
throw new Error('Error fetching student: ' + error.message);
throw new Error('An error occurred while fetching student (service): ' + error.message);

await student.update(data);
return student;
} catch (error) {
throw new Error('Error updating student: ' + error.message);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
throw new Error('Error updating student: ' + error.message);
throw new Error('An error occurred while updating student (service): ' + error.message);

await student.destroy();
return { message: 'Student deleted successfully' };
} catch (error) {
throw new Error('Error deleting student: ' + error.message);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
throw new Error('Error deleting student: ' + error.message);
throw new Error('An error occurred while deleting student (service) : ' + error.message);

Comment on lines +1 to +16
// 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",
});
};
Copy link

@vrajpatelTH vrajpatelTH Feb 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// 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.

Comment on lines +2 to +8
export const HTTP_STATUS_CODES = {
OK: 200,
CREATED: 201,
BAD_REQUEST: 400,
NOT_FOUND: 404,
INTERNAL_SERVER_ERROR: 500,
};

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
export const HTTP_STATUS_CODES = {
OK: 200,
CREATED: 201,
BAD_REQUEST: 400,
NOT_FOUND: 404,
INTERNAL_SERVER_ERROR: 500,
};
  1. We can use the http-status code npm package : https://www.npmjs.com/package/http-status-codes (I am preferring this)
  2. 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,
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants