Skip to content

Commit 2319b1a

Browse files
committed
Add TypeScript, NodeJS, Express based Microservice Template
1 parent 442f135 commit 2319b1a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1117
-106
lines changed

.dockerignore

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
node_modules/
2+
npm-debug.log
3+
4+
dist/
5+
build/
6+
7+
Dockerfile*
8+
docker-compose*
9+
.dockerignore
10+
11+
.git
12+
.gitignore
13+
14+
.vscode
15+
*.log
16+
17+
.env

.env.example

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
NODE_ENV=development
2+
3+
# Application related config
4+
PORT=9000
5+
6+
# DB related config
7+
DB_TYPE=sqlite
8+
DB_HOST=db.db
9+
DB_PORT=
10+
DB_USERNAME=
11+
DB_PASSWORD=
12+
DB_DATABASE=
13+
DB_LOGGING=true
14+
15+
# Use DB_SYNCHRONIZE option on local development only
16+
# DB_SYNCHRONIZE=true
17+
18+
# Use this option when you want to run migrations on each run (On Dev/QA Instance)
19+
DB_MIGRATIONS_RUN=true

.eslintignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Ignore artifacts
2+
build
3+
dist
4+
coverage
5+
node_modules

.eslintrc.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"root": true,
3+
"parser": "@typescript-eslint/parser",
4+
"plugins": [
5+
"@typescript-eslint",
6+
"prettier"
7+
],
8+
"extends": [
9+
"eslint:recommended",
10+
"plugin:@typescript-eslint/eslint-recommended",
11+
"plugin:@typescript-eslint/recommended",
12+
"plugin:prettier/recommended",
13+
"prettier/@typescript-eslint",
14+
"prettier"
15+
],
16+
"rules": {}
17+
}

.gitignore

Lines changed: 9 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -1,104 +1,9 @@
1-
# Logs
2-
logs
3-
*.log
4-
npm-debug.log*
5-
yarn-debug.log*
6-
yarn-error.log*
7-
lerna-debug.log*
8-
9-
# Diagnostic reports (https://nodejs.org/api/report.html)
10-
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
11-
12-
# Runtime data
13-
pids
14-
*.pid
15-
*.seed
16-
*.pid.lock
17-
18-
# Directory for instrumented libs generated by jscoverage/JSCover
19-
lib-cov
20-
21-
# Coverage directory used by tools like istanbul
22-
coverage
23-
*.lcov
24-
25-
# nyc test coverage
26-
.nyc_output
27-
28-
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
29-
.grunt
30-
31-
# Bower dependency directory (https://bower.io/)
32-
bower_components
33-
34-
# node-waf configuration
35-
.lock-wscript
36-
37-
# Compiled binary addons (https://nodejs.org/api/addons.html)
38-
build/Release
39-
40-
# Dependency directories
41-
node_modules/
42-
jspm_packages/
43-
44-
# TypeScript v1 declaration files
45-
typings/
46-
47-
# TypeScript cache
48-
*.tsbuildinfo
49-
50-
# Optional npm cache directory
51-
.npm
52-
53-
# Optional eslint cache
54-
.eslintcache
55-
56-
# Microbundle cache
57-
.rpt2_cache/
58-
.rts2_cache_cjs/
59-
.rts2_cache_es/
60-
.rts2_cache_umd/
61-
62-
# Optional REPL history
63-
.node_repl_history
64-
65-
# Output of 'npm pack'
66-
*.tgz
67-
68-
# Yarn Integrity file
69-
.yarn-integrity
70-
71-
# dotenv environment variables file
72-
.env
73-
.env.test
74-
75-
# parcel-bundler cache (https://parceljs.org/)
76-
.cache
77-
78-
# Next.js build output
79-
.next
80-
81-
# Nuxt.js build / generate output
82-
.nuxt
83-
dist
84-
85-
# Gatsby files
86-
.cache/
87-
# Comment in the public line in if your project uses Gatsby and *not* Next.js
88-
# https://nextjs.org/blog/next-9-1#public-directory-support
89-
# public
90-
91-
# vuepress build output
92-
.vuepress/dist
93-
94-
# Serverless directories
95-
.serverless/
96-
97-
# FuseBox cache
98-
.fusebox/
99-
100-
# DynamoDB Local files
101-
.dynamodb/
102-
103-
# TernJS port file
104-
.tern-port
1+
.idea/
2+
.vscode/
3+
node_modules*/
4+
build/
5+
dist/
6+
tmp/
7+
temp/
8+
9+
.env

.prettierignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Ignore artifacts
2+
build
3+
dist
4+
coverage

.prettierrc.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"semi": true,
3+
"useTabs": false,
4+
"tabWidth": 2,
5+
"printWidth": 120,
6+
"trailingComma": "all",
7+
"singleQuote": true,
8+
"eslintIntegration": true,
9+
"endOfLine": "auto"
10+
}

