Skill

Generate NestJS Module Code

Creates NestJS modules: controllers, TypeORM services with error handling, dynamic modules, custom Redis providers, DTO validation, and Jest tests.

Works with nestjstypescriptnode.jstypeormredis

79
Spark score
out of 100
Updated 21 days ago
Version 1.0.0
Models

Add to Favorites

Why it matters

Automate the creation of well-structured and scalable NestJS modules, including controllers, services, repositories, DTOs, and tests, adhering to best practices for maintainability and efficiency.

Outcomes

What it gets done

01

Generate boilerplate code for NestJS modules based on feature-based organization.

02

Implement dependency injection patterns and best practices.

03

Create essential module components like controllers, services, and repositories.

04

Generate unit tests for services to ensure code quality.

Install

Add it to your toolbox

Run in your project directory:

curl -fsSL https://spark.entire.vc/get/vb-nestjs-module-creator | bash

Overview

NestJS Module Creator

Guides creating NestJS feature modules - controller/service/repository structure with dependency injection, dynamic modules, custom providers, validated DTOs, and Jest unit testing with mocked repositories. Reach for this when creating or restructuring a NestJS feature module that needs proper DI, dynamic configuration, or unit test coverage.

What it does

This skill creates well-structured NestJS backend modules using TypeScript and dependency injection. Module organization follows feature-based boundaries with clear separation between controllers, services, and repositories, barrel exports for clean imports, and hierarchical core/shared/feature module structure. A basic module wires a TypeOrmModule.forFeature() entity import together with its controller and providers, explicitly exporting only the service other modules need.

@Module({
  imports: [TypeOrmModule.forFeature([User])],
  controllers: [UserController],
  providers: [UserService, UserRepository],
  exports: [UserService],
})
export class UserModule {}

Controller implementation uses decorators for Swagger documentation (@ApiTags, @ApiOperation, @ApiResponse), guard-based auth (@UseGuards(JwtAuthGuard)), and a ValidationPipe on the request body. The service layer demonstrates constructor-injected TypeORM repositories, structured logging via NestJS's built-in Logger, and proper exception handling - throwing BadRequestException for duplicate emails and NotFoundException for missing records rather than returning null or generic errors.

Advanced patterns cover dynamic modules via a forRoot() static method accepting typed configuration options and internally configuring TypeOrmModule.forRoot() (useful for database or other environment-parameterized modules), and custom providers using a factory function to construct and export a Redis client under an injection token, consumed elsewhere via @Inject(REDIS_CLIENT). DTOs use class-validator decorators (@IsEmail, @MinLength, @IsOptional) combined with class-transformer for input normalization (like lowercasing email) and Swagger @ApiProperty metadata for auto-generated docs. Testing uses NestJS's Test.createTestingModule with a mocked TypeORM repository (via getRepositoryToken) to unit test service methods without a real database. File organization groups DTOs, entities, guards, and interfaces into subdirectories alongside the controller/service/module files and a barrel index.ts.

When to use - and when NOT to

Use this skill when creating or restructuring a NestJS feature module - building a controller/service/repository trio with proper DI, dynamic modules for configurable dependencies, custom providers for third-party clients (like Redis), validated DTOs, and unit tests with mocked repositories.

It is not the right tool for Node.js frameworks other than NestJS (Express, Fastify used directly) since the module/DI/decorator patterns here are NestJS-specific, or for simple scripts with no need for a modular, dependency-injected architecture.

Inputs and outputs

Input: the feature/domain the module represents (e.g. users, orders) and its data model, external dependencies, and validation rules. Output: a NestJS module with a controller (Swagger-documented, guarded), a service with proper exception handling and logging, DTOs with validation decorators, optionally a dynamic module or custom provider for configurable dependencies, and Jest unit tests with mocked repositories.

Integrations

Built on the NestJS framework with @nestjs/typeorm for database access, @nestjs/swagger for API documentation, class-validator/class-transformer for DTO validation, ioredis for custom Redis providers, and Jest (@nestjs/testing) for unit testing.

Who it's for

Backend developers building NestJS applications who need well-structured, testable feature modules - particularly those using TypeORM for persistence, Swagger for API docs, and dependency-injected custom providers for third-party services.

FAQ

Common questions

Discussion

Questions & comments ยท 0

Sign In Sign in to leave a comment.