Last active
July 12, 2019 08:33
-
-
Save leovarmak/1014319c691bbc2ac26c6046706be571 to your computer and use it in GitHub Desktop.
Sequelize boilerplate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let express = require("express"); | |
let mysql = require("mysql"); | |
const Sequelize = require("sequelize"); | |
let server = express(); | |
const port = 3000; | |
const sequelize = new Sequelize("mysql://root:*******@localhost:3306/CRUD"); | |
const Employee = sequelize.define("Employees", { | |
id: { | |
type: Sequelize.INTEGER, | |
autoIncrement: true, | |
allowNull: false, | |
primaryKey: true | |
}, | |
first_name: { | |
type: Sequelize.STRING(30), | |
allowNull: false | |
}, | |
last_name: { | |
type: Sequelize.STRING(30), | |
allowNull: false | |
}, | |
phone: { | |
type: Sequelize.CHAR(10), | |
allowNull: false | |
}, | |
salary: { | |
type: Sequelize.INTEGER, | |
allowNull: false | |
} | |
}); | |
sequelize | |
.authenticate() | |
.then(() => { | |
console.log("Connection has been established successfully."); | |
}) | |
.catch(err => { | |
console.error("Unable to connect to the database:", err); | |
}); | |
Employee.sync().then(result => { | |
console.log(`Result ${result}`); | |
server.listen(port, function() { | |
console.log("Server started"); | |
}); | |
}); | |
server.get("/read", function(req, res) { | |
Employee.findAll() | |
.then(data => { | |
res.status(200).jsonp({ | |
success: true, | |
response: "Employee records fetched successfully", | |
data: data | |
}); | |
}) | |
.catch(err => { | |
res.status(500).jsonp({ | |
success: false, | |
error: err.message | |
}); | |
}); | |
}); | |
server.post("/create", function(req, res) { | |
let { first_name, last_name, phone, salary } = req.query; | |
Employee.create({ | |
first_name: first_name, | |
last_name: last_name, | |
phone: phone, | |
salary: salary | |
}) | |
.then(data => { | |
res.status(200).jsonp({ | |
success: true, | |
response: "New employee successfully added", | |
data: data | |
}); | |
}) | |
.catch(err => { | |
res.status(500).jsonp({ | |
success: false, | |
error: err.message | |
}); | |
}); | |
}); | |
server.put("/update", function(req, res) { | |
let { first_name, last_name, phone, salary } = req.query; | |
Employee.update( | |
{ | |
first_name: first_name, | |
last_name: last_name, | |
phone: phone, | |
salary: salary | |
}, | |
{ | |
where: { | |
phone: phone | |
} | |
} | |
) | |
.then(data => | |
res.status(200).jsonp({ | |
success: true, | |
response: "Employee record updated successfully", | |
data: data | |
}) | |
) | |
.catch(err => { | |
res.status(500).jsonp({ | |
success: false, | |
error: err.message | |
}); | |
}); | |
}); | |
server.delete("/delete", function(req, res) { | |
let { phone } = req.query; | |
Employee.destroy({ | |
where: { | |
phone: phone | |
} | |
}) | |
.then(data => { | |
res.status(200).jsonp({ | |
success: true, | |
response: "Employee deleted successfully", | |
data: data | |
}); | |
}) | |
.catch(err => { | |
res.status(500).jsonp({ | |
success: false, | |
error: err.message | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment