Gathering detailed insights and metrics for express-sequelize-kit-mb
Gathering detailed insights and metrics for express-sequelize-kit-mb
Gathering detailed insights and metrics for express-sequelize-kit-mb
Gathering detailed insights and metrics for express-sequelize-kit-mb
A reusable package for backend development using Node.js, Express, and Sequelize, featuring customizable CRUD operations and commonly used API and repository functions. It promotes clean code, encapsulation, and DRY principles for easy integration into various projects.
npm install express-sequelize-kit-mb
Typescript
Module System
Min. Node Version
Node Version
NPM Version
JavaScript (100%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
1 Stars
6 Commits
1 Watchers
1 Branches
2 Contributors
Updated on May 08, 2025
Latest Version
1.0.2
Package Id
express-sequelize-kit-mb@1.0.2
Unpacked Size
52.77 kB
Size
9.76 kB
File Count
6
NPM Version
8.5.5
Node Version
17.8.0
Published on
Sep 28, 2024
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
4
express-sequelize-kit-mb is a reusable package for backend development using Node.js, Express, and Sequelize, featuring customizable CRUD operations and commonly used API and repository functions. It promotes clean code, encapsulation, and DRY (Don't Repeat Yourself) principles for easy integration into various projects.
1npm install express-sequelize-kit-mb
Also, install dependencies:
1npm install express sequelize mysql2 dotenv cors
1├── config 2│ └── dbConfig.js 3├── controllers 4│ └── UserController.js 5├── models 6│ └── userModel.js 7├── repository 8│ └── userRepository.js 9├── services 10│ └── userService.js 11├── routes 12│ └── userRoutes.js 13├── index.js
dbConfig.js
)Configure your database connection using Sequelize:
1const { Sequelize } = require("sequelize"); 2require("dotenv").config(); 3 4const sequelize = new Sequelize(process.env.DB_NAME, process.env.DB_USER, process.env.DB_PASSWORD, { 5 host: process.env.DB_HOST, 6 dialect: "mysql" 7}); 8 9sequelize 10 .authenticate() 11 .then(() => { 12 console.log("Connection has been established successfully."); 13 }) 14 .catch((err) => { 15 console.error("Unable to connect to the database:", err); 16 }); 17 18module.exports = sequelize;
userModel.js
)Create a Sequelize model for User
:
1const { DataTypes } = require("sequelize"); 2const sequelize = require("../config/dbConfig"); 3 4const UserModel = sequelize.define("User", { 5 userId: { 6 type: DataTypes.INTEGER, 7 autoIncrement: true, 8 primaryKey: true 9 }, 10 name: { 11 type: DataTypes.STRING(100), 12 allowNull: false 13 }, 14 age: { 15 type: DataTypes.INTEGER 16 }, 17 deleteFlag: { 18 type: DataTypes.TINYINT, 19 defaultValue: 0 20 } 21}, { 22 timestamps: true 23}); 24 25module.exports = UserModel;
userRepository.js
)1// Import the User model 2const UserModel = require("../models/userModel"); 3 4// Import the Repository class from express-sequelize-kit-mb package 5const { Repository } = require("express-sequelize-kit-mb"); 6 7// Define the UserRepository class extending the generic Repository 8class UserRepository extends Repository { 9 constructor() { 10 // Call the parent Repository constructor and pass necessary parameters: 11 // 1. UserModel: The Sequelize model for the User table 12 // 2. Soft delete field: Leave as an empty string if not using soft delete 13 // 3. Soft delete option: Set to false to disable soft deletion 14 15 // If soft delete is required, set the field name (e.g., "deleteFlag") and true as the second and third arguments. 16 super(UserModel, "deleteFlag", true); 17 } 18} 19 20// Create an instance of the UserRepository 21const userRepository = new UserRepository(); 22 23// Export the userRepository instance to be used in the service layer 24module.exports = { userRepository }; 25
userService.js
)1// Import the userRepository instance from the userRepository file 2const { userRepository } = require("../repository/userRepository"); 3 4// Import the generic Service class from the express-sequelize-kit-mb package 5const { Service } = require("express-sequelize-kit-mb"); 6 7// Define the UserService class extending the generic Service 8class UserService extends Service { 9 constructor() { 10 // Call the parent Service constructor and pass the userRepository instance 11 // This allows the service layer to utilize the repository's methods 12 super(userRepository); 13 } 14} 15 16// Create an instance of the UserService 17let userService = new UserService(); 18 19// Export the userService instance to be used in the controller layer 20module.exports = { userService }; 21
UserController.js
)Handle incoming requests in the controller:
1// Import the generic Controller class from the express-sequelize-kit-mb package 2const { Controller } = require("express-sequelize-kit-mb"); 3 4// Import the userService instance from the userService file 5const { userService } = require("../service/userService"); 6 7 8// Define the UserController class extending the generic Controller 9class UserController extends Controller { 10 constructor() { 11 // Call the parent Controller constructor and pass the following: 12 // 1. userService: The service layer object to handle business logic and repository interaction 13 // 2. Enable logs: Set to true if you want to enable logging for actions within the controller 14 super(userService, true); 15 } 16} 17 18// Create an instance of the UserController 19let userController = new UserController(); 20 21// Export the userController instance to be used in the route layer 22module.exports = { userController }; 23 24
userRoutes.js
)Route API requests to the correct controller methods:
1// Import Express framework 2const express = require("express"); 3 4// Create a new router instance 5const route = express.Router(); 6 7// Import the userController instance from the UserController file 8const { userController } = require("../controllers/UserController"); 9 10// Import middlewares for validation and assigning organization data 11const { validateFields } = require("../middlewares/validationMiddleware"); 12const { assignOrgInfo } = require("../middlewares/routeMiddlewares"); 13 14// Define routes for user-related operations 15 16// POST route to save user data 17// Uses the validateFields middleware to ensure 'name' and 'age' are provided 18// The assignOrgInfo middleware can be used to assign user-related data such as organizationId from the JWT token 19// If you want to modify or assign additional data (like roles, user permissions), you can use middlewares to do so before passing the data to the controller same like given below 20route.post("/User-Save", validateFields("name", "age"), assignOrgInfo, userController.saveData); 21 22// PATCH route to update user data by ID 23// You can add middlewares here if you need to modify the request data before updating the user record 24route.patch("/User-Update/:id", userController.updateData); 25 26// DELETE route to delete user data by ID 27// Middlewares can be used here to apply additional checks before deletion (e.g., role-based access control) 28route.delete("/User-Delete/:id", userController.deleteData); 29 30// GET route to fetch user data by ID 31// If you need to filter or modify the response, use a middleware before calling the controller method 32route.get("/User-Get-Data-By-Id/:id", userController.getDataById); 33 34// GET route to fetch all users 35// Middlewares can be added to modify or limit the data being fetched (e.g., based on user roles, organizations, etc.) 36route.get("/User-Get-All-Data", userController.getAllData); 37 38// GET route to fetch all users with pagination 39// If you want to customize pagination or filtering criteria dynamically, middlewares can handle those assignments before the controller processes the request 40route.get("/User-Get-All-Data-By-Pagination", userController.getAllDataWithPagination); 41 42// Export the route to be used in other parts of the application 43module.exports = route; 44 45
index.js
)Integrate everything into the main app:
1// Load environment variables from .env file 2require("dotenv").config(); 3 4// Import Express and CORS modules 5const express = require("express"); 6const cors = require("cors"); 7 8// Initialize Express app 9const app = express(); 10 11// Middleware to parse JSON requests 12app.use(express.json()); 13 14// Enable CORS (Cross-Origin Resource Sharing) for all origins 15app.use(cors({ origin: "*" })); 16 17// Import and configure the database connection 18require("./config/dbConfig"); 19 20// Import User routes 21const UserRoutes = require("./routes/userRoutes"); 22 23// Use the imported User routes for all requests to /User endpoint 24app.use("/User", UserRoutes); 25 26// Load relationships between models (associations) 27require("./models/relationships"); 28 29// Start the Express server on port 3000 30app.listen(3000, () => { 31 console.log("Server is started on port 3000"); 32}); 33 34
Start the server by running:
1npm start
Your API will be available at http://localhost:3000/
.
For a complete demo, check out the demo repository.
You can explore and test the API using Postman. Click the link below to view the Postman collection:
Postman Collection for express-sequelize-kit-mb
Mangesh Balkawade
GitHub
No vulnerabilities found.
No security vulnerabilities found.