DEV Community

Ashish Kushwaha
Ashish Kushwaha

Posted on

Production Ready NestJS Microservice Setup with Candy-Nest-CLI 🍬

If you build microservices with NestJS, you already know how powerful and structured the framework is. However, starting a new project often involves a fair amount of initial setup.

Every time a new service is spun up, developers usually have to go through similar steps:

  • Setting up docker-compose.yml for local databases.
  • Configuring TypeORM, Prisma, or Mongoose.
  • Wiring up message brokers like Kafka or RabbitMQ.
  • Dealing with local environment startup issues.
  • Setting up loggers and metrics.

By the time you actually start writing business logic, a good amount of time has already gone into scaffolding.

Save your AI tokens! While you could prompt ChatGPT or Claude to write this boilerplate, generating a fully-wired NestJS microservice with infrastructure configs easily burns 3,000 to 5,000+ tokens per project (not to mention the context window eaten up by debugging configuration errors). Candy-Nest-CLI lets you bypass that entirely so you can save your usage limits for writing actual business logic.

To help automate this process, I put together Candy-Nest-CLIβ€”an open-source interactive scaffolding tool designed to generate production-ready NestJS microservices.


πŸ› οΈ Building a Service: Step-by-Step

Let's walk through exactly how easy it is to spin up a fully-wired backend service with a PostgreSQL database and Kafka messaging queue.

Step 1: Installation & Initialization

You can either install the CLI globally to have it available anywhere, or run it directly on the fly.

Option 1: Global Installation (Recommended)
Open your terminal and install the package globally:

# Using npm
npm install -g candy-nest-cli

# Or using yarn
yarn global add candy-nest-cli
Enter fullscreen mode Exit fullscreen mode

Once installed, you can generate a new project by running:

candy-nest-cli init user-service
Enter fullscreen mode Exit fullscreen mode

Option 2: Using npx (No Installation)
If you prefer not to install packages globally, you can just run:

npx candy-nest-cli init user-service
Enter fullscreen mode Exit fullscreen mode

Step 2: Choose Your Stack

The CLI uses @inquirer/prompts to walk you through a highly detailed setup. To build our target microservice, we will answer the prompts like this:

  1. What is the name of your project? user-service
  2. Which package manager do you want to use? npm
  3. Select the communication protocols to support: REST
  4. Which HTTP adapter do you want to use? Express
  5. Do you want to include a database? Yes
  6. Select databases to include: PostgreSQL (RDBMS)
  7. Which ORM for PostgreSQL? TypeORM
  8. Do you want to configure an messaging queue? Yes
  9. Which messaging queue do you want to use? Kafka
  10. Do you want to configure Dead Letter Queue (DLQ) and Retries? Yes
  11. Do you want to include Redis for caching? Yes
  12. Which logger do you want to configure? Pino
  13. Which tracing & metrics solution do you want? Prometheus
  14. Do you want to generate API documentation (Swagger)? Yes
  15. Do you want to include a Circuit Breaker (Opossum)? Yes

1. Package Manager Selection

Choose your preferred package manager (npm, yarn, or pnpm). We will select npm for this project.

Package Manager Selection

2. Protocol Selection

Select the communication protocols your microservice will support. The CLI supports a multi-protocol combination of REST, GraphQL, gRPC, and WebSockets. We'll go with REST for standard HTTP routing.

Protocol Selection

3. HTTP Adapter Selection

Choose between Express (the industry standard) and Fastify (high performance). We'll select Express for maximum compatibility.

Adapter Selection

4. Database Integration

Select whether you want to include a database module. We will select Yes to set up persistence.

Database Option Selection

5. Database Choice Selection

Choose which database to use (PostgreSQL, MySQL, or MongoDB). We'll select PostgreSQL (RDBMS) for robust relational database management.

Database Choice Selection

6. Database ORM Selection

Select your ORM of choice. The CLI supports TypeORM or Prisma for SQL databases. Let's select TypeORM.

ORM Selection

