Skip to content

Commit b137613

Browse files
committed
Angular 10 Nodejs PostgreSQL CRUD Examples with Sequelize ORM and Express Framework - Backend Nodejs development
1 parent f5f6c05 commit b137613

File tree

8 files changed

+936
-0
lines changed

8 files changed

+936
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const env = require('./env.js');
2+
3+
const Sequelize = require('sequelize');
4+
const sequelize = new Sequelize(env.database, env.username, env.password, {
5+
host: env.host,
6+
dialect: env.dialect,
7+
operatorsAliases: false,
8+
9+
pool: {
10+
max: env.max,
11+
min: env.pool.min,
12+
acquire: env.pool.acquire,
13+
idle: env.pool.idle
14+
}
15+
});
16+
17+
const db = {};
18+
19+
db.Sequelize = Sequelize;
20+
db.sequelize = sequelize;
21+
22+
db.Customer = require('../models/customer.model.js')(sequelize, Sequelize);
23+
24+
module.exports = db;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const env = {
2+
database: 'loizenaidb',
3+
username: 'postgres',
4+
password: '123',
5+
host: 'localhost',
6+
dialect: 'postgres',
7+
pool: {
8+
max: 5,
9+
min: 0,
10+
acquire: 30000,
11+
idle: 10000
12+
}
13+
};
14+
15+
module.exports = env;
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
const db = require('../config/db.config.js');
2+
const Customer = db.Customer;
3+
4+
/**
5+
* Save a Customer object to database MySQL/PostgreSQL
6+
* @param {*} req
7+
* @param {*} res
8+
*/
9+
exports.create = (req, res) => {
10+
let customer = {};
11+
12+
try{
13+
// Building Customer object from upoading request's body
14+
customer.firstname = req.body.firstname;
15+
customer.lastname = req.body.lastname;
16+
customer.address = req.body.address;
17+
customer.age = req.body.age;
18+
19+
// Save to MySQL database
20+
Customer.create(customer).then(result => {
21+
// send uploading message to client
22+
res.status(200).json({
23+
message: "Upload Successfully a Customer with id = " + result.id,
24+
customers: [result],
25+
error: ""
26+
});
27+
});
28+
}catch(error){
29+
res.status(500).json({
30+
message: "Fail!",
31+
customers: [],
32+
error: error.message
33+
});
34+
}
35+
}
36+
37+
/**
38+
* Retrieve Customer information from database
39+
* @param {*} req
40+
* @param {*} res
41+
*/
42+
exports.retrieveAllCustomers = (req, res) => {
43+
// find all Customer information from
44+
try{
45+
Customer.findAll({attributes: ['id', 'firstname', 'lastname', 'age', 'address']})
46+
.then(customerInfos => {
47+
res.status(200).json({
48+
message: "Get all Customers' Infos Successfully!",
49+
customers: customerInfos,
50+
error: ""
51+
});
52+
})
53+
}catch(error) {
54+
// log on console
55+
console.log(error);
56+
57+
res.status(500).json({
58+
message: "Error!",
59+
customers: [],
60+
error: error
61+
});
62+
}
63+
}
64+
65+
/**
66+
* Updating a Customer
67+
* @param {*} req
68+
* @param {*} res
69+
*/
70+
exports.updateById = async (req, res) => {
71+
try{
72+
let customerId = req.params.id;
73+
let customer = await Customer.findByPk(customerId);
74+
75+
if(!customer){
76+
// return a response to client
77+
res.status(404).json({
78+
message: "Not Found for updating a customer with id = " + customerId,
79+
customers: [],
80+
error: "404"
81+
});
82+
} else {
83+
// update new change to database
84+
let updatedObject = {
85+
firstname: req.body.firstname,
86+
lastname: req.body.lastname,
87+
address: req.body.address,
88+
age: req.body.age
89+
}
90+
let result = await Customer.update(updatedObject, {returning: true, where: {id: customerId}});
91+
92+
// return the response to client
93+
if(!result) {
94+
res.status(500).json({
95+
message: "Error -> Can not update a customer with id = " + req.params.id,
96+
error: "Can NOT Updated",
97+
customers: []
98+
});
99+
}
100+
101+
res.status(200).json({
102+
message: "Update successfully a Customer with id = " + customerId,
103+
customers: [updatedObject],
104+
error: ""
105+
});
106+
}
107+
} catch(error){
108+
res.status(500).json({
109+
message: "Error -> Can not update a customer with id = " + req.params.id,
110+
error: error.message,
111+
customers: []
112+
113+
});
114+
}
115+
}
116+
117+
/**
118+
* Delete a Customer by ID
119+
* @param {*} req
120+
* @param {*} res
121+
*/
122+
exports.deleteById = async (req, res) => {
123+
try{
124+
let customerId = req.params.id;
125+
let customer = await Customer.findByPk(customerId);
126+
127+
if(!customer){
128+
res.status(404).json({
129+
message: "Does Not exist a Customer with id = " + customerId,
130+
error: "404",
131+
customers: []
132+
});
133+
} else {
134+
await customer.destroy();
135+
res.status(200).json({
136+
message: "Delete Successfully a Customer with id = " + customerId,
137+
customers: [customer],
138+
error: ""
139+
});
140+
}
141+
} catch(error) {
142+
res.status(500).json({
143+
message: "Error -> Can NOT delete a customer with id = " + req.params.id,
144+
error: error.message,
145+
customers: []
146+
});
147+
}
148+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module.exports = (sequelize, Sequelize) => {
2+
const Customer = sequelize.define('customer', {
3+
id: {
4+
type: Sequelize.INTEGER,
5+
autoIncrement: true,
6+
primaryKey: true
7+
},
8+
firstname: {
9+
type: Sequelize.STRING
10+
},
11+
lastname: {
12+
type: Sequelize.STRING
13+
},
14+
address: {
15+
type: Sequelize.STRING
16+
},
17+
age: {
18+
type: Sequelize.INTEGER
19+
}
20+
});
21+
22+
return Customer;
23+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
let express = require('express');
2+
let router = express.Router();
3+
4+
const customers = require('../controllers/controller.js');
5+
6+
router.post('/api/customers/create', customers.create);
7+
router.get('/api/customers/retrieveinfos', customers.retrieveAllCustomers);
8+
router.put('/api/customers/updatebyid/:id', customers.updateById);
9+
router.delete('/api/customers/deletebyid/:id', customers.deleteById);
10+
11+
module.exports = router;

0 commit comments

Comments
 (0)