Server Side Pagination In Node js MySQL- Express RestAPIs + Sequelize ORM Pagination Filtering Sort
Article: https://loizenai.com/tutorial-nodejs-...
Facing with a large dataset is one of the most user-case in software development, and we need to present it to the user in smaller chunks. To resolve the problem, pagination and sorting is a way to handle it. So in the tutorial, I guide step by step with clearly code to implement a Node.js project with Express, Sequelize and MySQL database to provide a set of RestAPIs for pagination filtering and sorting.
I. Overall Node.js Sequelize MySQL Pagination RestAPIs
To handling Pagination RestAPI requests and do Paging Filtering and Sorting queries with MySQL database, we create a backend web Node.js application with 4 main points:
1. To handle pagination RestAPI requests with Node.js, We use Express framework.
2. To do pagination filtering and sorting queries with MySQL database, We use Sequelize ORM.
3. We define all RestAPI URLs in router.js.
4. We implement code how to process each paging filtering and sorting RestAPI request in controller.js file.
II. Goal
We define a set of 3 RestAPIs to do the pagination, filtering and sorting queries with MySQL databases as below list:
1. Filtering Request – a GET RestAPI /api/customers/filteringbyage is used to fetch all records from MySQL with a filtering by age
2. Pagination Request – a GET RestAPI /api/customers/pagination is used to fetch data from MySQL with pagination purpose.
3. Pagination Filtering and Sorting – a GET RestAPI /api/customers/pagefiltersort is defined to fetch data from MySQL with pagination, filtering by age and ordering by 2 fields firstname and lastname
III. Sequelize Pagination Filtering Sorting Queries
To do the pagination with database, Sequelize ORM provides 2 model methods for supporting the purpose with limit and offset parameters:
1 - .findAll() - Search for multiple elements in the database
2 - .findAndCountAll() - Search for multiple elements in the database, returns both data and total count
How about limit & offset for nodejs pagination?
limit is the maximum number of records to fetch
offset is the quantity of records to skip
IV. Structure of Video
0:06 - Introduce Project Overview and Architecture of Nodejs Project Structure.
1:00 - Download Sourcecode: Nodejs Pagination And Sorting with MySQL
1:24 - Install needed dependencies for Nodejs project
1:36 - Import Nodejs Project to Visual Studio Code
1:47 - REview project structure of Sourcecode
1:52 - review server.js file for creating Express Application
2:04 - Review env.js file for MySQL environment configuration
2:08 - db.config.js file for configure Sequelize ORM with MySQL database
2:14 - create Sequelize model customer.model.js file
2:23 - check the router.js file for endpoint RestAPIs
2:27 - controller.js file for implement how RestAPIs working with Sequelize Queries to do pagination with MySQL
2:36 - view package.json file for needed dependencies
2:40 - Start debuging with nodejs project
2:55 - Check created customers table in MYSQL database
3:13 - prepare data for Customers table
--DEBUG TIME--
3:29 - Do the first testcase - DEGBUG with filtering RESTAPI age=23
5:13 - Do the DEBUG mode for the filtering restapi with age=18
5:52 - DO the DEBUG with Nodejs pagination RestAPI page=1 & limit=5
7:05 DO the DEBUG with Nodejs pagination RestAPI page=4 & limit=5
7:42 - Do the Debug with Nodejs Pagination RestAPI page=5 & limit=5
8:02 - Do the Debug with Pagination Filtering and Sorting with page=1 & limit=5 & age=23
9:51 - Do the Debug with Pagination Filtering and Sorting with page=2 & limit=5 & age=23
Related posts:
1. Nodejs JWT Authentication Example – Express RestAPIs + JSON Web Token + BCryptjs + Sequelize + MySQL/PostgreSQL
https://loizenai.com/nodejs-jwt-authe...
2. Angular Nodejs Fullstack CRUD Application with MySQL/PostgreSQL – Angular 10-9-8 HttpClient + Nodejs Express, Sequelize ORM
https://loizenai.com/angular-nodejs-f...
3. Build Nodejs CRUD Application with MySQL/PostgreSQL – Express RestAPIs + Ajax : Post/Get/Put/Delete Request
https://loizenai.com/build-nodejs-cru...
4. Tutorial Nodejs Express RestAPIs MySQL with Sequelize Queries Examples
https://loizenai.com/tutorial-nodejs-...
Sourcecode at:
https://loizenai.com/tutorial-nodejs-...
@Copyright by: https://loizenai.com
Youtube: loizenai