Skip to content

Commit 2db1144

Browse files
committed
Ajax Pagination Node.js MySQL RestAPIs Example – Client Side Pagination in Node.js Tutorial
1 parent f42bcf8 commit 2db1144

File tree

10 files changed

+1255
-0
lines changed

10 files changed

+1255
-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: 'root',
4+
password: '12345',
5+
host: 'localhost',
6+
dialect: 'mysql',
7+
pool: {
8+
max: 5,
9+
min: 0,
10+
acquire: 30000,
11+
idle: 10000
12+
}
13+
};
14+
15+
module.exports = env;
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
/**
2+
* Copyright by https://loizenai.com
3+
* youtube loizenai
4+
*/
5+
6+
const db = require('../config/db.config.js');
7+
const Customer = db.Customer;
8+
const Sequelize = db.Sequelize;
9+
10+
11+
exports.create = (req, res) => {
12+
let customer = {};
13+
14+
try{
15+
// Building Customer object from upoading request's body
16+
customer.firstname = req.body.firstname;
17+
customer.lastname = req.body.lastname;
18+
customer.address = req.body.address;
19+
customer.age = req.body.age;
20+
customer.salary = req.body.salary;
21+
22+
// Save to MySQL database
23+
Customer.create(customer).then(result => {
24+
// send uploading message to client
25+
res.status(200).json({
26+
message: "Upload Successfully a Customer with id = " + result.id,
27+
customer: result,
28+
});
29+
});
30+
}catch(error){
31+
res.status(500).json({
32+
message: "Fail!",
33+
error: error.message
34+
});
35+
}
36+
}
37+
38+
exports.pagingfilteringsorting = async (req, res) => {
39+
try{
40+
let page = parseInt(req.query.page);
41+
let limit = parseInt(req.query.size);
42+
let agesorting = (req.query.agesorting === 'true');
43+
let desc = (req.query.desc === 'true');
44+
let salary = req.query.salary ? parseInt(req.query.salary) : -1;
45+
46+
const offset = page ? page * limit : 0;
47+
48+
console.log("offset = " + offset);
49+
50+
let result = {};
51+
52+
// NOT Filtering with salary
53+
if(salary < 0 ){
54+
// not sorting with age
55+
if(agesorting == false) {
56+
result = await Customer.findAndCountAll({
57+
attributes: ['id', 'firstname', 'lastname', 'age', 'address', 'salary', 'copyrightby'],
58+
limit: limit,
59+
offset:offset
60+
});
61+
} else {
62+
if(desc == false) { // sorting with age and ascending
63+
result = await Customer.findAndCountAll({
64+
attributes: ['id', 'firstname', 'lastname', 'age', 'address', 'salary', 'copyrightby'],
65+
limit: limit,
66+
offset:offset,
67+
order: [
68+
['age', 'ASC']
69+
]
70+
});
71+
} else { // sorting with age and descending
72+
result = await Customer.findAndCountAll({
73+
attributes: ['id', 'firstname', 'lastname', 'age', 'address', 'salary', 'copyrightby'],
74+
limit: limit,
75+
offset:offset,
76+
order: [
77+
['age', 'DESC']
78+
]
79+
});
80+
}
81+
}
82+
} else { // Filtering with salary
83+
// not sorting with age
84+
if(agesorting == false) {
85+
result = await Customer.findAndCountAll({
86+
attributes: ['id', 'firstname', 'lastname', 'age', 'address', 'salary', 'copyrightby'],
87+
where: {salary: salary},
88+
limit: limit,
89+
offset:offset
90+
});
91+
} else {
92+
if(desc == false) { // sorting with age and ascending
93+
result = await Customer.findAndCountAll({
94+
attributes: ['id', 'firstname', 'lastname', 'age', 'address', 'salary', 'copyrightby'],
95+
where: {salary: salary},
96+
limit: limit,
97+
offset:offset,
98+
order: [
99+
['age', 'ASC']
100+
]
101+
});
102+
} else { // sorting with age and descending
103+
result = await Customer.findAndCountAll({
104+
attributes: ['id', 'firstname', 'lastname', 'age', 'address', 'salary', 'copyrightby'],
105+
where: {salary: salary},
106+
limit: limit,
107+
offset:offset,
108+
order: [
109+
['age', 'DESC']
110+
]
111+
});
112+
}
113+
}
114+
}
115+
116+
const totalPages = Math.ceil(result.count / limit);
117+
const response = {
118+
"copyrightby": "https://loizenai.com",
119+
"totalPages": totalPages,
120+
"pageNumber": page,
121+
"pageSize": result.rows.length,
122+
"customers": result.rows
123+
};
124+
res.send(response);
125+
}catch(error) {
126+
res.status(500).send({
127+
message: "Error -> Can NOT complete a paging request!",
128+
error: error.message,
129+
});
130+
}
131+
}
132+
133+
exports.getSalaries = (req, res) => {
134+
Customer.findAll({
135+
attributes: [
136+
[Sequelize.fn('DISTINCT', Sequelize.col('salary')), 'salary'],
137+
],
138+
order: [
139+
['salary', 'ASC']
140+
],
141+
}).then(result => {
142+
let salaries = result.map(x => x.salary);
143+
res.send(salaries);
144+
}).catch(error => {
145+
res.status(500).send({
146+
message: "Error -> Can NOT get all customer's salaries",
147+
error: error.message
148+
});
149+
});
150+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
salary: {
21+
type: Sequelize.INTEGER
22+
},
23+
copyrightby: {
24+
type: Sequelize.STRING,
25+
defaultValue: 'https://loizenai.com'
26+
}
27+
});
28+
29+
return Customer;
30+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
let express = require('express');
2+
let router = express.Router();
3+
4+
const customers = require('../controllers/controller.js');
5+
6+
let path = __basedir + '/view/';
7+
8+
router.get('/', (req,res) => {
9+
console.log("__basedir" + __basedir);
10+
res.sendFile(path + "index.html");
11+
});
12+
13+
router.post('/api/customers/create', customers.create);
14+
router.get('/api/customers/pagefiltersort', customers.pagingfilteringsorting);
15+
router.get('/api/customers/salaries', customers.getSalaries);
16+
17+
module.exports = router;

0 commit comments

Comments
 (0)