Generate a REST Resource

nest generate resource scaffolds a full CRUD module, controller, service, and DTOs at once.

Syntaxnest 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.
  • create and update DTOs.
  • The feature module, imported into the app.

The CLI asks which transport layer you want; choose REST API for a standard HTTP resource.

Example

Example Β· bash
# 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.ts

When 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.

Example Β· bash
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.ts

Generate individual artifacts

Creates module, controller, and service files individually β€” useful when you need fine-grained control over what gets generated.

Example Β· bash
nest g module payments
nest g controller payments --no-spec
nest g service payments --no-spec

Dry-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.

Example Β· bash
nest g resource subscriptions --dry-run
# Prints which files would be created without writing them
# Useful for reviewing before committing to the changes

Discussion

  • Be the first to comment on this lesson.