Languages
[Edit]
EN

Node.js - PostgreSQL - rows pagination

3 points
Created by:
marc_so
234

In this article, we would like to show you how to paginateΒ data rows in the PostgreSQL usingΒ Node.js.

Note:

Pagination consists in dividing data into pages and sending only those rows that the client currently needs - we do not have to send all data at once.

Node.js - PostgreSQL - rows pagination
Data used in the example - HeidiSQL

Note:Β at the end of this article you can find database preparation SQL queries.

const { Client } = require('pg');

const client = new Client({
    host: '127.0.0.1',
    user: 'my_username',
    database: 'my_database',
    password: 'my_password',
    port: 5432,
});

const fetchRows = async (pageNumber, pageSize) => {
    const query = `
            SELECT *
            FROM "users"
            ORDER BY "users"."id"
            LIMIT $2
            OFFSET (($1 - 1) * $2);
    `;
    await client.connect(); // creates connection
    try {
        const { rows } = await client.query(query, [pageNumber, pageSize]); // sends query
        return rows;
    } finally {
        await client.end(); // closes connection
    }
};

fetchRows(1, 3) // fetch 3 rows from page 1
    .then(result => console.table(result))
    .catch(error => console.error(error.stack));

Result:Β 

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ (index) β”‚ id β”‚  name   β”‚  surname  β”‚ department_id β”‚  salary   β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚    0    β”‚ 1  β”‚ 'John'  β”‚ 'Stewart' β”‚       1       β”‚ '2000.00' β”‚
β”‚    1    β”‚ 2  β”‚ 'Chris' β”‚  'Brown'  β”‚       3       β”‚ '2000.00' β”‚
β”‚    2    β”‚ 3  β”‚ 'Chris' β”‚  'Lewis'  β”‚       3       β”‚ '2000.00' β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Result for fetchRows(2, 4):

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ (index) β”‚ id β”‚    name     β”‚  surname  β”‚ department_id β”‚  salary   β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚    0    β”‚ 5  β”‚   'Kate'    β”‚ 'Stewart' β”‚       3       β”‚ '2000.00' β”‚
β”‚    1    β”‚ 6  β”‚  'Ailisa'   β”‚  'Lewis'  β”‚       3       β”‚ '2000.00' β”‚
β”‚    2    β”‚ 7  β”‚  'Ailisa'   β”‚  'Gomez'  β”‚       3       β”‚ '3000.00' β”‚
β”‚    3    β”‚ 8  β”‚ 'Gwendolyn' β”‚  'James'  β”‚       2       β”‚ '3000.00' β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Database preparation

create_tables.sqlΒ file:

CREATE TABLE "users" (
	"id" SERIAL,
	"name" VARCHAR(50) NOT NULL,
	"surname" VARCHAR(50) NOT NULL,
	"department_id" INTEGER,
    "salary" DECIMAL(15,2) NOT NULL,
	PRIMARY KEY ("id")
);

insert_data.sqlΒ file:

INSERT INTO "users"
	( "name", "surname", "department_id", "salary")
VALUES
	('John', 'Stewart', 1, '2000.00'),
	('Chris', 'Brown', 3, '2000.00'),
	('Chris', 'Lewis', 3, '2000.00'),
	('Kate', 'Lewis', 3, '2000.00'),
	('Kate', 'Stewart', 3, '2000.00'),
	('Ailisa', 'Lewis', 3, '2000.00'),
	('Ailisa', 'Gomez', 3, '3000.00'),
	('Gwendolyn', 'James', 2, '3000.00'),
	('Simon', 'James', 2, '2000.00'),
	('Simon', 'Brown', 3, '2000.00'),
	('Simon', 'Collins', 3, '3000.00');

Native SQL query (used in the above example):

SELECT *
FROM "users"
ORDER BY "users"."id"
LIMIT 4
OFFSET 1 * 4;

Node.js - PostgreSQL

Native Advertising
πŸš€
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

β€οΈπŸ’» πŸ™‚

Join