0.35.0 • Published 5 months ago

@rafikidota/iroh v0.35.0

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

Iroh

Sometimes, the best way to solve your own problems is to help someone else.

Using Iroh with Nestjs

The following TypeScript code snippet illustrates an example of using the Iroh library within the Nestjs framework. This code establishes a basic Nestjs CRUD and highlights how to implement Iroh in your Nestjs project.

Prerequisites

Before using the schematics, ensure you have the following:

  • A NestJS project set up
  • TypeORM configured
  • Necessary dependencies installed

Step-by-Step Guide

1. Generate Basic Modules

Run the following command to generate the core, common and security modules:

npx nest g -c @rafikidota/iroh init 

2. Import Basic Modules on AppModule

import { Module } from '@nestjs/common';
import { NestModule } from '@nestjs/common';
import { MiddlewareConsumer } from '@nestjs/common';
import { StartTimeMiddleware } from '@rafikidota/iroh';
import { CommonModule } from './common/common.module';
import { CoreModule } from './modules/core.module';

@Module({
  imports: [CommonModule, CoreModule],
  controllers: [],
  providers: [],
})
export class AppModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    consumer.apply(StartTimeMiddleware).forRoutes('/*path');
  }
}

3. Generate CRUD Module

Run the following command to generate a new CRUD module:

npx nest g -c @rafikidota/iroh crud <module-name>

4. Output files

Module

Import necessary modules and set up your feature module:

import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { HeroController } from './hero.controller';
import { HeroService } from './hero.service';
import { HeroRepository } from './infra/hero.repository';
import { HeroPersistence } from './infra/hero.persistence';

@Module({
  controllers: [HeroController],
  providers: [HeroService, HeroRepository],
  imports: [TypeOrmModule.forFeature([HeroPersistence])],
  exports: [TypeOrmModule],
})
export class HeroModule {}

Controller

Define your controller by extending GenericController:

import { Controller } from '@nestjs/common';
import { Query } from '@nestjs/common';
import { Body } from '@nestjs/common';
import { Param } from '@nestjs/common';
import { ParseUUIDPipe } from '@nestjs/common';
import { ApiBearerAuth } from '@nestjs/swagger';
import { ApiTags } from '@nestjs/swagger';

import { IrohController } from '@rafikidota/iroh';
import { SecurityGuard } from '@rafikidota/iroh';
import { GenericPost } from '@rafikidota/iroh';
import { GenericGet } from '@rafikidota/iroh';
import { GenericGetById } from '@rafikidota/iroh';
import { GenericPatch } from '@rafikidota/iroh';
import { GenericDelete } from '@rafikidota/iroh';
import { ParseQueryPipe } from '@rafikidota/iroh';
import { SearchDto } from '@rafikidota/iroh';
import { QueryOptionDto } from '@rafikidota/iroh';

import { PermissionPersistence } from '../../common/security/permission';
import { HeroService } from './hero.service';
import { HeroMapper } from './infra/hero.mapper';
import { CreateHeroDto } from './app/dto/hero.create.dto';
import { UpdateHeroDto } from './app/dto/hero.update.dto';
import { HeroView } from './app/dto/hero.view';

@ApiBearerAuth()
@ApiTags('Hero')
@Controller('hero')
@SecurityGuard(PermissionPersistence)
export class HeroController extends IrohController<HeroMapper> {
  constructor(readonly service: HeroService) {
    super(service);
  }

  @GenericPost(CreateHeroDto, HeroView)
  create(body: CreateHeroDto): Promise<HeroView> {
    return super.create(body);
  }

  @GenericGet(HeroView)
  paginate(@Query(ParseQueryPipe) query: SearchDto): Promise<HeroView[]> {
    return super.paginate(query);
  }

  @GenericGetById(HeroView)
  findById(
    @Param('id', ParseUUIDPipe) id: string,
    @Query(ParseQueryPipe) query: QueryOptionDto,
  ): Promise<HeroView> {
    return super.findById(id, query);
  }