Dockerfile

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
########### Stage 1 ###########
2+
FROM node:14.15.0-alpine as builder
3+
4+
# Update image
5+
RUN apk update
6+
7+
# Add work directory
8+
WORKDIR /app
9+
10+
# Use "node" user
11+
RUN chown -Rh node:node /app
12+
USER node
13+
14+
# Install npm modules
15+
COPY package*.json ./
16+
RUN npm ci --quiet
17+
18+
# Copy source code
19+
COPY tsconfig.json ./
20+
COPY jest.config.js ./
21+
COPY src /app/src
22+
23+
# Build
24+
RUN npm run build
25+
26+
########### Stage 2 ###########
27+
FROM node:14.15.0-alpine
28+
LABEL maintainer="vs4vijay@gmail.com"
29+
30+
WORKDIR /app
31+
32+
# Install npm modules
33+
COPY package*.json ./
34+
RUN npm ci --quiet --only=production
35+
36+
# Copy build from previous stage
37+
COPY --from=builder /app/dist ./dist
38+
39+
# Expose port
40+
EXPOSE 9000
41+
42+
CMD [ "node", "./dist/app.js" ]

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2021 Vizay Soni
3+
Copyright (c) 2020 Vizay Soni
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 106 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,106 @@
1-
# typescript-express-template
1+
# typescript-express-template
2+
3+
A Microservice Template in NodeJS, TypeScript, and Express
4+
5+
Express + Routing-Controllers + TypeScript + TypeORM + TypeDI
6+
7+
---
8+
9+
## Installation
10+
11+
```shell
12+
npm install
13+
```
14+
15+
- Create `.env` file from `.env.example` and populate environment variables
16+
17+
---
18+
19+
## Running
20+
21+
```shell
22+
# Build and Run
23+
npm start
24+
25+
# Run on local machine with watcher
26+
npm run start:dev
27+
```
28+
29+
---
30+
31+
## Features
32+
33+
- [x] TypeScript
34+
- [x] Depedency Injection of Services, Repositories, Controllers
35+
- [x] Modular App.ts
36+
- [x] Properly structured codebase models, repositories, services, controllers, migrations etc.
37+
- [x] ORM Migrations used for maintaining database schemas
38+
- [x] Follows pure REST APIs
39+
- [x] Input validations
40+
- [x] Use of `.env` file
41+
- [x] Git pre-commit hooks setup
42+
- [x] Linting and Standard Formatting
43+
- [x] Added system metadata like createdAt, updatedAt
44+
- [x] Use of DTOs
45+
- [x] Added audit log like createdBy, updatedBy
46+
- [x] isActive
47+
- [x] uuid as primary key
48+
- [x] Soft Delete Options
49+
- [x] Pagination
50+
- [ ] Structured Logging
51+
- [ ] Unit Testing
52+
- [ ] Authentication
53+
- [ ] Authorization
54+
- [ ] Error Handling and Generic Error Middleware
55+
- [ ] Search Framework
56+
- [ ] AbstractService or interface Service
57+
- [ ] Graceful Shutdown
58+
- [x] Containerized with Docker
59+
60+
---
61+
62+
## Libraries Used
63+
64+
- NodeJS with TypeScript
65+
- ts-node and ts-node-dev for running on local machine
66+
- Express Framework
67+
- TypeORM - ORM Tool to interact with Database
68+
- TypeDI - Dependency Injection library (https://github.com/typestack/typedi)
69+
- routing-controllers - For Defining Routes in elegent way (https://github.com/typestack/routing-controllers)
70+
- class-validator - For input validations (https://github.com/typestack/class-validator)
71+
- class-transformer - For transforming objects (https://github.com/typestack/class-transformer)
72+
- dotenv - Use Environment Variables from .env file
73+
- ESLint - For Linting the ES and TypeScript codebase
74+
- `ext install dbaeumer.vscode-eslint`
75+
- Prettier - For Formatting Standard (https://eslint.org/docs/user-guide/getting-started)
76+
- `ext install esbenp.prettier-vscode`
77+
- Husky - https://github.com/typicode/husky
78+
- lint-staged - https://github.com/okonet/lint-staged
79+
80+
---
81+
82+
## Development
83+
84+
- Create an entity: `npm run typeorm -- entity:create -n User`
85+
- Generate migration: `npm run typeorm -- migration:generate -n CreateUser`
86+
- Run the migrations: `npm run typeorm -- migration:run`
87+
88+
Notes:
89+
90+
- Migration name pattern: Add<field>To<Entity>
91+
- AddLastNameToUser
92+
- UpdateEmailToEmployee
93+
94+
---
95+
96+
### Development Guidelines
97+
98+
- Make use of `index.ts` in folder which has multiple files
99+
100+
---
101+
102+
### Development Notes
103+
104+
```javascript
105+
106+
```

0 commit comments

Comments
 (0)