@crdsyntax/nest-module
v1.4.2
Published
generate nest module with basic interface using nestjs, typeorm, swagger
Readme
Nest Module Generator
A small utility script that scaffolds a NestJS module with common building blocks (module, service, controller), plus folders and files commonly used in a CRUD module: DTOs, entity, repository, tests, and extra folders.
This repository contains the script at bin/generate-module.js which automates invoking the Nest CLI and then creates & rearranges files into a standard module layout.
Quick summary
- Script:
bin/generate-module.js - Purpose: Generate a NestJS module scaffold (module/service/controller) and add commonly used files and folders (entities, dto, repositories, services, controllers, tests).
- Language: Node.js (plain JavaScript)
Requirements
- Node.js (tested with Node 12+; use a modern LTS version)
- Internet access for
npx @nestjs/cli(the script runs Nest CLI vianpx) - A NestJS project structure with a
src/folder (the script generatessrc/<module-name>) - Optional:
typeormand@nestjs/swaggerif you want the generated files to compile without additional changes
Installation / make executable
Clone the repository and (optionally) make the script executable:
git clone <repo-url>
cd nest-module
chmod +x ./bin/generate-module.jsYou can run it with node or directly if executable.
Usage
Run the script from the repository root (or adjust path accordingly):
# using node
node ./bin/generate-module.js MyModuleName
# or, if executable
./bin/generate-module.js MyModuleNameExamples (input name formats accepted):
UserProfile(PascalCase)userProfile(camelCase)user-profile(kebab-case)
The script will convert the provided name into kebab-case for folder names and into PascalCase for class names. For example, userProfile -> module folder src/user-profile and class UserProfile.
What the script does
Calls the Nest CLI to generate the module, service and controller:
npx @nestjs/cli g module <kebab-name>npx @nestjs/cli g service <kebab-name>npx @nestjs/cli g controller <kebab-name>
Creates a set of folders under
src/<kebab-name>:services/controllers/dto/entities/tests/repositories/
Creates or writes several files (if they don't already exist):
entities/<kebab-name>.entity.ts— basic TypeORM entity scaffolddto/create-<kebab-name>.dto.ts— Swagger-decorated create DTOdto/update-<kebab-name>.dto.ts— PartialType-based update DTOrepositories/<kebab-name>.repository.ts— basic repository using@InjectRepositoryandRepository<T>
Moves the generated service and controller files into the
services/andcontrollers/folders respectively, rewrites them with templates that delegate to the repository and include Swagger decorators for the controller.
Example generated structure
After running node ./bin/generate-module.js userProfile you should see (roughly):
src/
└── user-profile/
├── controllers/
│ └── user-profile.controller.ts
├── dto/
│ ├── create-user-profile.dto.ts
│ └── update-user-profile.dto.ts
├── entities/
│ └── user-profile.entity.ts
├── repositories/
│ └── user-profile.repository.ts
├── services/
│ └── user-profile.service.ts
├── tests/
│ ├── user-profile.controller.spec.ts
│ └── user-profile.service.spec.ts
└── user-profile.module.tsShort description of generated files
- entity: a TypeORM
@Entity()class withid,nameand date columns. - DTOs:
Create...Dtowith@ApiProperty()andUpdate...Dtobased onPartialType. - repository: an injectable class that wraps
Repository<T>and provides basic CRUD helper methods. - service: thin service that delegates to the repository and wraps calls with try/catch and HttpExceptions in the template.
- controller: CRUD endpoints with Swagger decorators and request validation pipes (e.g.,
ParseIntPipe).
Notes & caveats
- The script depends on
npx @nestjs/cliand will try to run it automatically; ensure you have network access or@nestjs/cliinstalled globally. - The generated TypeScript templates may contain small issues that should be reviewed before compiling (for example, some date-decorator names or variable references in templates may require correction). Please inspect files like the entity and service templates and adjust imports / names if TypeScript compilation fails.
- The script only writes some files if they don't already exist (it checks with
fs.existsSyncbefore writing certain files). It will, however, use the Nest CLI which may overwrite files generated by the CLI itself before the script moves them.
Suggested improvements (low-risk)
- Fix small typos in the generated entity/service templates (e.g.,
UpdateDatecolumn->UpdateDateColumn). - Replace any undefined template variables (like
pascalNameif present) with the computed class name. - Add an optional
--forceflag to overwrite existing files explicitly. - Add unit tests that validate the generator's output structure and minimal TypeScript compilation.
Troubleshooting
- If you see TypeScript errors after generation, open the generated files under
src/<kebab-name>and inspect import names and decorator casing. - If
npxfails, try installing the Nest CLI globally:npm i -g @nestjs/cliand re-run the script.
License
This project inherits the repository license (if any). Use the script freely within the constraints of that license.
If you'd like, I can also fix the small typos and template inconsistencies in bin/generate-module.js so the generated files compile cleanly. Let me know if you want me to apply those improvements.