  @GenericPatch(UpdateHeroDto, HeroView)
  update(
    @Param('id', ParseUUIDPipe) id: string,
    @Body() body: UpdateHeroDto,
  ): Promise<HeroView> {
    return super.update(id, body);
  }

  @GenericDelete()
  remove(@Param('id', ParseUUIDPipe) id: string): Promise<HeroView> {
    return super.remove(id);
  }
}

Service

Define your service by extending GenericService:

import { Injectable } from '@nestjs/common';
import { IrohService } from '@rafikidota/iroh';
import { HeroRepository } from './infra/hero.repository';
import { HeroMapper } from './infra/hero.mapper';

@Injectable()
export class HeroService extends IrohService<HeroMapper> {
  constructor(readonly repository: HeroRepository) {
    super(repository);
  }
}

Repository

Define your repository by extending GenericTypeOrmRepository:

import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { IrohRepository } from '@rafikidota/iroh';
import { HeroMapper } from './hero.mapper';
import { HeroPersistence } from './hero.persistence';

@Injectable()
export class HeroRepository extends IrohRepository<HeroMapper> {
  constructor(
    @InjectRepository(HeroPersistence)
    repository: Repository<HeroPersistence>,
  ) {
    super(repository);
    this.mapper = new HeroMapper();
  }
}

Persistence

Define your persistence by extending GenericPersistence:

import { Column, Entity } from 'typeorm';
import { GenericPersistence } from '@rafikidota/iroh';
import { IHero } from '../domain';

@Entity('hero')
export class HeroPersistence extends GenericPersistence implements IHero {
  @Column()
  name: string;
}

Domain

Define your domain by extending GenericDomain:

import { DeepPartial } from 'typeorm';
import { Result } from '@rafikidota/iroh';
import { DomainFactory } from '@rafikidota/iroh';
import { GenericDomain } from '@rafikidota/iroh';
import { IUpdatableDomain } from '@rafikidota/iroh';
import { Props } from '@rafikidota/iroh';
import { IHero } from './hero.interface';

export type HeroProps = Props<HeroDomain>;

export type NewHeroProps = Omit<HeroProps, ''>;

export type UpdateHeroProps = DeepPartial<NewHeroProps>;

export type IHeroDomain = IHero & IUpdatableDomain<HeroDomain>;

export class HeroDomain extends GenericDomain implements IHeroDomain {
  name: string;

  static create(props: NewHeroProps): Result<HeroDomain> {
    const domain = DomainFactory.create(HeroDomain, { ...props });
    return Result.Ok(domain);
  }

  public update(props: UpdateHeroProps): Result<HeroDomain> {
    Object.assign(this, { updatedAt: new Date(), ...props });
    return Result.Ok(this);
  }
}

View

Define your view by extending OmitType from the IntersectionType of PartialType(CreateHeroDto) and GenericView:

import { ApiProperty } from '@nestjs/swagger';
import { IntersectionType } from '@nestjs/swagger';
import { OmitType } from '@nestjs/swagger';
import { PartialType } from '@nestjs/swagger';
import { GenericView } from '@rafikidota/iroh';
import { CreateHeroDto } from './hero.create.dto';
import { HeroDomain, IHero } from '../../domain';

export class HeroView
  extends OmitType(
    IntersectionType(PartialType(CreateHeroDto), GenericView),
    [],
  )
  implements IHero
{
  @ApiProperty()
  id: string;
  @ApiProperty({
    example: 'Pudge',
  })
  name: string;
  @ApiProperty()
  createdAt: Date;
  @ApiProperty()
  updatedAt: Date;

  constructor(domain: HeroDomain) {
    super();
    this.id = domain.id;
    this.name = domain.name;
    this.createdAt = domain.createdAt;
    this.updatedAt = domain.updatedAt;
  }
}

Mapper

Define your mapper by extending GenericEntityMapper:

import { DomainFactory } from '@rafikidota/iroh';
import { PersistenceFactory } from '@rafikidota/iroh';
import { DomainProps } from '@rafikidota/iroh';
import { IrohMapper } from '@rafikidota/iroh';
import { HeroPersistence } from './hero.persistence';
import { HeroDomain } from '../domain/hero.domain';
import { HeroView } from '../app/dto/hero.view';

