Rishabh570 / pagination-nodejs

Implements cursor-based pagination with Node.js and PostgreSQL

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

pagination-nodejs

Implements cursor-based pagination with Node.js and PostgreSQL

Pre-requisites

  • PostgreSQL server installed and running on your local machine (I prefer psql to interact with the database).
  • Node.js and npm installed with Node.js version >= 16.
  • Basic understanding of REST API with Express.js framework.

Installation

  1. Clone the repo
  git clone git@github.com:Rishabh570/pagination-nodejs.git
  1. Install the dependencies
  npm install
  1. Source the environment variables

You can find the ENV variables being used in config.js at the project's root level. Rename the .env.example file to .env and fill in the desired values for the environment variables.

If your Postgres is not set to trust local connections, provide the correct password for your Postgres user. To turn off password authentication, edit your pg_hba.conf (run SHOW hba_file; as a superuser to know the file location) file to trust all local connections. It should look like this:

host    all             all             0.0.0.0/0             trust

Once done, you can do source .env on macOS to load environment variables. Otherwise, try replacing "source" with "." (for more information, refer to this) or load the environment variables directly into the terminal.

  1. Run the server
  npm start
  1. Create a table in your Postgres database. Connect to your Postgres database locally and run the below command to create a table named "items".
  create table items (
    id bigint generated by default as identity,
    created_at timestamp with time zone not null default now(),
    name text not null,
    description text null,
    constraint item_pkey primary key (id),
    constraint item_id_key unique (id),
    constraint item_name_key unique (name)
  );
  1. Load sample data in your items table by hitting the API endpoint - POST /item/create.

About

Implements cursor-based pagination with Node.js and PostgreSQL

License:MIT License


Languages

Language:JavaScript 100.0%