8000 GitHub - habiburrahman30/nestjs-doc Β· GitHub
[go: up one dir, main page]

Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

Β 

History

5 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 

Repository files navigation

πŸ“ Professional NestJS Folder Structure (for Senior Developers)

This document outlines a robust, scalable folder structure for enterprise-level NestJS applications. It supports best practices, domain-driven design (DDD), third-party integrations, environment management, and advanced tooling.


Install postgresql

# Install PostgreSQL (Server + Client Tools)
$ sudo apt update
$ sudo apt install postgresql postgresql-contrib

# Start PostgreSQL Service
$ sudo systemctl enable postgresql
$ sudo systemctl start postgresql

# Set the postgres Password

# Switch to postgres system user (Linux/macOS)
$ sudo -u postgres psql

# Inside psql:
$ ALTER USER postgres PASSWORD 'your_new_password';
npm i cookie-parser express-session bcrypt --loglevel=silly
npm i --save @nestjs/swagger @nestjs/axios axios @nestjs-modules/mailer nodemailer @nestjs/jwt @nestjs/passport passport passport-local @nestjs/websockets @nestjs/platform-socket.io class-validator class-transformer @nestjs/typeorm typeorm pg @nestjs/config
npm i --save-dev @types/passport-local @types/nodemailer
npm i -D @types/cookie-parser @types/multer @types/express-session @types/bcrypt

πŸ“¦ Project Structure

src/
β”œβ”€β”€ main.ts
β”œβ”€β”€ app.module.ts
β”œβ”€β”€ app.service.ts

β”œβ”€β”€ modules/                        # Domain-driven feature modules
β”‚   β”œβ”€β”€ auth/
β”‚   β”‚   β”œβ”€β”€ auth.module.ts
β”‚   β”‚   β”œβ”€β”€ auth.controller.ts
β”‚   β”‚   β”œβ”€β”€ auth.service.ts
β”‚   β”‚   β”œβ”€β”€ auth.strategy.ts
β”‚   β”‚   β”œβ”€β”€ auth.guard.ts
β”‚   β”‚   β”œβ”€β”€ dto/
β”‚   β”‚   β”œβ”€β”€ interfaces/
β”‚   β”‚   └── enums/
β”‚   β”‚
β”‚   β”œβ”€β”€ users/
β”‚   β”‚   β”œβ”€β”€ users.module.ts
β”‚   β”‚   β”œβ”€β”€ users.service.ts
β”‚   β”‚   β”œβ”€β”€ users.controller.ts
β”‚   β”‚   β”œβ”€β”€ entities/
β”‚   β”‚   β”œβ”€β”€ dto/
β”‚   β”‚   β”œβ”€β”€ interfaces/
β”‚   β”‚   └── enums/
β”‚   β”‚
β”‚   └── ... (other features like orders, products, etc.)

β”œβ”€β”€ common/                         # Shared logic
β”‚   β”œβ”€β”€ decorators/
β”‚   β”œβ”€β”€ filters/
β”‚   β”‚   └── http-exception.filter.ts
β”‚   β”œβ”€β”€ interceptors/
β”‚   β”œβ”€β”€ guards/
β”‚   β”œβ”€β”€ middlewares/
β”‚   β”œβ”€β”€ pipes/
β”‚   └── utils/

β”œβ”€β”€ infrastructu
7ED1
re/                # External system integrations
β”‚   β”œβ”€β”€ logger/
β”‚   β”‚   └── logger.service.ts
β”‚   β”œβ”€β”€ mailer/
β”‚   β”œβ”€β”€ storage/
β”‚   β”œβ”€β”€ database/
β”‚   β”‚   β”œβ”€β”€ database.module.ts
β”‚   β”‚   β”œβ”€β”€ typeorm.config.ts
β”‚   β”‚   β”œβ”€β”€ seeds/
β”‚   β”‚   └── migrations/
β”‚   └── ...

β”œβ”€β”€ config/                         # Environment configuration
β”‚   β”œβ”€β”€ config.module.ts
β”‚   β”œβ”€β”€ config.service.ts
β”‚   └── validation.ts               # Joi schema for env validation

β”œβ”€β”€ shared/                         # Shared constants, types, enums
β”‚   β”œβ”€β”€ constants/
β”‚   β”œβ”€β”€ interfaces/
β”‚   └── enums/

β”œβ”€β”€ third-party/                    # External services and APIs
β”‚   β”œβ”€β”€ cloudinary/
β”‚   β”‚   β”œβ”€β”€ cloudinary.module.ts
β”‚   β”‚   β”œβ”€β”€ cloudinary.service.ts
β”‚   β”‚   └── cloudinary.constants.ts
β”‚   └── ...

β”œβ”€β”€ typings/                        # Type overrides (e.g. Express user)
β”‚   └── express.d.ts

🧩 Other Files & Tools

.env                            # Main environment file
.env.sample                     # Sample for team/CI use
.prettierrc                     # Code formatting rules
.eslintrc.js                    # Linting rules
Dockerfile                      # Docker container setup
.dockerignore
nestjs-cli.json
package.json
README.md