const Mapper = IrohMapper<HeroPersistence, HeroDomain, HeroView>;

export class HeroMapper extends Mapper {
  PersistenceToDomain(persistence: HeroPersistence): HeroDomain {
    const props: Partial<DomainProps<HeroDomain>> = {
      id: persistence.id,
      name: persistence.name,
      createdAt: persistence.createdAt,
      updatedAt: persistence.updatedAt,
      deletedAt: persistence.deletedAt,
    };
    return DomainFactory.instance(HeroDomain, { ...props });
  }

  DomainToPersistence(domain: HeroDomain): HeroPersistence {
    const props: Partial<DomainProps<HeroDomain>> = {
      id: domain.id,
      name: domain.name,
      createdAt: domain.createdAt,
      updatedAt: domain.updatedAt,
      deletedAt: domain.deletedAt,
    };
    return PersistenceFactory.create(HeroPersistence, { ...props });
  }

  DomainToView(domain: HeroDomain): HeroView {
    return new HeroView(domain);
  }
}

Additional Resources

0.8.9

10 months ago

0.8.8

10 months ago

0.8.5

11 months ago

0.8.4

11 months ago

0.8.7

11 months ago

0.8.6

11 months ago

0.17.0

8 months ago

0.32.0

5 months ago

0.29.0

6 months ago

0.21.4

7 months ago

0.21.3

7 months ago

0.21.2

8 months ago

0.21.1

8 months ago

0.9.4

10 months ago

0.9.3

10 months ago

0.9.6

10 months ago

0.9.5

10 months ago

0.21.0

8 months ago

0.18.1

8 months ago

0.18.2

8 months ago

0.18.3

8 months ago

0.18.4

8 months ago

0.18.5

8 months ago

0.33.1

5 months ago

0.33.0

5 months ago

0.10.1

9 months ago

0.18.0

8 months ago

0.10.0

9 months ago

0.22.0

7 months ago

0.15.1

9 months ago

0.15.2

9 months ago

0.30.1

6 months ago

0.30.0

6 months ago

0.27.0

6 months ago

0.16.0

8 months ago

0.16.1

8 months ago

0.31.0

6 months ago

0.28.0

6 months ago

0.13.1

9 months ago

0.13.2

9 months ago

0.25.0

6 months ago

0.14.0

9 months ago

0.26.1

6 months ago

0.26.0

6 months ago

0.19.0

8 months ago

0.34.0

5 months ago

0.11.0

9 months ago

0.23.0

7 months ago

0.9.0

10 months ago

0.9.2

10 months ago

0.9.1

10 months ago

0.35.0

5 months ago

0.12.0

9 months ago

0.12.2

9 months ago

0.12.3

9 months ago

0.24.1

7 months ago

0.24.0

7 months ago

0.8.1

1 year ago

0.8.0

1 year ago

0.8.3

1 year ago

0.8.2

1 year ago

0.7.9

1 year ago

0.7.6

1 year ago

0.7.7

1 year ago

0.7.2

1 year ago

0.7.1

1 year ago

0.7.4

1 year ago

0.7.3

1 year ago

0.7.0

1 year ago

0.7.5

1 year ago

0.6.21

1 year ago

0.6.20

1 year ago

0.6.23

1 year ago

0.6.22

1 year ago

0.6.25

1 year ago

0.6.24

1 year ago

0.6.26

1 year ago

0.6.10

1 year ago

0.6.12

1 year ago

0.6.11

1 year ago

0.6.18

1 year ago

0.6.17

1 year ago

0.6.19

1 year ago

0.6.14

1 year ago

0.6.13

1 year ago

0.6.16

1 year ago

0.6.15

1 year ago

0.6.9

1 year ago

0.6.8

1 year ago

0.6.7

1 year ago

0.6.6

1 year ago

0.6.5

1 year ago

0.6.4

1 year ago

0.6.3

1 year ago

0.6.2