7. Messaging Queue Integration

Select whether you want to configure an event-driven messaging queue. We'll select Yes.

Queue Option Selection

8. Queue Choice Selection

Choose between Kafka, RabbitMQ, and BullMQ as your message broker. We'll select Kafka to build an event-driven stream processing microservice.

Queue Choice Selection

9. Dead Letter Queue (DLQ) & Retries

Select whether to set up robust message handling with a Dead Letter Queue (DLQ) and custom retry loops to ensure event resiliency out of the box. We'll select Yes.

DLQ Prompt Selection

10. Redis Caching

Choose whether to set up Redis as a fast, in-memory distributed cache. We'll select Yes.

Redis Prompt Selection

11. Logger Selection

Select your logging solution (Winston, Pino, or the default NestJS logger). We'll select Pino for high-speed JSON logging.

Logger Selection

12. Observability & Tracing

Select your observability stack (Prometheus or OpenTelemetry). We will configure Prometheus metrics.

Observability Selection

13. API Documentation

Choose if you want fully-featured auto-generating Swagger documentation for your REST APIs. We'll select Yes.

Swagger Prompt Selection

14. Circuit Breaker

Enable Opossum-based Circuit Breakers to guard your outgoing HTTP requests and RPC calls from downstream failures. We'll select Yes.

Circuit Breaker Prompt Selection

After confirming your final choice, the CLI will output the progress as it generates the files, writes configurations, and installs the dependencies automatically.

Generation Progress

Scaffolding Complete

Sensible Defaults vs. Ultimate Flexibility

candy-nest-cli is designed with sensible defaults, allowing you to breeze through by just pressing Enter. In our guide, we only customized a few options (like choosing Pino for faster logging and enabling Dead Letter Queues for Kafka).

Here is how our choices compare against the CLI's default settings and alternative integrations:

Option Selected in Walkthrough Default Preset Alternative Choices
Package Manager npm npm yarn, pnpm
Protocols REST [REST] GraphQL, gRPC, WebSockets
HTTP Adapter Express Express Fastify
Database PostgreSQL (RDBMS) PostgreSQL MySQL, MongoDB
SQL ORM TypeORM TypeORM Prisma
Messaging Queue Kafka Kafka RabbitMQ, BullMQ
DLQ & Retries Yes (Customized) No No
Redis Caching Yes Yes No
Logger Pino (Customized) Winston Winston, None
Observability Prometheus Prometheus OpenTelemetry, None
Swagger API Docs Yes Yes No
Circuit Breaker Yes Yes No

Step 3: Boot Up the Infrastructure

Once the CLI finishes generating the code, all your infrastructure is ready to go. No writing Docker files from scratch.

cd user-service
cp .env.example .env
docker-compose up -d
npm run start:dev
Enter fullscreen mode Exit fullscreen mode

Because of how the CLI scaffolds the project, Kafka boots in KRaft mode with a special initialization container. You will see Postgres, Redis, and Kafka all start up perfectly without any race conditions.

Docker Compose Up

Step 4: Run the App

With our infrastructure running, we just start the NestJS app:

npm run start:dev
Enter fullscreen mode Exit fullscreen mode

NestJS Startup Logs

Just like that, you have a production-ready microservice running in under 2 minutes.

Note: you might get kafka-topic related error at startup, you can simply restart the application after a few seconds, and topic will be created or you can exec into docker and create the topic manually.

🌐 Interactive Developer Playgrounds

If you enabled Swagger API Docs or GraphQL, the CLI pre-configures and exposes rich, interactive playgrounds out of the box so you can immediately execute and test operations:

Swagger API Playground (REST)

Access the auto-generated Swagger UI at http://localhost:3000/api/docs to see your REST endpoints, database CRUD examples, and health checks beautifully laid out:

Swagger API Documentation

Apollo Sandbox Playground (GraphQL)

If you enabled GraphQL, open http://localhost:3000/graphql to view the Apollo Sandbox where you can run queries, check schemas, and test mutations immediately:

