0.8.3 • Published 10 months ago

@rafikidota/iroh v0.8.3

Weekly downloads
-
License
MIT
Repository
github
Last release
10 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 { CommonModule } from './common/common.module';
import { CoreModule } from './modules/core.module';
@Module({
  imports: [
    //... some other imports
    CommonModule,
    CoreModule,
  ],
  controllers: [],
  providers: [],
})
export class AppModule {}

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 { SecurityModule } from '../../common/security/security.module';
import { HeroController } from './hero.controller';
import { HeroService } from './hero.service';
import { HeroRepository } from './infra/hero.repository';
import { HeroPersistent } from './infra/hero.persistent';

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

Controller

Define your controller by extending GenericController:

import { Controller } from '@nestjs/common';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { GenericController, SecurityGuard } from '@rafikidota/iroh';
import { PermissionPersistent } from '../../common/security/permission';
import { HeroService } from './hero.service';
import { HeroPersistent } from './infra/hero.persistent';
import { CreateHeroDto, UpdateHeroDto } from './app/dto';
import { HeroView } from './app/dto/hero.view';

@ApiBearerAuth()
@ApiTags('Hero')
@Controller('hero')
@SecurityGuard(PermissionPersistent)
export class HeroController extends GenericController(
  HeroPersistent,
  CreateHeroDto,
  UpdateHeroDto,
  HeroView,
) {
  constructor(readonly service: HeroService) {
    super(service);
  }
}

Service

Define your service by extending GenericService:

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

@Injectable()
export class HeroService extends GenericService(HeroPersistent, HeroMapper) {
  constructor(readonly repository: HeroRepository) {
    super(repository);
  }
}

Repository

Define your repository by extending GenericTypeOrmRepository:

import { Injectable } from '@nestjs/common';
import { GenericTypeOrmRepository } from '@rafikidota/iroh';
import { HeroPersistent } from './hero.persistent';

@Injectable()
export class HeroRepository extends GenericTypeOrmRepository(HeroPersistent) {}

Persistent

Define your persistent by extending GenericPersistent:

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

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

Domain

Define your domain by extending GenericDomain:

import { GenericDomain } from '@rafikidota/iroh';
import { HeroPersistent } from '../infra/hero.persistent';
import { IHero } from './hero.interface';

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

  constructor(persistent: HeroPersistent) {
    super();
    this.id = persistent.id;
    this.name = persistent.name;
    this.createdAt = persistent.createdAt;
    this.updatedAt = persistent.updatedAt;
    this.deletedAt = persistent.deletedAt;
  }
}

View

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

import {
  ApiProperty,
  IntersectionType,
  OmitType,
  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(domain);
    this.id = domain.id;
    this.name = domain.name;
    this.createdAt = domain.createdAt;
    this.updatedAt = domain.updatedAt;
  }
}

Mapper

Define your mapper by extending GenericEntityMapper:

import { GenericEntityMapper, IEntityMapper } from '@rafikidota/iroh';
import { HeroPersistent } from './hero.persistent';
import { HeroDomain } from '../domain/hero.domain';
import { HeroView } from '../app/dto/hero.view';

export class HeroMapper
  extends GenericEntityMapper(HeroPersistent, HeroDomain, HeroView)
  implements IEntityMapper<HeroPersistent, HeroDomain, HeroView>
{
  PersistToDomain(persistent: HeroPersistent): HeroDomain {
    return new HeroDomain(persistent);
  }

  DomainToPersist(domain: HeroDomain): HeroPersistent {
    const persistent = {
      id: domain.id,
      name: domain.name,
      createdAt: domain.createdAt,
      updatedAt: domain.updatedAt,
      deletedAt: domain.deletedAt,
    } as unknown as HeroPersistent;
    return persistent;
  }

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

Additional Resources

0.8.1

10 months ago

0.8.0

10 months ago

0.8.3

10 months ago

0.8.2

10 months ago

0.7.9

10 months ago

0.7.6

10 months ago

0.7.7

10 months ago

0.7.2

10 months ago

0.7.1

10 months ago

0.7.4

10 months ago

0.7.3

10 months ago

0.7.0

10 months ago

0.7.5

10 months ago

0.6.21

11 months ago

0.6.20

11 months ago

0.6.23

10 months ago

0.6.22

11 months ago

0.6.25

10 months ago

0.6.24

10 months ago

0.6.26

10 months ago

0.6.10

11 months ago

0.6.12

11 months ago

0.6.11

11 months ago

0.6.18

11 months ago

0.6.17

11 months ago

0.6.19

11 months ago

0.6.14

11 months ago

0.6.13

11 months ago

0.6.16

11 months ago

0.6.15

11 months ago

0.6.9

11 months ago

0.6.8

11 months ago

0.6.7

11 months ago

0.6.6

11 months ago

0.6.5

11 months ago

0.6.4

11 months ago

0.6.3

11 months ago

0.6.2

11 months ago

0.6.1

11 months ago

0.6.0

11 months ago

0.5.74

12 months ago

0.5.73

12 months ago

0.5.72

12 months ago

0.5.71

12 months ago

0.5.70

12 months ago

0.5.69

12 months 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