DEV Community

Cover image for nest.js + TypeORM + PostgreSQL
Rahul Kumar
Rahul Kumar

Posted on

nest.js + TypeORM + PostgreSQL

Agenda :

Building a rest api using Nest.JS .

Backend Architecture :

Alt Text

Technologies used:

  1. Node.js --- Platform .
  2. NestJS --- server .
  3. TypeORM --- orm .
  4. PostgreSQL --- database .

What is Node.js :

Node js
NodeJS is a Javascript runtime(contains everything to run javascript) for building server side applications .

What is NestJs :

Nest
Nest is a framework for building efficient and scalable NodeJs server side applications .

What is TypeORM :

TypeORM is an object relational mapper which task is to basically generates objects using object oriented programming which maps to the database virtually .

What is PostgreSQL :

PostgreSQL is an object relational database management system for building scalable and high availability applications .

  • Install @nestjs/cli package .and create a new project
$ npm i -g @nestjs/cli $ nest new project-name 
Enter fullscreen mode Exit fullscreen mode

In a common nestJS project :

  • main.ts contains the bootstraping code .
  • .spec file contains the testing files .
  • nestjs usages module to organize the application structure .
  • controller contains the routing rules for the application .
  • service contains the business logic for the application .

$ npm run start

curl localhost:3000 
Enter fullscreen mode Exit fullscreen mode

Set up Database :

$sudo -U postgres $psql $create database conduit $create user conduit with encrypted password conduit $grant all privileges on database conduit to conduit 
Enter fullscreen mode Exit fullscreen mode
  • File structure :
src |- app.controller.ts |- app.controller.spec.ts |- app.module.ts |- app.service.ts |- main.ts 
Enter fullscreen mode Exit fullscreen mode

Create a connection to the database .

$ touch app.dbconfig.ts

import {TypeOrmModuleOptions} from "typeorm"; export function createTypeOrmProdConfig(): TypeOrmModuleOptions{ return({ type: "postgres", username: "conduit", password: "conduit", database: "conduit" entities: [join(__dirname, '**', '*.entity.{ts, js}')], synchronize: true, logging: true, logger: "advanced-console", }); } 
Enter fullscreen mode Exit fullscreen mode
  • Next we have to create the entities for our Database . And the individual entity to the entities array in the createConnection function .

$ mkdir src/entities

$cd entities

$nano Article.ts

@Entity() export class Article { @PrimaryColumn({length: 40}) slug: string @Column({length: 40}) title?: string @Column({length: 100, nullable:true}) description: string @Column({type: 'text'}) body: string 
Enter fullscreen mode Exit fullscreen mode

Generate module, service, controller for the article route :

nest g mo article module nest g co article module nest g s article module 
Enter fullscreen mode Exit fullscreen mode

$ cd module/article

$ nano module.article.ts

imports : [Typeormmodue.forFeature([Article])]

  • navigate to the article service file .
@InjectRepository(Article) private readonly articleRepo: Repository<Article>, async getAllArticles(): Promise<Article[]> { return await this.articleRepo.find(); } 
Enter fullscreen mode Exit fullscreen mode
  • navigate to the article controller file .
constructor(private readonly articlesService: ArticlesService) {} @Get() async getAllArticles(): Promise<Article[]> { return await this.articlesService.getAllArticles(); } 
Enter fullscreen mode Exit fullscreen mode

Then finally in the application root module .

imports :[TypeOrmModule.forroot([Article])]

$npm start

Thank You For Reading 🔥

Top comments (1)

Collapse
 
vikbert profile image
Xun Zhou

thx for sharing. Nice drawing of the concept