Apollo GraphQL Sandbox


πŸ—οΈ What Exactly Gets Generated?

candy-nest-cli dynamically generates only the modules you asked for.

Here is a look at the generated project structure for the service we just built:

Generated Project Structure

user-service/
β”œβ”€β”€ dist/
β”œβ”€β”€ logs/
β”œβ”€β”€ node_modules/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ database/           # Database configuration and connection module
β”‚   β”œβ”€β”€ examples/           # REST CRUD database examples
β”‚   β”œβ”€β”€ graphql/            # GraphQL resolvers and schema setup
β”‚   β”œβ”€β”€ grpc/               # gRPC controller and service implementations
β”‚   β”œβ”€β”€ health/             # Terminus-based health indicators
β”‚   β”œβ”€β”€ kafka/              # Kafka microservice controller and providers
β”‚   β”œβ”€β”€ logger/             # Pino/Winston logging configurations
β”‚   β”œβ”€β”€ redis/              # Redis caching module and service
β”‚   β”œβ”€β”€ resiliency/         # Circuit Breaker (Opossum) configuration
β”‚   β”œβ”€β”€ websockets/         # WebSockets gateway and adapter
β”‚   β”œβ”€β”€ app.controller.spec.ts
β”‚   β”œβ”€β”€ app.controller.ts
β”‚   β”œβ”€β”€ app.module.ts
β”‚   β”œβ”€β”€ app.service.ts
β”‚   β”œβ”€β”€ app.throttler.guard.ts # Robust cross-protocol rate limiting guard
β”‚   β”œβ”€β”€ main.ts             # Microservice hybrid bootstrapper (HTTP, gRPC, Kafka)
β”‚   └── schema.gql          # Auto-generated GraphQL schema
β”œβ”€β”€ test/                   # E2E test suites
β”œβ”€β”€ .env
β”œβ”€β”€ .env.example
β”œβ”€β”€ .prettierrc
β”œβ”€β”€ docker-compose.yml      # Local dev stack (PostgreSQL, Kafka, Redis)
β”œβ”€β”€ Dockerfile              # Multi-stage production build definition
β”œβ”€β”€ eslint.config.mjs
β”œβ”€β”€ nest-cli.json
β”œβ”€β”€ package-lock.json
β”œβ”€β”€ package.json
β”œβ”€β”€ README.md
β”œβ”€β”€ tsconfig.build.json
└── tsconfig.json
Enter fullscreen mode Exit fullscreen mode

Fully Wired Code Modules

  • Databases: It generates database.module.ts, injects the ConfigService, and creates a sample Entity or Schema.
  • Message Brokers: It scaffolds the microservice transport layer, including client publishers and controller decorators to consume events.
  • Testing Suite: Fully-mocked unit tests (.spec.ts) are generated for every selected module (including database services, Kafka producers/consumers, Pino loggers, and Redis). It even includes a dedicated regression E2E test file (regression.e2e-spec.ts) so you can run npm run test out of the box!
  • API Documentation: Because we selected REST, Swagger is automatically hooked up to main.ts!

πŸš€ Quick Start / Cheatsheet

Start a new interactive project:

npx candy-nest-cli init my-awesome-microservice
Enter fullscreen mode Exit fullscreen mode

Skip the prompts and generate everything (Kitchen Sink mode):

npx candy-nest-cli init my-kitchen-sink-service --all
Enter fullscreen mode Exit fullscreen mode

Add a new feature to an existing CLI-generated project:

npx candy-nest-cli add
Enter fullscreen mode Exit fullscreen mode

Why Candy-Nest-CLI?

The NestJS ecosystem is fantastic, but maintaining consistency across a large microservice architecture can sometimes be challenging.

The goal of this CLI is to provide a standardized, fast way to boot up services that follow common best practices from day one.

If you work with NestJS, feel free to give it a try. I am actively maintaining it and always open to feedback or contributions!

Let me know what you think in the comments below. Happy coding! πŸš€

Top comments (0)