├── .dockerignore ├── .eslintrc.js ├── .gitignore ├── .prettierrc ├── Dockerfile ├── FUNDING.yml ├── README.md ├── docker-compose.yml ├── nest-cli.json ├── package-lock.json ├── package.json ├── src ├── app.controller.spec.ts ├── app.controller.ts ├── app.module.ts ├── app.service.ts ├── main.ts └── user │ ├── controller │ ├── user.controller.spec.ts │ └── user.controller.ts │ ├── models │ ├── user.entity.ts │ └── user.interface.ts │ ├── service │ ├── user.service.spec.ts │ └── user.service.ts │ └── user.module.ts ├── test ├── app.e2e-spec.ts └── jest-e2e.json ├── tsconfig.build.json └── tsconfig.json /.dockerignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | npm-debug.log -------------------------------------------------------------------------------- /.eslintrc.js: -------------------------------------------------------------------------------- 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/eslint-recommended', 10 | 'plugin:@typescript-eslint/recommended', 11 | 'prettier', 12 | 'prettier/@typescript-eslint', 13 | ], 14 | root: true, 15 | env: { 16 | node: true, 17 | jest: true, 18 | }, 19 | rules: { 20 | '@typescript-eslint/interface-name-prefix': 'off', 21 | '@typescript-eslint/explicit-function-return-type': 'off', 22 | '@typescript-eslint/no-explicit-any': 'off', 23 | }, 24 | }; 25 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 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 -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "singleQuote": true, 3 | "trailingComma": "all" 4 | } -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM node:12 2 | 3 | # Create app directory, this is in our container/in our image 4 | WORKDIR /thomas/src/app 5 | 6 | # Install app dependencies 7 | # A wildcard is used to ensure both package.json AND package-lock.json are copied 8 | # where available (npm@5+) 9 | COPY package*.json ./ 10 | 11 | RUN npm install 12 | # If you are building your code for production 13 | # RUN npm ci --only=production 14 | 15 | # Bundle app source 16 | COPY . . 17 | 18 | RUN npm run build 19 | 20 | EXPOSE 8080 21 | CMD [ "node", "dist/main" ] -------------------------------------------------------------------------------- /FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: ThomasOliver545 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Content Video 1 2 | Dockerize the NestJS starter App. 3 | https://www.youtube.com/watch?v=BrlQthcUHGw 4 | 5 | # Content Video 2 6 | - Add a docker-compose file and run also a postgres db 7 | - Connect with NestJS to the db 8 | - Post Data && Get Data over http (to NestJS) and then save it in the db 9 | 10 | https://www.youtube.com/watch?v=jYFyLLqvHy8 11 | (published on 05th of November 2020) 12 | 13 | # You need 14 | - NPM 15 | - Node.js 16 | - NestJS 17 | - Docker 18 | 19 | # Start Commands for docker-compose file 20 | Builds, (re)creates, starts, and attaches to containers for a service. 21 | `docker-compose up` 22 | 23 | # Start Commands for Docker 24 | Build your image: 25 | `docker build -t </project-name>` 26 | 27 | Run: 28 | `docker run -p 8080:3000 </project-name>` 29 | 30 | For Example: 31 | `docker build -t thomas-oliver/nestjs-dockerized` 32 | `docker run -p 8080:3000 thomas-oliver/nestjs-dockerized` 33 | 34 | Basic Docker Commands: 35 | List your docker images: `docker images` 36 | List your running containers: `docker ps` 37 | List also stopped containers: `docker ps -a` 38 | Kill a running container: `docker kill `, eg `docker kill fea` 39 | 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: "3.8" 2 | services: 3 | api: 4 | # image: thomas-oliver/nestjs-dockerized 5 | build: 6 | dockerfile: Dockerfile 7 | context: . 8 | depends_on: 9 | - postgres 10 | environment: 11 | DATABASE_URL: postgres://user:password@postgres:5432/db 12 | NODE_ENV: development 13 | PORT: 3000 14 | ports: 15 | - "8080:3000" 16 | 17 | postgres: 18 | image: postgres:10.4 19 | ports: 20 | - "35000:5432" 21 | environment: 22 | POSTGRES_USER: user 23 | POSTGRES_PASSWORD: password 24 | POSTGRES_DB: db 25 | -------------------------------------------------------------------------------- /nest-cli.json: -------------------------------------------------------------------------------- 1 | { 2 | "collection": "@nestjs/schematics", 3 | "sourceRoot": "src" 4 | } 5 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nestjs-dockerized", 3 | "version": "0.0.1", 4 | "description": "", 5 | "author": "", 6 | "private": true, 7 | "license": "UNLICENSED", 8 | "scripts": { 9 | "prebuild": "rimraf dist", 10 | "build": "nest build", 11 | "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"", 12 | "start": "nest start", 13 | "start:dev": "nest start --watch", 14 | "start:debug": "nest start --debug --watch", 15 | "start:prod": "node dist/main", 16 | "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix", 17 | "test": "jest", 18 | "test:watch": "jest --watch", 19 | "test:cov": "jest --coverage", 20 | "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand", 21 | "test:e2e": "jest --config ./test/jest-e2e.json" 22 | }, 23 | "dependencies": { 24 | "@nestjs/common": "^7.0.0", 25 | "@nestjs/config": "^0.5.0", 26 | "@nestjs/core": "^7.0.0", 27 | "@nestjs/platform-express": "^7.0.0", 28 | "@nestjs/typeorm": "^7.1.4", 29 | "pg": "^8.4.2", 30 | "reflect-metadata": "^0.1.13", 31 | "rimraf": "^3.0.2", 32 | "rxjs": "^6.5.4", 33 | "typeorm": "^0.2.28" 34 | }, 35 | "devDependencies": { 36 | "@nestjs/cli": "^7.0.0", 37 | "@nestjs/schematics": "^7.0.0", 38 | "@nestjs/testing": "^7.0.0", 39 | "@types/express": "^4.17.3", 40 | "@types/jest": "25.2.3", 41 | "@types/node": "^13.9.1", 42 | "@types/supertest": "^2.0.8", 43 | "@typescript-eslint/eslint-plugin": "3.0.2", 44 | "@typescript-eslint/parser": "3.0.2", 45 | "eslint": "7.1.0", 46 | "eslint-config-prettier": "^6.10.0", 47 | "eslint-plugin-import": "^2.20.1", 48 | "jest": "26.0.1", 49 | "prettier": "^1.19.1", 50 | "supertest": "^4.0.2", 51 | "ts-jest": "26.1.0", 52 | "ts-loader": "^6.2.1", 53 | "ts-node": "^8.6.2", 54 | "tsconfig-paths": "^3.9.0", 55 | "typescript": "^3.7.4" 56 | }, 57 | "jest": { 58 | "moduleFileExtensions": [ 59 | "js", 60 | "json", 61 | "ts" 62 | ], 63 | "rootDir": "src", 64 | "testRegex": ".spec.ts$", 65 | "transform": { 66 | "^.+\\.(t|j)s$": "ts-jest" 67 | }, 68 | "coverageDirectory": "../coverage", 69 | "testEnvironment": "node" 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/app.controller.spec.ts: -------------------------------------------------------------------------------- 1 | import { Test, TestingModule } from '@nestjs/testing'; 2 | import { AppController } from './app.controller'; 3 | import { AppService } from './app.service'; 4 | 5 | describe('AppController', () => { 6 | let appController: AppController; 7 | 8 | beforeEach(async () => { 9 | const app: TestingModule = await Test.createTestingModule({ 10 | controllers: [AppController], 11 | providers: [AppService], 12 | }).compile(); 13 | 14 | appController = app.get(AppController); 15 | }); 16 | 17 | describe('root', () => { 18 | it('should return "Hello World!"', () => { 19 | expect(appController.getHello()).toBe('Hello World!'); 20 | }); 21 | }); 22 | }); 23 | -------------------------------------------------------------------------------- /src/app.controller.ts: -------------------------------------------------------------------------------- 1 | import { Controller, Get } from '@nestjs/common'; 2 | import { AppService } from './app.service'; 3 | 4 | @Controller() 5 | export class AppController { 6 | constructor(private readonly appService: AppService) {} 7 | 8 | @Get() 9 | getHello(): string { 10 | return this.appService.getHello(); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/app.module.ts: -------------------------------------------------------------------------------- 1 | import { Module } from '@nestjs/common'; 2 | import { AppController } from './app.controller'; 3 | import { AppService } from './app.service'; 4 | import {TypeOrmModule} from '@nestjs/typeorm'; 5 | import { ConfigModule } from '@nestjs/config'; 6 | import { UserModule } from './user/user.module'; 7 | 8 | @Module({ 9 | imports: [ 10 | ConfigModule.forRoot({isGlobal: true}), 11 | TypeOrmModule.forRoot({ 12 | type: 'postgres', 13 | url: process.env.DATABASE_URL, 14 | autoLoadEntities: true, 15 | synchronize: true 16 | }), 17 | UserModule 18 | ], 19 | controllers: [AppController], 20 | providers: [AppService], 21 | }) 22 | export class AppModule {} 23 | -------------------------------------------------------------------------------- /src/app.service.ts: -------------------------------------------------------------------------------- 1 | import { Injectable } from '@nestjs/common'; 2 | 3 | @Injectable() 4 | export class AppService { 5 | getHello(): string { 6 | return 'Hello World!'; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/main.ts: -------------------------------------------------------------------------------- 1 | import { NestFactory } from '@nestjs/core'; 2 | import { AppModule } from './app.module'; 3 | 4 | async function bootstrap() { 5 | const app = await NestFactory.create(AppModule); 6 | await app.listen(3000); 7 | } 8 | bootstrap(); 9 | -------------------------------------------------------------------------------- /src/user/controller/user.controller.spec.ts: -------------------------------------------------------------------------------- 1 | import { Test, TestingModule } from '@nestjs/testing'; 2 | import { UserController } from './user.controller'; 3 | 4 | describe('UserController', () => { 5 | let controller: UserController; 6 | 7 | beforeEach(async () => { 8 | const module: TestingModule = await Test.createTestingModule({ 9 | controllers: [UserController], 10 | }).compile(); 11 | 12 | controller = module.get(UserController); 13 | }); 14 | 15 | it('should be defined', () => { 16 | expect(controller).toBeDefined(); 17 | }); 18 | }); 19 | -------------------------------------------------------------------------------- /src/user/controller/user.controller.ts: -------------------------------------------------------------------------------- 1 | import { Body, Controller, Get, Post } from '@nestjs/common'; 2 | import { Observable } from 'rxjs'; 3 | import { runInThisContext } from 'vm'; 4 | import { UserI } from '../models/user.interface'; 5 | import { UserService } from '../service/user.service'; 6 | 7 | @Controller('users') 8 | export class UserController { 9 | 10 | constructor(private userService: UserService) {} 11 | 12 | @Post() 13 | add(@Body() user: UserI): Observable { 14 | return this.userService.add(user); 15 | } 16 | 17 | @Get() 18 | findAll(): Observable { 19 | return this.userService.findAll(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/user/models/user.entity.ts: -------------------------------------------------------------------------------- 1 | import { Column, Entity, PrimaryGeneratedColumn } from "typeorm"; 2 | 3 | @Entity() 4 | export class UserEntity { 5 | 6 | @PrimaryGeneratedColumn() 7 | id: number; 8 | 9 | @Column() 10 | name: string; 11 | 12 | } -------------------------------------------------------------------------------- /src/user/models/user.interface.ts: -------------------------------------------------------------------------------- 1 | export interface UserI { 2 | id: number; 3 | name: string; 4 | } -------------------------------------------------------------------------------- /src/user/service/user.service.spec.ts: -------------------------------------------------------------------------------- 1 | import { Test, TestingModule } from '@nestjs/testing'; 2 | import { UserService } from './user.service'; 3 | 4 | describe('UserService', () => { 5 | let service: UserService; 6 | 7 | beforeEach(async () => { 8 | const module: TestingModule = await Test.createTestingModule({ 9 | providers: [UserService], 10 | }).compile(); 11 | 12 | service = module.get(UserService); 13 | }); 14 | 15 | it('should be defined', () => { 16 | expect(service).toBeDefined(); 17 | }); 18 | }); 19 | -------------------------------------------------------------------------------- /src/user/service/user.service.ts: -------------------------------------------------------------------------------- 1 | import { Injectable } from '@nestjs/common'; 2 | import { InjectRepository } from '@nestjs/typeorm'; 3 | import { from, Observable } from 'rxjs'; 4 | import { Repository } from 'typeorm'; 5 | import { UserEntity } from '../models/user.entity'; 6 | import { UserI } from '../models/user.interface'; 7 | 8 | @Injectable() 9 | export class UserService { 10 | 11 | constructor( 12 | @InjectRepository(UserEntity) 13 | private userRepository: Repository 14 | ) {} 15 | 16 | add(user: UserI): Observable { 17 | return from(this.userRepository.save(user)); 18 | } 19 | 20 | findAll(): Observable { 21 | return from(this.userRepository.find()); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/user/user.module.ts: -------------------------------------------------------------------------------- 1 | import { Module } from '@nestjs/common'; 2 | import { UserService } from './service/user.service'; 3 | import { UserController } from './controller/user.controller'; 4 | import { TypeOrmModule } from '@nestjs/typeorm'; 5 | import { UserEntity } from './models/user.entity'; 6 | 7 | @Module({ 8 | imports: [ 9 | TypeOrmModule.forFeature([UserEntity]) 10 | ], 11 | providers: [UserService], 12 | controllers: [UserController] 13 | }) 14 | export class UserModule {} 15 | -------------------------------------------------------------------------------- /test/app.e2e-spec.ts: -------------------------------------------------------------------------------- 1 | import { Test, TestingModule } from '@nestjs/testing'; 2 | import { INestApplication } from '@nestjs/common'; 3 | import * as request from 'supertest'; 4 | import { AppModule } from './../src/app.module'; 5 | 6 | describe('AppController (e2e)', () => { 7 | let app: INestApplication; 8 | 9 | beforeEach(async () => { 10 | const moduleFixture: TestingModule = await Test.createTestingModule({ 11 | imports: [AppModule], 12 | }).compile(); 13 | 14 | app = moduleFixture.createNestApplication(); 15 | await app.init(); 16 | }); 17 | 18 | it('/ (GET)', () => { 19 | return request(app.getHttpServer()) 20 | .get('/') 21 | .expect(200) 22 | .expect('Hello World!'); 23 | }); 24 | }); 25 | -------------------------------------------------------------------------------- /test/jest-e2e.json: -------------------------------------------------------------------------------- 1 | { 2 | "moduleFileExtensions": ["js", "json", "ts"], 3 | "rootDir": ".", 4 | "testEnvironment": "node", 5 | "testRegex": ".e2e-spec.ts$", 6 | "transform": { 7 | "^.+\\.(t|j)s$": "ts-jest" 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /tsconfig.build.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.json", 3 | "exclude": ["node_modules", "test", "dist", "**/*spec.ts"] 4 | } 5 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "module": "commonjs", 4 | "declaration": true, 5 | "removeComments": true, 6 | "emitDecoratorMetadata": true, 7 | "experimentalDecorators": true, 8 | "allowSyntheticDefaultImports": true, 9 | "target": "es2017", 10 | "sourceMap": true, 11 | "outDir": "./dist", 12 | "baseUrl": "./", 13 | "incremental": true 14 | } 15 | } 16 | --------------------------------------------------------------------------------