Generate a REST Resource
nest generate resource scaffolds a full CRUD module, controller, service, and DTOs at once.
Syntax
nest generate resource <name>The fastest way to build a REST endpoint is nest generate resource. It creates the module, controller, service, entity, and DTOs, and wires them together.
What you get
- A controller with all five CRUD routes.
- A service with matching methods.
createandupdateDTOs.- The feature module, imported into the app.
The CLI asks which transport layer you want; choose REST API for a standard HTTP resource.
Example
# Generate a complete CRUD resource named 'products'
nest generate resource products
# Produces:
# products/products.module.ts
# products/products.controller.ts
# products/products.service.ts
# products/dto/create-product.dto.ts
# products/dto/update-product.dto.ts
# products/entities/product.entity.tsWhen to use it
- A developer runs `nest g resource invoices` to scaffold a complete CRUD module for invoices including DTOs and entity stubs in seconds.
- A team uses the CLI to ensure every new feature module follows the same folder layout and file naming conventions across the codebase.
- An engineer generates only a service with `nest g service notifications --no-spec` when they do not need a full CRUD module.
More examples
Generate a full REST resource
Generates an entire CRUD resource with module, controller, service, two DTOs, and an entity in a single interactive command.
nest generate resource invoices
# ? What transport layer: REST API
# ? Generate CRUD entry points: Yes
# Creates: src/invoices/{invoices.module,controller,service}.ts
# src/invoices/dto/{create,update}-invoice.dto.ts
# src/invoices/entities/invoice.entity.tsGenerate individual artifacts
Creates module, controller, and service files individually β useful when you need fine-grained control over what gets generated.
nest g module payments
nest g controller payments --no-spec
nest g service payments --no-specDry-run to preview output
Runs the generator in dry-run mode to preview the list of files that would be created without actually writing any of them.
nest g resource subscriptions --dry-run
# Prints which files would be created without writing them
# Useful for reviewing before committing to the changes
Discussion