- This repo for define template for reuse any Project APIs in NodeJS or GraphQL
- This Template using NestJS Framework for development and can make issues at HERE
- Discord Channel of NestJS Framework for more discussion.
- Create a
.env
file from.env.sample
and update value before start. - Edit env config
- Edit the file in the
src/config
folder. - Update value for each
ENVIRONMENT
atsrc/config/envs/{ENV_NAME}.ts
, example:default / development / production / test
- Edit the file in the
- Activating and configuring ESLint
Setup on WebStorm
- In the
Settings/Preferences
dialog(Ctrl+Alt+S)
, go toLanguages and Frameworks | JavaScript | Code Quality Tools | ESLint
. - Select the
Manual ESLint configuration
option to use a custom ESLint package and configuration. - In the
Node Interpreter
field, specify the path to Node.js. If you followed the standard installation procedure,WebStorm
detects the path and fills in the field itself. - In the
ESLint Package
field, specify the location of the eslint or standard package.{project_path}/node_modules/eslint
- In the
Configuration file
field, select this option to use a custom file and specify the file location in the Path field.{project_path}/.eslintrc
- Click
Apply > OK
Setup on VS Code
- Configure Visual Studio Code
- Please install the 3 extensions below into VS Code:
- ESLint extension
- EditorConfig for VS Code extension
- Restart VS Code
- This use for setup
- If you use multiple databases then need change some in
bin/entity.js at Line 52
# 1. node_modules - just for NPM
npm ci && npm i
# or
yarn install
# 2. When import entities from an existing database
npm run entity
# or
yarn run entity
- Start browser at
http://localhost:{port_in_env_file}
after run command under.
npm run dev
- For run test with unittest, e2e in codebase
npm test # exclude e2e
npm run e2e
- Use only for production env
npm run build
# define environment variable yourself.
# NODE_ENV=production PORT=8000 NO_COLOR=true node dist/app
node dist/app
# or
npm start
# or
yarn start
+-- bin // Custom tasks
+-- dist // Source build
+-- public // Static Files
+-- src
| +-- config // Environment Configuration
| +-- entities // TypeORM Entities generated by `typeorm-model-generator` module
| +-- modules // Store all module source here each sub-folder is one module and it's includes all files in this
| | +-- auth // Authentication module
| +-- common // Global Nest Module
| | +-- base // base class for project
| | +-- constants // Constant value and Enum
| | +-- controllers // Nest Controllers
| | +-- decorators // Nest Decorators
| | +-- dto // DTO (Data Transfer Object) Schema, Validation
| | +-- filters // Nest Filters
| | +-- guards // Nest Guards
| | +-- loggers // All logger folder
| | +-- interceptors // Nest Interceptors
| | +-- interfaces // TypeScript Interfaces
| | +-- middleware // Nest Middleware
| | +-- pipes // Nest Pipes
| | +-- providers // Nest Providers
| | +-- models // Global models
| | +-- services // Global services
| | +-- * // repositories...
| +-- shared // Shared Nest Modules
| +-- debug // custom debug
| +-- gql // GraphQL Structure
| +-- * // Other Nest Modules, non-global, same as common structure above
+-- tests // 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/greeter
| +-- * // folders
| +-- greeter.constant.ts // all constant for this module
| +-- greeter.controller.ts // controller file for this module
| +-- greeter.service.ts // service file for handle business logic and communicate with model for response data to controller
| +-- greeter.module.ts // sub-module file to setup module for NestJS
| +-- greeter.*.ts
| +-- index.ts
- See Bootstrap & App Module
- Database, Module Router, Static Files, Validation
- Global Exception Filter
- Global Logging Middleware
- Custom Logger for Production
- Custom Decorators Example at Nest level
- Configuration
- Authentication - JWT and Session login with Passport
- Role-based Guard
- Controller Routes
- Auth Login
- Sample Parameter and DTO
- CRUD API Sample
- Database Query Example
- Unit Test
- E2E Test
- Shared Modules Example
- GraphQL Structure Example
# APP, Compodoc
npm run doc #> http://localhost:8080
# API, Swagger - src/swagger.ts
npm run api #> http://localhost:8000/api
- Some rules for write code in this template
It is recommended to apply the known extended presets in addition to the basic rules
- eslint-config-airbnb-typescript
- eslint-config-airbnb with TypeScript support
- eslint-plugin-sonarjs
- eslint-plugin-unicorn
Conventional Commits: For consistent commit message
Semantic Versioning: For automatic versioning and changelog based on consistent commit messages
- Standard Version
- If you publish the project as a package, use semantic-release.
# It's recommended to place index.ts in each folder and export.
# Unless it's a special case, it's 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';
# My preferred method is to place only one fileOrFolder name at the end of the path.
- import { UtilService } from '../common/providers/util.service';
+ import { UtilService } from '../common';
# Do not use a path that ends with a dot.
- import { FooService } from '.';
- import { BarService } from '..';
+ import { FooService } from './foo.service';
+ import { BarService } from '../providers';