Skip to content

dusknguyen/nestjs-mysql-api-structure

Repository files navigation

Nestjs API

Introduce

API NestJS project structure

How to use this template

Folders

+--bin // Custom tasks
+--dist // Source build
+--public // Static Files
+--src
| +--config // Environment Configuration
| +--constants // Constant value and Enum
| +--entity // TypeORM Entities generated by `typeorm-model-generator` module
| +--modules
| | +--common // Basic Common Project Nest Module
| | | +--decorators // Nest Decorators
| | | +--filters // Nest Filters
| | | +--guards // Nest Guards
| | | +--middleware // Nest Middleware
| | | +--providers // Common Providers
| | +--share // Shareable Common Library Nest Module
| | +--_ // _ Module
| | | +--controllers // Nest Controllers
| | | | +--_.controller.ts
| | | +--dto // DTO (Data Transfer Object) Schema, Validation
| | | +--providers // Nest Providers
+--test // Jest testing
+--typings // Modules and global type definitions
// Module structure
// Add folders according to module scale. If it's small, you don't need to add folders.
+-- src/modules/sample
| +-- _ // folders
| | | +-- sample._.ts
| +-- controllers
| | | +--sample.controller.ts
| | | +-- sample._.ts
| +-- providers
| | | +-- sample.service.ts
| | | +-- sample.\*.ts
| +-- sample.module.ts
| +-- index.ts

File Naming for Class

export class PascalCaseSuffix {} //= pascal-case.suffix.ts
// Except for suffix, PascalCase to hyphen-case
class FooBarNaming {} //= foo-bar.naming.ts
class FooController {} //= foo.controller.ts
class BarQueryDto {} //= bar-query.dtos.ts

Interface Naming

// https://stackoverflow.com/questions/541912
// https://stackoverflow.com/questions/2814805
interface User {}

interface CustomeUser extends User {}

interface ThirdCustomeUser extends CustomeUser {}

Index Exporting

# It is recommended to place index.ts in each folder and export.
# Unless it's a special case, it is import from a folder instead of directly from a file.
- import { FooController } from './controllers/foo.controller';
- import { BarController } from './controllers/bar.controller';
+ import { FooController, BarController } from './controllers';

Variables Naming

refer to Naming cheatsheet

Some Notes