NestJS

The what and how

Who am i?


              @Entity()
              Class Presentor {
                  @Column()
                  name: "Wout";

                  @Column()
                  age: 23;

                  @Column()
                  profession: "Fullstack developer @ Riaktr";

                  @Column("string", { array: true })
                  hobbies: ["Coding", "Gaming", "Trains"];
              }
            

WHAT is NestJS?

  • Framework
  • Opinionated
  • Typescript
  • Scalable
  • Choice

WHY NestJS?

  • Structured
  • Robust
  • Secure
  • Generator

Structure


              .
              └── src/
                  ├── app.module.ts
                  ├── app.controller.ts
                  ├── app.service.ts
                  └── main.ts
            

Structure


              .
              └── src/
                  ├── community/
                  │   ├── community.module.ts
                  │   ├── community.controller.ts 
                  │   └── community.service.ts
                  ├── app.module.ts
                  ├── app.controller.ts
                  ├── app.service.ts
                  └── main.ts
            

Controller


            @Controller('community')
            export class CommunityController {
              constructor(
                private readonly communityService: CommunityService
              ) {}
            
              @Get('/best')
              getBestCommunity(): string {
                return this.communityService.getBestCommunity();
              }

              @Post(':id')
              create(@Body() createCommunity: { name: string }) {
                return this.communityService.create(createCommunity);
              }

            }
            

Service


            @Injectable()
            export class CommunityService {
              getBestCommunity(): string {
                return 'BeJS';
              }

              create(createCommunityDto: CreateCommunityDto) {
                return 'This action adds a new community';
              }
            }
            

Module


            @Module({
              imports: [],
              controllers: [AppController],
              providers: [AppService],
            })
            export class AppModule {}
            

Module


            @Module({
              imports: [CommunityModule],
              controllers: [AppController],
              providers: [AppService],
            })
            export class AppModule {}
            

Resource


              .
              └── src/
                  ├── community/
                  │   └── ...
                  ├── ...
                  └── main.ts
              

Recourse


              .
              └── src/
                  ├── community/
                  ├── meetup/
                  │   ├── dto/
                  │   │   ├── create-meetup.dto.ts
                  │   │   └── update-meetup.dto.ts
                  │   ├── entities/
                  │   │   └── meetup.entity.ts
                  │   ├── meetup.module.ts
                  │   ├── meetup.controller.ts 
                  │   └── meetup.service.ts
                  ├── ...
                  └── main.ts
              

Entity


            @Entity()
            export class Meetup {
              @PrimaryGeneratedColumn()
              id: number;
            
              @Column()
              name: string;
            
              @Column()
              description: string;
            
              @Column()
              date: Date;
            }
          

Data Transfer Object (DTO)


            export class CreateMeetupDto {
              @IsString()
              @IsNotEmpty()
              name: string;
            
              @IsString()
              @IsNotEmpty()
              description: string;
            
              @IsDate()
              @IsNotEmpty()
              date: Date;
            }
          

Demo Time!

Demo code

Questions?

Thank you