Secret to configuring the best Postgres-Nest project- Part 1

bhargavmantha

BhargavMantha

Posted on July 8, 2021

Secret to configuring the best Postgres-Nest project- Part 1

Header Image
Connecting to the Postgres server using Nest Js project

Notations
This project is divided into 4 parts

  1. My Computer setup
  2. Prerequisites
  3. Installation
  4. Project setup
  5. Explanation

My Computer setup

  1. OS: Linux x64 Arch
  2. Kernal- 1.10.42-1
  3. Distrubution- Manajro KDE
  4. Code editor: VS CODE-Insiders
  5. Ram - 16 GB
  6. AMD ryzen 7 pro

Prerequisites

  1. This tutorial assumes that you have a basic knowledge of Node js, Postgres, and Docker
  2. Node and npm installed
  3. Docker Installed

Installation
PART-1 ( Docker )

Pull postgres Image
docker run --rm --name pg-docker -e POSTGRES_PASSWORD=poc-estm -d -p 5432:5432 -v $HOME/docker/volumes/postgres:/var/lib/postgresql/data postgres
Enter fullscreen mode Exit fullscreen mode

--rm is used to remove the constaine if it exists
--name is used to specify the name of the container
-e environment variables
-d in detached mode
-p port number to run on
-v volume to store the data internal to the container on
postgres the image to pull
The below command will interactively log in to the TTY terminal of the container

docker exec -it pg-docker bash
once inside the psql prompt login to the DB and create a DB of ur choice (will be used in the code )
psql -h localhost -U postgres -d postgres
docker docs referances

PART-2 ( Nest Installation )

Install nest Globally

npm i -g @nestjs/cli

create a new project in the desigred location using the following command

nest new <project-name>

Project Setup
Install the required Dependencies
run the following command to install the dependicies for typeorm and postgres
npm install --save @nestjs/typeorm typeorm mysql2
now run the following command in the new project folder created
nest generate module db
this command will generate

  1. create a folder named db
  2. a module named db.module.ts in the above db folder we will need 2 other files basically interfaces and classes db.interfaces.ts db.interfaces.ts the db.errors.ts db.errors.ts lets now populate our db.module.ts file db.module.ts

The most important part of this article

I am using convict to set up the environment variables convict
If you look at lines 26 to 36 getConnectionOptions() this function is responsible for creating the JSON that will act as the connector in Nest

  1. The type is the type of the database that you want to connect to it can be (PostgreSQL, Oracle, Microsoft SQL Server, SQLite, and even NoSQL databases like MongoDB)
  2. host the the dbhost
  3. post is the port you want to use to connect to the DB in our case it is 5432`
  4. username and password for the DB
  5. the database databaseName
  6. keepConnectionAlive is necessary to keep a connection open with the DB
  7. the remaining part of the code we will revisit in the next part of the article !!! you can also use a db URL to connect to the database in that case just replace the entire JSON from line 23 to 29 with url:<db-url> app.module.ts app.module.ts

update your app.module.ts to look like above

The forRoot() is important here it is responsible for returning a DynamicModule which will create the connection for us with the Postgres DB

Please ignore lines 3 and 4 I will come to it in the next part of the article :)

Now we are ready to test ;)
run the following command in the terminal-
npm run start:dev

If all goes well you will get no errors in case of any please revisit the steps to check what is different.

See you in the next part 😀

💖 💪 🙅 🚩
bhargavmantha
BhargavMantha

Posted on July 8, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related