Skip to content

Commit 3f30b61

Browse files
committed
code standarts for nestjs
1 parent 3d75ff4 commit 3f30b61

20 files changed

+18538
-0
lines changed

nestjs/.env

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
NODE_ENV=production
2+
3+
PORT=3000
4+
5+
POSTGRES_HOST=postgres
6+
POSTGRES_PORT=5432
7+
POSTGRES_USER=nestjsexample
8+
POSTGRES_PASSWORD=mypass
9+
POSTGRES_DB=nestjs

nestjs/.eslintrc.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
module.exports = {
2+
parser: '@typescript-eslint/parser',
3+
parserOptions: {
4+
project: 'tsconfig.json',
5+
sourceType: 'module',
6+
},
7+
plugins: ['@typescript-eslint/eslint-plugin'],
8+
extends: [
9+
'plugin:@typescript-eslint/recommended',
10+
'plugin:prettier/recommended',
11+
],
12+
root: true,
13+
env: {
14+
node: true,
15+
jest: true,
16+
},
17+
ignorePatterns: ['.eslintrc.js', '**/migrations/*.ts'],
18+
rules: {
19+
'@typescript-eslint/typedef': [
20+
'error',
21+
{
22+
arrowParameter: true,
23+
},
24+
],
25+
'@typescript-eslint/explicit-module-boundary-types': ['error'],
26+
'@typescript-eslint/no-explicit-any': ['error'],
27+
'@typescript-eslint/adjacent-overload-signatures': ['error'],
28+
'@typescript-eslint/explicit-function-return-type': ['error'],
29+
'no-console': ['warn'],
30+
'@typescript-eslint/explicit-member-accessibility': ['error'],
31+
'@typescript-eslint/no-duplicate-imports': ['error'],
32+
'no-unused-vars': 'off',
33+
'@typescript-eslint/no-unused-vars': ['error'],
34+
'@typescript-eslint/no-require-imports': ['error'],
35+
'@typescript-eslint/no-unnecessary-type-arguments': ['error'],
36+
'@typescript-eslint/non-nullable-type-assertion-style': ['error'],
37+
'@typescript-eslint/prefer-readonly': ['error'],
38+
'@typescript-eslint/no-unnecessary-type-constraint': ['error'],
39+
'@typescript-eslint/no-array-constructor': ['error'],
40+
'@typescript-eslint/no-invalid-void-type': ['error'],
41+
},
42+
};

nestjs/.gitignore

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# compiled output
2+
/dist
3+
/node_modules
4+
5+
# Logs
6+
logs
7+
*.log
8+
npm-debug.log*
9+
yarn-debug.log*
10+
yarn-error.log*
11+
lerna-debug.log*
12+
13+
# OS
14+
.DS_Store
15+
16+
# Tests
17+
/coverage
18+
/.nyc_output
19+
20+
# IDEs and editors
21+
/.idea
22+
.project
23+
.classpath
24+
.c9/
25+
*.launch
26+
.settings/
27+
*.sublime-workspace
28+
29+
# IDE - VSCode
30+
.vscode/*
31+
!.vscode/settings.json
32+
!.vscode/tasks.json
33+
!.vscode/launch.json
34+
!.vscode/extensions.json

nestjs/.prettierrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"singleQuote": true,
3+
"trailingComma": "all"
4+
}

nestjs/README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Code standarts for NestJS application
2+
3+
At the start a new project follow the next steps:
4+
5+
## Configure ESLint and Prettier
6+
7+
1. Run **npm install prettier eslint-plugin-prettier eslint-config-prettier eslint --save-dev**
8+
2. Copy [.prettierrc.json](.prettierrc.json) file to your project to root folder
9+
3. Copy [.eslintrc.json](.eslintrc.json) file to your project to root folder
10+
4. Add script - **"lint": "eslint \"{src,apps,libs,test}/\**/*.ts\" --fix"** to [.package.json](.package.json)
11+
12+
## Configure husky pre-commit
13+
14+
1. Run **npm install husky --save-dev**
15+
2. Add script - **"prepare": "husky install"** to [.package.json](.package.json)
16+
3. Run **npm run prepare** after that will be created .husky folder in root project
17+
4. Run **npx husky add .husky/pre-commit "npm lint"** after that will be created file pre-commit in .husky folder.
18+
In this file you can add any scripts what you want
19+
20+
## Configure environment
21+
22+
1. Run **npm i --save @nestjs/config**
23+
2. Add ConfigModule to [./src/app.module.ts](./src/app.module.ts) like in the example
24+
3. Copy [.env](.env) file to your project to root folder and change the secret data to yours
25+
26+
## Configure swagger
27+
28+
1. Run **npm install --save @nestjs/swagger**
29+
2. Сopy the swagger section from the [./src/main.ts](./src/main.ts) file
30+
3. Change the title, description and tag to your own
31+
32+
## Configure database
33+
34+
1. Run **npm install --save @nestjs/typeorm typeorm postgres pg**
35+
1. Copy the entire contents of the folder [./src/database/](./src/database/)
36+
2. Add TypeOrmModule to [./src/app.module.ts](./src/app.module.ts) like in the example
37+
38+
## Configure docker
39+
40+
1. Copy [.docker-compose.yaml.json](.docker-compose.yaml.json) file to your project to root folder
41+
2. Copy the entire contents of the folder [./docker/](./docker/)
42+
3. Copy [.init.sh](.init.sh) file to your project to root folder
43+
4. Start docker with command **COMPOSE_HTTP_TIMEOUT=300 docker-compose --env-file=.env -f docker-compose.yaml up -d --build**

nestjs/docker-compose.yaml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
version: '3.8'
2+
3+
services:
4+
postgres:
5+
image: postgres:latest
6+
ports:
7+
- ${POSTGRES_PORT}:${POSTGRES_PORT}
8+
volumes:
9+
- pgdata:/var/lib/postgresql/data
10+
- ./init.sh:/docker-entrypoint-initdb.d/init.sh
11+
environment:
12+
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
13+
POSTGRES_USER: ${POSTGRES_USER}
14+
POSTGRES_DB: ${POSTGRES_DB}
15+
restart: on-failure
16+
17+
backend:
18+
build:
19+
context: .
20+
dockerfile: ./docker/dockerfile
21+
depends_on:
22+
- postgres
23+
ports:
24+
- ${PORT}:${PORT}
25+
restart: on-failure
26+
27+
volumes:
28+
pgdata:

nestjs/docker/dockerfile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
FROM node:16.9.1-alpine3.14
2+
WORKDIR /base
3+
COPY package.json .
4+
RUN npm install
5+
COPY . .
6+
EXPOSE 3000
7+
CMD [ "sh", "-c", "npm run typeorm:run && npm run start:debug" ]

nestjs/init.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/bash
2+
set -e
3+
4+
psql -v ON_ERROR_STOP=1 --username "postgres" --dbname "postgres" <<-EOSQL
5+
CREATE DATABASE nestjs;
6+
USE nestjs;
7+
create user nestjsexample with encrypted password 'mypass';
8+
grant all on database nestjs to nestjsexample;
9+
EOSQL

nestjs/nest-cli.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"collection": "@nestjs/schematics",
3+
"sourceRoot": "src"
4+
}

0 commit comments

Comments
 (0)