├── .gitignore ├── config └── db.config.js ├── package.json ├── src ├── routes │ └── employee.route.js ├── controllers │ └── employee.controller.js └── models │ └── employee.model.js ├── index.js ├── README.md ├── mysql_query.txt └── NodeMySQLCrudAPI.postman_collection.json /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules 2 | -------------------------------------------------------------------------------- /config/db.config.js: -------------------------------------------------------------------------------- 1 | const mysql = require('mysql'); 2 | 3 | // create here mysql connection 4 | 5 | const dbConn = mysql.createConnection({ 6 | host: 'localhost', 7 | user: 'root', 8 | password: '', 9 | database: 'node_mysql_crud_db' 10 | }); 11 | 12 | dbConn.connect(function(error){ 13 | if(error) throw error; 14 | console.log('Database Connected Successfully!!!'); 15 | }) 16 | 17 | module.exports = dbConn; 18 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nodemysqlcrudapi", 3 | "version": "1.0.0", 4 | "description": "Node MySQL curd API", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1", 8 | "start": "nodemon index" 9 | }, 10 | "author": "", 11 | "license": "ISC", 12 | "dependencies": { 13 | "body-parser": "^1.19.0", 14 | "express": "^4.17.1", 15 | "mysql": "^2.18.1" 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/routes/employee.route.js: -------------------------------------------------------------------------------- 1 | const express = require('express'); 2 | const router = express.Router(); 3 | 4 | const employeeController = require('../controllers/employee.controller'); 5 | 6 | // get all employees 7 | router.get('/', employeeController.getEmployeeList); 8 | 9 | // get employee by ID 10 | router.get('/:id',employeeController.getEmployeeByID); 11 | 12 | // create new employee 13 | router.post('/', employeeController.createNewEmployee); 14 | 15 | // update employee 16 | router.put('/:id', employeeController.updateEmployee); 17 | 18 | // delete employee 19 | router.delete('/:id',employeeController.deleteEmployee); 20 | 21 | module.exports = router; -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const express = require('express'); 2 | const bodyParser = require('body-parser'); 3 | 4 | // create express app 5 | const app = express(); 6 | 7 | // setup the server port 8 | const port = process.env.PORT || 5000; 9 | 10 | // parse request data content type application/x-www-form-rulencoded 11 | app.use(bodyParser.urlencoded({extended: false})); 12 | 13 | // parse request data content type application/json 14 | app.use(bodyParser.json()); 15 | 16 | // define root route 17 | app.get('/', (req, res)=>{ 18 | res.send('Hello World'); 19 | }); 20 | // import employee routes 21 | const employeeRoutes = require('./src/routes/employee.route'); 22 | 23 | // create employee routes 24 | app.use('/api/v1/employee', employeeRoutes); 25 | 26 | // listen to the port 27 | app.listen(port, ()=>{ 28 | console.log(`Express is running at port ${port}`); 29 | }); -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## About Node JS 2 | 3 | The API reference documentation provides detailed information about a function or object in Node.js. This documentation indicates what arguments a method accepts, the return value of that method, and what errors may be related to that method. It also indicates which methods are available for different versions of Node.js: 4 | 5 | - [Node JS Documentation](https://nodejs.org/en/docs/). 6 | 7 | ## About ExpressJS 8 | 9 | Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications. 10 | With a myriad of HTTP utility methods and middleware at your disposal, creating a robust API is quick and easy. 11 | 12 | - [Getting Started with Express JS ](https://expressjs.com/en/starter/installing.html) 13 | 14 | ## How to run it? 15 | 16 | 1) Clone this repository 17 | 2) Install dependencies using below command 18 | npm install 19 | 3) Create database and connection 20 | 4) npm start -------------------------------------------------------------------------------- /mysql_query.txt: -------------------------------------------------------------------------------- 1 | CREATE DATABASE node_mysql_crud_db; 2 | 3 | CREATE TABLE IF NOT EXISTS `employees` ( 4 | `id` BIGINT UNSIGNED AUTO_INCREMENT, 5 | `first_name` VARCHAR(255) NOT NULL, 6 | `last_name` VARCHAR(255) NOT NULL, 7 | `email` VARCHAR(255) NOT NULL, 8 | `phone` VARCHAR(50) NOT NULL, 9 | `organization` VARCHAR(255) NOT NULL, 10 | `designation` VARCHAR(100) NOT NULL, 11 | `salary` DECIMAL(11,2) UNSIGNED DEFAULT 0.00, 12 | `status` TINYINT UNSIGNED DEFAULT 0, 13 | `is_deleted` TINYINT UNSIGNED DEFAULT 0, 14 | `created_at` DATETIME NOT NULL, 15 | `updated_at` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, 16 | PRIMARY KEY (`id`)) 17 | ENGINE = InnoDB; 18 | 19 | 20 | INSERT INTO `node_mysql_crud_db`.`employees` (`first_name`, `last_name`, `email`, `phone`, `organization`, `designation`, `salary`, `status`, `is_deleted`, `created_at`) VALUES ('John', 'Doe', 'johndoe@gmail.com', '1234567890', 'Microsoft', 'Full Stack Developer', '50000.00', '1', '0', '2020-18-07 15:30:30'); 21 | 22 | INSERT INTO `node_mysql_crud_db`.`employees` (`first_name`, `last_name`, `email`, `phone`, `organization`, `designation`, `salary`, `status`, `is_deleted`, `created_at`) VALUES ('Jane', 'Doe', 'janedoe@gmail.com', '9876543210', 'Google', 'Software Engineer', '44500.00', '1', '0', '2020-18-07 15:30:30'); -------------------------------------------------------------------------------- /src/controllers/employee.controller.js: -------------------------------------------------------------------------------- 1 | 2 | const EmployeeModel = require('../models/employee.model'); 3 | 4 | // get all employee list 5 | exports.getEmployeeList = (req, res)=> { 6 | //console.log('here all employees list'); 7 | EmployeeModel.getAllEmployees((err, employees) =>{ 8 | console.log('We are here'); 9 | if(err) 10 | res.send(err); 11 | console.log('Employees', employees); 12 | res.send(employees) 13 | }) 14 | } 15 | 16 | // get employee by ID 17 | exports.getEmployeeByID = (req, res)=>{ 18 | //console.log('get emp by id'); 19 | EmployeeModel.getEmployeeByID(req.params.id, (err, employee)=>{ 20 | if(err) 21 | res.send(err); 22 | console.log('single employee data',employee); 23 | res.send(employee); 24 | }) 25 | } 26 | 27 | // create new employee 28 | exports.createNewEmployee = (req, res) =>{ 29 | const employeeReqData = new EmployeeModel(req.body); 30 | console.log('employeeReqData', employeeReqData); 31 | // check null 32 | if(req.body.constructor === Object && Object.keys(req.body).length === 0){ 33 | res.send(400).send({success: false, message: 'Please fill all fields'}); 34 | }else{ 35 | EmployeeModel.createEmployee(employeeReqData, (err, employee)=>{ 36 | if(err) 37 | res.send(err); 38 | res.json({status: true, message: 'Employee Created Successfully', data: employee.insertId}) 39 | }) 40 | } 41 | } 42 | 43 | // update employee 44 | exports.updateEmployee = (req, res)=>{ 45 | const employeeReqData = new EmployeeModel(req.body); 46 | console.log('employeeReqData update', employeeReqData); 47 | // check null 48 | if(req.body.constructor === Object && Object.keys(req.body).length === 0){ 49 | res.send(400).send({success: false, message: 'Please fill all fields'}); 50 | }else{ 51 | EmployeeModel.updateEmployee(req.params.id, employeeReqData, (err, employee)=>{ 52 | if(err) 53 | res.send(err); 54 | res.json({status: true, message: 'Employee updated Successfully'}) 55 | }) 56 | } 57 | } 58 | 59 | // delete employee 60 | exports.deleteEmployee = (req, res)=>{ 61 | EmployeeModel.deleteEmployee(req.params.id, (err, employee)=>{ 62 | if(err) 63 | res.send(err); 64 | res.json({success:true, message: 'Employee deleted successully!'}); 65 | }) 66 | } -------------------------------------------------------------------------------- /src/models/employee.model.js: -------------------------------------------------------------------------------- 1 | var dbConn = require('../../config/db.config'); 2 | 3 | var Employee = function(employee){ 4 | this.first_name = employee.first_name; 5 | this.last_name = employee.last_name; 6 | this.email = employee.email; 7 | this.phone = employee.phone; 8 | this.organization = employee.organization; 9 | this.designation = employee.designation; 10 | this.salary = employee.salary; 11 | this.status = employee.status ? employee.status : 1; 12 | this.created_at = new Date(); 13 | this.updated_at = new Date(); 14 | } 15 | 16 | // get all employees 17 | Employee.getAllEmployees = (result) =>{ 18 | dbConn.query('SELECT * FROM employees WHERE is_deleted=0', (err, res)=>{ 19 | if(err){ 20 | console.log('Error while fetching employess', err); 21 | result(null,err); 22 | }else{ 23 | console.log('Employees fetched successfully'); 24 | result(null,res); 25 | } 26 | }) 27 | } 28 | 29 | // get employee by ID from DB 30 | Employee.getEmployeeByID = (id, result)=>{ 31 | dbConn.query('SELECT * FROM employees WHERE id=?', id, (err, res)=>{ 32 | if(err){ 33 | console.log('Error while fetching employee by id', err); 34 | result(null, err); 35 | }else{ 36 | result(null, res); 37 | } 38 | }) 39 | } 40 | 41 | // create new employee 42 | Employee.createEmployee = (employeeReqData, result) =>{ 43 | dbConn.query('INSERT INTO employees SET ? ', employeeReqData, (err, res)=>{ 44 | if(err){ 45 | console.log('Error while inserting data'); 46 | result(null, err); 47 | }else{ 48 | console.log('Employee created successfully'); 49 | result(null, res) 50 | } 51 | }) 52 | } 53 | 54 | // update employee 55 | Employee.updateEmployee = (id, employeeReqData, result)=>{ 56 | dbConn.query("UPDATE employees SET first_name=?,last_name=?,email=?,phone=?,organization=?,designation=?,salary=? WHERE id = ?", [employeeReqData.first_name,employeeReqData.last_name,employeeReqData.email,employeeReqData.phone,employeeReqData.organization,employeeReqData.designation,employeeReqData.salary, id], (err, res)=>{ 57 | if(err){ 58 | console.log('Error while updating the employee'); 59 | result(null, err); 60 | }else{ 61 | console.log("Employee updated successfully"); 62 | result(null, res); 63 | } 64 | }); 65 | } 66 | 67 | // delete employee 68 | Employee.deleteEmployee = (id, result)=>{ 69 | // dbConn.query('DELETE FROM employees WHERE id=?', [id], (err, res)=>{ 70 | // if(err){ 71 | // console.log('Error while deleting the employee'); 72 | // result(null, err); 73 | // }else{ 74 | // result(null, res); 75 | // } 76 | // }) 77 | dbConn.query("UPDATE employees SET is_deleted=? WHERE id = ?", [1, id], (err, res)=>{ 78 | if(err){ 79 | console.log('Error while deleting the employee'); 80 | result(null, err); 81 | }else{ 82 | console.log("Employee deleted successfully"); 83 | result(null, res); 84 | } 85 | }); 86 | } 87 | 88 | module.exports = Employee; -------------------------------------------------------------------------------- /NodeMySQLCrudAPI.postman_collection.json: -------------------------------------------------------------------------------- 1 | { 2 | "info": { 3 | "_postman_id": "eda3479b-3bad-4de0-b2be-45c5a5ecce87", 4 | "name": "NodeMySQLCrudAPI", 5 | "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json" 6 | }, 7 | "item": [ 8 | { 9 | "name": "Get All Employees List", 10 | "request": { 11 | "method": "GET", 12 | "header": [], 13 | "url": { 14 | "raw": "http://localhost:5000/api/v1/employee", 15 | "protocol": "http", 16 | "host": [ 17 | "localhost" 18 | ], 19 | "port": "5000", 20 | "path": [ 21 | "api", 22 | "v1", 23 | "employee" 24 | ] 25 | } 26 | }, 27 | "response": [] 28 | }, 29 | { 30 | "name": "Get Employee By ID", 31 | "request": { 32 | "method": "GET", 33 | "header": [], 34 | "url": { 35 | "raw": "http://localhost:5000/api/v1/employee/2", 36 | "protocol": "http", 37 | "host": [ 38 | "localhost" 39 | ], 40 | "port": "5000", 41 | "path": [ 42 | "api", 43 | "v1", 44 | "employee", 45 | "2" 46 | ] 47 | } 48 | }, 49 | "response": [] 50 | }, 51 | { 52 | "name": "Update Employee By ID", 53 | "request": { 54 | "method": "PUT", 55 | "header": [], 56 | "body": { 57 | "mode": "urlencoded", 58 | "urlencoded": [ 59 | { 60 | "key": "first_name", 61 | "value": "Rohit", 62 | "type": "text" 63 | }, 64 | { 65 | "key": "last_name", 66 | "value": "Gupta", 67 | "type": "text" 68 | }, 69 | { 70 | "key": "email", 71 | "value": "rohit@test.com", 72 | "type": "text" 73 | }, 74 | { 75 | "key": "phone", 76 | "value": "0123456787", 77 | "type": "text" 78 | }, 79 | { 80 | "key": "organization", 81 | "value": "Google", 82 | "type": "text" 83 | }, 84 | { 85 | "key": "designation", 86 | "value": "Software Engg", 87 | "type": "text" 88 | }, 89 | { 90 | "key": "salary", 91 | "value": "50000", 92 | "type": "text" 93 | }, 94 | { 95 | "key": "status", 96 | "value": "1", 97 | "description": "1=Active, 0=Inactive", 98 | "type": "text" 99 | } 100 | ] 101 | }, 102 | "url": { 103 | "raw": "http://localhost:5000/api/v1/employee/4", 104 | "protocol": "http", 105 | "host": [ 106 | "localhost" 107 | ], 108 | "port": "5000", 109 | "path": [ 110 | "api", 111 | "v1", 112 | "employee", 113 | "4" 114 | ] 115 | } 116 | }, 117 | "response": [] 118 | }, 119 | { 120 | "name": "Create Employee", 121 | "request": { 122 | "method": "POST", 123 | "header": [], 124 | "body": { 125 | "mode": "urlencoded", 126 | "urlencoded": [ 127 | { 128 | "key": "first_name", 129 | "value": "Rahul", 130 | "type": "text" 131 | }, 132 | { 133 | "key": "last_name", 134 | "value": "Gupta", 135 | "type": "text" 136 | }, 137 | { 138 | "key": "email", 139 | "value": "rahul@test.com", 140 | "type": "text" 141 | }, 142 | { 143 | "key": "phone", 144 | "value": "0123456787", 145 | "type": "text" 146 | }, 147 | { 148 | "key": "organization", 149 | "value": "Google", 150 | "type": "text" 151 | }, 152 | { 153 | "key": "designation", 154 | "value": "Software Engg", 155 | "type": "text" 156 | }, 157 | { 158 | "key": "salary", 159 | "value": "50000", 160 | "type": "text" 161 | }, 162 | { 163 | "key": "status", 164 | "value": "1", 165 | "description": "1=Active, 0=Inactive", 166 | "type": "text" 167 | } 168 | ] 169 | }, 170 | "url": { 171 | "raw": "http://localhost:5000/api/v1/employee/4", 172 | "protocol": "http", 173 | "host": [ 174 | "localhost" 175 | ], 176 | "port": "5000", 177 | "path": [ 178 | "api", 179 | "v1", 180 | "employee", 181 | "4" 182 | ] 183 | } 184 | }, 185 | "response": [] 186 | }, 187 | { 188 | "name": "Delete Employee By ID", 189 | "request": { 190 | "method": "DELETE", 191 | "header": [], 192 | "url": { 193 | "raw": "http://localhost:5000/api/v1/employee/4", 194 | "protocol": "http", 195 | "host": [ 196 | "localhost" 197 | ], 198 | "port": "5000", 199 | "path": [ 200 | "api", 201 | "v1", 202 | "employee", 203 | "4" 204 | ] 205 | } 206 | }, 207 | "response": [] 208 | } 209 | ], 210 | "protocolProfileBehavior": {} 211 | } --------------------------------------------------------------------------------