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 :
NodeJS is a Javascript runtime(contains everything to run javascript) for building server side applications .
What is NestJs :
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 thecreateConnection
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 🔥
Top comments (1)
thx for sharing. Nice drawing of the concept