Using Node-Postgres in Node.js Express

Tram Ho

In this article, we’ll walk you through a step-by-step guide on using node-postgres with Node.js Express. We’ll apply it to a real project, making it easy to understand, flexible, and scalable. We’ll also implement some important mechanisms like singleton, repository, query on two databases for read and write, and a reconnect mechanism when disconnected from the database.

Introduction to Node-Postgres

Node-Postgres is a popular PostgreSQL client library for Node.js. It allows you to interact with a PostgreSQL database easily and efficiently. With its simple API, you can execute queries, manage transactions, and utilize connection pooling for better performance.

Source Structure

Here’s an overview of the source structure for this project:

Setting Up the Project

Before diving into the code, make sure you have the following installed:

  • Node.js (14.x or higher)
  • PostgreSQL (9.6 or higher)

If you have docker, you can also run this project without these.

First, create a new directory for your project and navigate to it:

Initialize a new Node.js project and install the necessary dependencies:

Creating a Singleton Connection Pool

A connection pool is a cache of database connections maintained to improve performance. To create a singleton connection pool, we’ll use the pg package and the Singleton design pattern.

Create a new file called db.js and paste the following code:

Now, you can use the Singleton class to get a connection pool instance in other parts of your application.

Implementing the Repository Pattern

The repository pattern helps abstract the data access logic, making it more maintainable and testable. Create a new folder called repositories and create a new file inside it called userRepository.js:

Environment Configuration

Create a .env file in the project root directory to store the environment variables:

Make sure to replace your_read_db_connection_string and your_write_db_connection_string with your actual PostgreSQL connection strings.

Package.json and Entry Point

You can also modify the package.json file in the project root directory with the following content:

If you have modified the package.json file re-run this command:

Create an index.js file in the project root directory:

Now you have a complete Node.js Express application using node-postgres, with a singleton connection pool, the repository pattern, querying multiple databases for read and write operations, and a reconnect mechanism when disconnected from the database.

To run the application, use the following command if you have available the Postgres DB if not then the next step:

Docker Configuration and Initialize Sample Data

To initialize the sample data for the users table, we can use a SQL script that runs when the PostgreSQL container starts. Create a new folder called initdb in your project’s root directory, and inside the initdb folder, create a file called init.sql with the following content:

This script will create a users table if it doesn’t exist and insert three sample users into the table.

Let’s create a new file called Dockerfile and paste the following code:

Now, create a new file called docker-compose.yml and paste the following code:

To run the application with Docker Compose, use the following command:

When the containers start, the users table will be created and populated with sample data. And the node application is also started in the container.

Conclusion

In this article, we’ve covered how to use node-postgres with Node.js Express, implementing a singleton connection pool, the repository pattern, querying multiple databases for read and write operations, and a reconnect mechanism. By following this guide, you can build a flexible, scalable, and maintainable application using node-postgres.

And Finally

As always, I hope you enjoyed this article and learned something new.
Thank you and see you in the next articles!

If you liked this article, please give me a like and subscribe to support me. Thank you.

Ref

Share the news now

Source : Viblo