1 year ago

0.6.1

1 year ago

0.6.0

1 year ago

0.5.74

1 year ago

0.5.73

1 year ago

0.5.72

1 year ago

0.5.71

1 year ago

0.5.70

1 year ago

0.5.69

1 year ago

0.5.68

1 year ago

0.5.67

1 year ago

0.5.66

1 year ago

0.5.65

1 year ago

0.5.64

1 year ago

0.5.63

1 year ago

0.5.62

1 year ago

0.5.61

1 year ago

0.5.60

1 year ago

0.5.59

1 year ago

0.5.58

1 year ago

0.3.57

1 year ago

0.3.56

1 year ago

0.3.55

1 year ago

0.3.54

1 year ago

0.3.53

1 year ago

0.3.52

1 year ago

0.3.51

1 year ago

0.3.50

1 year ago

0.3.49

1 year ago

0.3.47

1 year ago

0.3.46

1 year ago

0.3.45

1 year ago

0.3.44

1 year ago

0.3.43

1 year ago

0.3.42

1 year ago

0.3.41

1 year ago

0.3.40

1 year ago

0.3.39

1 year ago

0.3.38

1 year ago

0.3.37

1 year ago

0.3.36

1 year ago

0.3.35

1 year ago

0.3.34

1 year ago

0.3.33

1 year ago

0.3.31

1 year ago

0.3.30

1 year ago

0.3.29

1 year ago

0.3.28

1 year ago

0.3.26

1 year ago

0.3.25

1 year ago

0.3.24

1 year ago

0.3.23

1 year ago

0.3.22

1 year ago

0.3.21

1 year ago

0.3.20

1 year ago

0.3.19

1 year ago

0.3.18

1 year ago

0.3.17

1 year ago

0.3.15

1 year ago

0.3.14

1 year ago

0.3.13

1 year ago

0.3.12

1 year ago

0.3.11

1 year ago

0.3.10

1 year ago

0.3.9

1 year ago

0.3.8

1 year ago

0.3.7

1 year ago

0.3.6

1 year ago

0.3.5

1 year ago

0.3.4

1 year ago

0.3.3

1 year ago

0.3.2

1 year ago

0.3.1

1 year ago

0.3.0

1 year ago

0.2.38

1 year ago

0.2.37

1 year ago

0.2.36

1 year ago

0.2.35

1 year ago

0.2.34

1 year ago

0.2.33

1 year ago

0.2.31

1 year ago

0.2.30

1 year ago

0.2.29

1 year ago

0.2.28

1 year ago

0.2.27

1 year ago

0.2.26

1 year ago

0.2.25

1 year ago

0.2.24

1 year ago

0.2.23

1 year ago

0.2.22

1 year ago

0.2.21

1 year ago

0.2.20

1 year ago

0.2.19

1 year ago

0.2.18

1 year ago

0.2.17

1 year ago

0.2.16

1 year ago

0.2.15

1 year ago

0.2.12

1 year ago

0.2.10

1 year ago

0.2.9

1 year ago

0.2.8

1 year ago

0.2.7

1 year ago

0.2.6

1 year ago

0.2.5

1 year ago

0.2.4

1 year ago

0.2.3

1 year ago

0.2.0

1 year ago

0.1.26

1 year ago

0.1.25

1 year ago

0.1.24

1 year ago

0.1.23

1 year ago

0.1.22

1 year ago

0.1.21

1 year ago

0.1.20

1 year ago

0.1.19

1 year ago

0.1.18

1 year ago

0.1.17

1 year ago

0.1.16

1 year ago

0.1.15

1 year ago

0.1.14

1 year ago

0.1.13

1 year ago

0.1.12

1 year ago

0.1.11

1 year ago

0.1.10

1 year ago

0.1.9

1 year ago

0.1.8

1 year ago

0.1.7

1 year ago

0.1.6

1 year ago

0.1.5

1 year ago

0.1.4

1 year ago

0.1.3

1 year ago

0.1.2

1 year ago

0.1.1

1 year ago

0.1.0

1 year ago