nestrs generate (aliased as nestrs g) creates individual source files or full multi-file resource scaffolds from built-in templates. Every generated file uses real nestrs macros and compiles out of the box. For resource generators, the CLI also automatically wires the new module into your parent main.rs, lib.rs, or mod.rs.
Syntax
<name> is converted to snake_case for file names and field references and to PascalCase for struct names automatically.
Generator kinds and aliases
Flags
File naming: --style nest vs --style rust
- --style nest (default)
- --style rust
Files are named The
<stem>.<kind>.rs, matching the Nest CLI convention:mod.rs uses #[path = "..."] attributes to load the dotted filenames.Automatic parent module wiring
After generating a resource, the CLI searches the--path directory for main.rs, lib.rs, or mod.rs in that order. If it finds one, it appends:
Automatic wiring only touches
main.rs, lib.rs, or mod.rs. It will not modify arbitrary Rust files.Resource generator: transport options
Theresource generator is the most complete generator — it creates a module, service, two DTOs, a mod.rs, and a transport-specific entry file in one command.
Generated files per transport
Generator templates
The code below shows the exact template output for a generator namedusers (snake: users, pascal: Users). Real names are substituted at generation time.
service
service
controller
controller
module
module
dto
dto
guard
guard
pipe
pipe
filter
filter
interceptor
interceptor
strategy
strategy
resolver (standalone)
resolver (standalone)
The standalone
resolver generator produces a bare stub. For a full GraphQL CRUD resolver, use nestrs g resource users --transport graphql.gateway (standalone)
gateway (standalone)
The standalone
gateway generator produces a bare stub. For a full WebSocket gateway, use nestrs g resource users --transport ws.microservice (standalone)
microservice (standalone)
transport (standalone)
transport (standalone)
The standalone
transport generator produces a bare stub. For full message patterns, use nestrs g resource users --transport microservice or --transport grpc.resource --transport rest (CRUD service)
resource --transport rest (CRUD service)
Shared across all transport kinds. Uses an in-memory
HashMap store — swap it for a real database in production.resource --transport rest (controller)
resource --transport rest (controller)
The REST controller registers five routes and includes optional OpenAPI attributes. Add
features = ["openapi"] to nestrs in Cargo.toml and chain .enable_openapi() on NestFactory to activate the /openapi.json endpoint.resource --transport graphql (resolver)
resource --transport graphql (resolver)
Requires
nestrs = { version = "...", features = ["graphql"] } and async-graphql = "=7.0.17" in Cargo.toml. Wire the schema in main.rs via app.enable_graphql(schema).resource --transport ws (gateway)
resource --transport ws (gateway)
Requires
nestrs = { version = "...", features = ["ws"] }. Client frames use { "event": "...", "data": { ... } } JSON envelopes.resource --transport grpc (transport)
resource --transport grpc (transport)
Requires
nestrs features microservices and microservices-grpc. Bootstrap with NestFactory::create_microservice_grpc::<RootModule>(...).resource --transport microservice (transport)
resource --transport microservice (transport)
Requires the
microservices feature. Bootstrap with NestFactory::create_microservice::<RootModule>(...). The generated code is identical to the gRPC transport except for the bootstrap method and the excluded microservices-grpc feature requirement.resource DTOs (create and update)
resource DTOs (create and update)
Both DTOs are generated for every resource regardless of transport.
resource module (per transport)
resource module (per transport)
The
#[module] declaration varies by transport to wire the correct providers and controllers.