Embarking on a Tech Journey: Setting up NestJS, TypeORM, and Postgres DB — Gmail sync system

Abdullah Irfan
5 min readOct 14, 2023

Before starting, let’s discuss our technologies through a brief Q&A! However, if you don’t like nitty gritty details, you can skip to setup part!

What is NestJS? Why is it better than other frameworks and why should we use it?

NestJS is a progressive Node.js framework for building efficient, reliable, and scalable server-side applications. This framework combines elements of OOP (Object Oriented Programming), FP (Functional Programming), and FRP (Functional Reactive Programming). But what sets NestJS apart from other frameworks? For starters, it uses TypeScript by default, offering a robust set of tools right out of the gate. The modular development pattern, adaptability with a wide array of libraries, and its rich developer ecosystem are just a few advantages that hold its superiority against other frameworks. Utilizing NestJS can empower developers to structure projects in a maintainable and scalable way, thus providing a stable groundwork for complex backend applications.

What is ORM? What is TypeORM? Why is it better than other ORMs and why should we use it?

ORM, or Object-Relational Mapping, is a technique that lets you interact with your database, like SQL, by using objects in your programming language, as opposed to using SQL queries. Now, pivoting towards TypeORM: it’s an ORM that can run in NodeJS, Browser, Cordova, PhoneGap, Ionic, React Native, NativeScript, Expo, and Electron platforms and can be used with TypeScript and JavaScript. Why opt for TypeORM over other ORMs? A few reasons stand out: its active record and data mapper patterns, wide range of supported database engines, and extensive TypeScript support. Using TypeORM can simplify the process of writing database queries, create a scalable and maintainable codebase, and optimize your development time by using its myriad of built-in methods and utilities.

What is Postgres? Why is it better than other DBs and why should we use it?

PostgreSQL, affectionately known as Postgres, is an open-source relational database management system (RDBMS) emphasizing extensibility and SQL compliance. Among a sea of database options, what makes Postgres shine? Its proven architecture, robustness, transactional integrity, and reliability even in the most demanding environments make it stand out. Moreover, it supports complex queries and large databases, offers vast extensibility, and has a large and active community that can come to your rescue whenever you’re stuck. Opting for Postgres, you will avail yourself of a versatile, dependable, and secure database that’s been successfully employed by various sized enterprises and individual developers alike.

Now, having all our concerned questions cleared, let’s move to setting up a NestJS application! We will be using npm, though some people prefer yarn and I have worked using yarn as well but for the sake of generalization, we will be moving with npm.
We can create nest app by following the documentation and using:

npm i -g @nestjs/cli
nest new project-name

Or we can use:

npx @nestjs/cli new APP_NAME

Since npx allows running packages without globally installing them. This avoids potential conflicts between project dependencies and global packages. So, I used npx, now as show in image below, it will ask to select package manager, since we will be using npm, so we will select it.

Package manager selection

It will then take few minutes to setup the project, installing the required packages and creating boiler plate files for us, this includes ESlint and prettier configuration, boilerplate app module, service, controller and test cases files. In The completion screen will be:

nest app installation complete

Now for code editor, you can use any of your liking, I will be using VSCode. As for Postgres, I will be using pgAdmin to better create/mantain the DB. Next, we will install TypeORM and Postgres packages with command:

npm install @nestjs/typeorm typeorm pg

After installing these packages, we will create DB in Postgres using pgAdmin, we will head to Servers => PostgreSQL 15 => Databases, right click and create new database.

Creating database

Next, we will provide name of the database and will create the database (as shown in image below).

Setting database name

Now to connect Postgres with nest app, we will DB configuration in app.module.ts file. The code will look something like this:

import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';

@Module({
imports: [
TypeOrmModule.forRoot({
type: 'postgres',
host: 'localhost',
port: 5432,
username: 'your_username',
password: 'your_password',
database: 'your_database_name',
entities: [__dirname + '/**/*.entity{.ts,.js}'],
synchronize: true,
}),
],
})
export class AppModule {}

For entities, rather than defining individual entities, we will provide relative path with defined pattern to get all entities in the project. After filling the required DB details, we will start our project in development mode by using start:dev command (you can view all available commands in package.json file), and if everything works fine, the console will look like:

npm run start:dev
Nest application started

Congratulations, our nest application is running on port 3000, in main.ts we can change the port however we like! Though at beginner level our application is ready for development, but this application doesn’t have proper migrations and environment setup. We will discuss in next story on how to set it up. This story code is available on GitHub in feature/configure-postgres-db branch. If you appreciate this work, please show your support by clapping for the story and starring the repository.

Before we conclude, here’s a handy toolset you might want to check out: The Dev’s Tools. It’s not directly related to our tutorial, but we believe it’s worth your attention. The Dev’s Tools offers an expansive suite of utilities tailored for developers, content creators, and digital enthusiasts:

  • Image Tools: Compress single or multiple images efficiently, and craft custom QR codes effortlessly.
  • JSON Tools: Validate, compare, and ensure the integrity of your JSON data.
  • Text Tools: From comparing texts, shuffling letters, and cleaning up your content, to generating random numbers and passwords, this platform has got you covered.
  • URL Tools: Ensure safe web browsing with the URL encoder and decoder.
  • Time Tools: Calculate date ranges and convert between Unix timestamps and human-readable dates seamlessly.

It’s a treasure trove of digital utilities, so do give it a visit!

--

--