nest.js + TypeORM + PostgreSQL

Agenda :

Building a rest api using Nest.JS .

Backend Architecture :

Technologies used:

Node.js — Platform .

NestJS — server .

TypeORM — orm .

PostgreSQL — database .

What is Node.js …


This content originally appeared on DEV Community and was authored by Rahul kumar

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 

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  

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
  • File structure :
src
|- app.controller.ts
|- app.controller.spec.ts
|- app.module.ts
|- app.service.ts
|- main.ts

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",
});
}
  • 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

Generate module, service, controller for the article route :

nest g mo article module
nest g co article module
nest g s article module

$ 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();
    }
  • navigate to the article controller file .
constructor(private readonly articlesService: ArticlesService) {}
@Get()
    async getAllArticles(): Promise<Article[]> {
        return await this.articlesService.getAllArticles();
    }

Then finally in the application root module .

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

$npm start

Thank You For Reading ?


This content originally appeared on DEV Community and was authored by Rahul kumar


Print Share Comment Cite Upload Translate Updates
APA

Rahul kumar | Sciencx (2021-06-08T04:30:53+00:00) nest.js + TypeORM + PostgreSQL. Retrieved from https://www.scien.cx/2021/06/08/nest-js-typeorm-postgresql/

MLA
" » nest.js + TypeORM + PostgreSQL." Rahul kumar | Sciencx - Tuesday June 8, 2021, https://www.scien.cx/2021/06/08/nest-js-typeorm-postgresql/
HARVARD
Rahul kumar | Sciencx Tuesday June 8, 2021 » nest.js + TypeORM + PostgreSQL., viewed ,<https://www.scien.cx/2021/06/08/nest-js-typeorm-postgresql/>
VANCOUVER
Rahul kumar | Sciencx - » nest.js + TypeORM + PostgreSQL. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/06/08/nest-js-typeorm-postgresql/
CHICAGO
" » nest.js + TypeORM + PostgreSQL." Rahul kumar | Sciencx - Accessed . https://www.scien.cx/2021/06/08/nest-js-typeorm-postgresql/
IEEE
" » nest.js + TypeORM + PostgreSQL." Rahul kumar | Sciencx [Online]. Available: https://www.scien.cx/2021/06/08/nest-js-typeorm-postgresql/. [Accessed: ]
rf:citation
» nest.js + TypeORM + PostgreSQL | Rahul kumar | Sciencx | https://www.scien.cx/2021/06/08/nest-js-typeorm-postgresql/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.