npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@rafikidota/iroh

v0.8.3

Published

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

Downloads

5

Readme

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