βœ… Best Practices

  • Group by domain (not by type)
  • Use modules/ for all business features
  • Use common/ for cross-cutting concerns (filters, guards, pipes, etc.)
  • Use infrastructure/ for low-level implementations (DB, logging, storage)
  • Use third-party/ for external API wrappers (Stripe, Cloudinary, etc.)
  • Use config/ to centralize environment config with validation
  • Use typings/ for TypeScript augmentations

Let me know if you need a code scaffold or starter repo based on this!

πŸ“¦ Project Structure 2

nestjs-pro/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ app.module.ts                # Root module
β”‚   β”œβ”€β”€ main.ts                      # Entry point
β”‚   β”œβ”€β”€ config/                      # Centralized configuration files
β”‚   β”‚   β”œβ”€β”€ app.config.ts
β”‚   β”‚   β”œβ”€β”€ database.config.ts
β”‚   β”‚   └── ...
β”‚   β”œβ”€β”€ common/                      # Common/shared utils and constants
β”‚   β”‚   β”œβ”€β”€ constants/
β”‚   β”‚   β”œβ”€β”€ decorators/
β”‚   β”‚   β”œβ”€β”€ filters/
β”‚   β”‚   β”œβ”€β”€ guards/
β”‚   β”‚   β”œβ”€β”€ interceptors/
β”‚   β”‚   └── utils/
β”‚   β”œβ”€β”€ modules/                     # Feature modules
β”‚   β”‚   β”œβ”€β”€ auth/
β”‚   β”‚   β”‚   β”œβ”€β”€ dto/
β”‚   β”‚   β”‚   β”œβ”€β”€ entities/
β”‚   β”‚   β”‚   β”œβ”€β”€ auth.controller.ts
β”‚   β”‚   β”‚   β”œβ”€β”€ auth.service.ts
β”‚   β”‚   β”‚   └── auth.module.ts
β”‚   β”‚   β”œβ”€β”€ users/
β”‚   β”‚   β”œβ”€β”€ addresses/
β”‚   β”‚   └── ...
β”‚   β”œβ”€β”€ jobs/                        # Background tasks
β”‚   β”‚   β”œβ”€β”€ queues/                  # BullMQ or Redis jobs
β”‚   β”‚   β”‚   β”œβ”€β”€ email.queue.ts
β”‚   β”‚   β”‚   └── report.queue.ts
β”‚   β”‚   β”œβ”€β”€ cron/                    # Scheduled jobs
β”‚   β”‚   β”‚   β”œβ”€β”€ cleanup.cron.ts
β”‚   β”‚   β”‚   └── analytics.cron.ts
β”‚   β”‚   β”œβ”€β”€ workers/                 # Long-running or async workers
β”‚   β”‚   β”‚   β”œβ”€β”€ pdf.worker.ts
β”‚   β”‚   β”‚   └── ...
β”‚   β”‚   └── jobs.module.ts
β”‚   β”œβ”€β”€ events/                      # Event-driven architecture
β”‚   β”‚   β”œβ”€β”€ handlers/
β”‚   β”‚   β”œβ”€β”€ emitters/
β”‚   β”‚   └── events.module.ts
β”‚   β”œβ”€β”€ notifications/              # Notification channels
β”‚   β”‚   β”œβ”€β”€ mail/
β”‚   β”‚   β”œβ”€β”€ sms/
β”‚   β”‚   β”œβ”€β”€ push/
β”‚   β”‚   └── notifications.module.ts
β”‚   β”œβ”€β”€ graphql/                    # Optional: GraphQL setup
β”‚   β”‚   β”œβ”€β”€ resolvers/
β”‚   β”‚   β”œβ”€β”€ schemas/
β”‚   β”‚   └── graphql.module.ts
β”‚   β”œβ”€β”€ logging/                    # Winston or Pino logger config
β”‚   β”‚   └── logger.service.ts
β”‚   β”œβ”€β”€ metrics/                    # Prometheus / monitoring
β”‚   β”‚   └── metrics.module.ts
β”‚   β”œβ”€β”€ health/                     # Health checks & readiness
β”‚   β”‚   └── health.controller.ts
β”‚   β”œβ”€β”€ webhooks/                   # Webhook listeners
β”‚   β”‚   β”œβ”€β”€ payment.webhook.ts
β”‚   β”‚   └── ...
β”‚   β”œβ”€β”€ schedulers/                 # Scheduler-based jobs
β”‚   β”‚   └── sync.scheduler.ts
β”‚   └── main.ts
β”œβ”€β”€ test/                            # Unit and e2e tests
β”‚   β”œβ”€β”€ auth.e2e-spec.ts
β”‚   β”œβ”€β”€ users.service.spec.ts
β”‚   └── ...
β”œβ”€β”€ .env                             # Environment variables
β”œβ”€β”€ nest-cli.json
β”œβ”€β”€ tsconfig.json
β”œβ”€β”€ package.json
└── README.md

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

0