├── .prettierrc ├── nest-cli.json ├── tsconfig.build.json ├── src ├── typeorm │ ├── index.ts │ ├── customer.entity.ts │ └── user.entity.ts ├── customers │ ├── dtos │ │ └── CreateCustomer.dto.ts │ ├── customers.module.ts │ ├── services │ │ └── customers │ │ │ ├── customers.service.spec.ts │ │ │ └── customers.service.ts │ └── controllers │ │ └── customers │ │ ├── customers.controller.spec.ts │ │ └── customers.controller.ts ├── users │ ├── dtos │ │ └── CreateUser.dto.ts │ ├── users.module.ts │ ├── services │ │ └── users │ │ │ ├── users.service.spec.ts │ │ │ └── users.service.ts │ └── controllers │ │ └── users │ │ ├── users.controller.spec.ts │ │ └── users.controller.ts ├── main.ts └── app.module.ts ├── test ├── jest-e2e.json └── app.e2e-spec.ts ├── .gitignore ├── tsconfig.json ├── .eslintrc.js ├── package.json └── README.md /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "singleQuote": true, 3 | "trailingComma": "all" 4 | } -------------------------------------------------------------------------------- /nest-cli.json: -------------------------------------------------------------------------------- 1 | { 2 | "collection": "@nestjs/schematics", 3 | "sourceRoot": "src" 4 | } 5 | -------------------------------------------------------------------------------- /tsconfig.build.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.json", 3 | "exclude": ["node_modules", "test", "dist", "**/*spec.ts"] 4 | } 5 | -------------------------------------------------------------------------------- /src/typeorm/index.ts: -------------------------------------------------------------------------------- 1 | import { Customer } from "./customer.entity"; 2 | import { User } from "./user.entity"; 3 | 4 | const entities = [User, Customer]; 5 | 6 | export {User, Customer}; 7 | export default entities; -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /src/customers/dtos/CreateCustomer.dto.ts: -------------------------------------------------------------------------------- 1 | import { IsEmail, IsNotEmpty, MinLength } from "class-validator"; 2 | 3 | export class CreateCustomerDto { 4 | @IsNotEmpty() 5 | @MinLength(3) 6 | username: string; 7 | 8 | @IsNotEmpty() 9 | @IsEmail() 10 | email: string; 11 | } -------------------------------------------------------------------------------- /src/users/dtos/CreateUser.dto.ts: -------------------------------------------------------------------------------- 1 | import { IsEmail, IsNotEmpty, MinLength } from "class-validator"; 2 | 3 | export class CreateUserDto { 4 | @IsNotEmpty() 5 | @MinLength(3) 6 | username: string; 7 | 8 | @IsNotEmpty() 9 | @MinLength(8) 10 | password: string; 11 | 12 | @IsNotEmpty() 13 | @IsEmail() 14 | email: string; 15 | } -------------------------------------------------------------------------------- /src/users/users.module.ts: -------------------------------------------------------------------------------- 1 | import { Module } from '@nestjs/common'; 2 | import { UsersController } from './controllers/users/users.controller'; 3 | import { UsersService } from './services/users/users.service'; 4 | import { TypeOrmModule } from '@nestjs/typeorm'; 5 | import { User } from 'src/typeorm'; 6 | 7 | 8 | @Module({ 9 | imports: [TypeOrmModule.forFeature([User])], 10 | controllers: [UsersController], 11 | providers: [UsersService] 12 | }) 13 | export class UsersModule {} 14 | -------------------------------------------------------------------------------- /src/typeorm/customer.entity.ts: -------------------------------------------------------------------------------- 1 | import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm'; 2 | 3 | @Entity() 4 | export class Customer { 5 | @PrimaryGeneratedColumn({ 6 | type: 'bigint', 7 | name: 'customer_id', 8 | }) 9 | id: number; 10 | 11 | @Column({ 12 | nullable: false, 13 | default: '', 14 | }) 15 | username: string; 16 | 17 | @Column({ 18 | nullable: false, 19 | default: '', 20 | name: 'email_address', 21 | }) 22 | email: string; 23 | } -------------------------------------------------------------------------------- /src/customers/customers.module.ts: -------------------------------------------------------------------------------- 1 | import { Module } from '@nestjs/common'; 2 | import { Customer } from 'src/typeorm'; 3 | import { CustomersController } from './controllers/customers/customers.controller'; 4 | import { CustomersService } from './services/customers/customers.service'; 5 | import { TypeOrmModule } from '@nestjs/typeorm'; 6 | 7 | 8 | @Module({ 9 | imports: [TypeOrmModule.forFeature([Customer])], 10 | controllers: [CustomersController], 11 | providers: [CustomersService] 12 | }) 13 | export class CustomersModule {} 14 | -------------------------------------------------------------------------------- /src/users/services/users/users.service.spec.ts: -------------------------------------------------------------------------------- 1 | import { Test, TestingModule } from '@nestjs/testing'; 2 | import { UsersService } from './users.service'; 3 | 4 | describe('UsersService', () => { 5 | let service: UsersService; 6 | 7 | beforeEach(async () => { 8 | const module: TestingModule = await Test.createTestingModule({ 9 | providers: [UsersService], 10 | }).compile(); 11 | 12 | service = module.get(UsersService); 13 | }); 14 | 15 | it('should be defined', () => { 16 | expect(service).toBeDefined(); 17 | }); 18 | }); 19 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # compiled output 2 | /dist 3 | /node_modules 4 | 5 | # Logs 6 | logs 7 | *.log 8 | npm-debug.log* 9 | pnpm-debug.log* 10 | yarn-debug.log* 11 | yarn-error.log* 12 | lerna-debug.log* 13 | 14 | # OS 15 | .DS_Store 16 | 17 | # Tests 18 | /coverage 19 | /.nyc_output 20 | 21 | # IDEs and editors 22 | /.idea 23 | .project 24 | .classpath 25 | .c9/ 26 | *.launch 27 | .settings/ 28 | *.sublime-workspace 29 | 30 | # IDE - VSCode 31 | .vscode/* 32 | !.vscode/settings.json 33 | !.vscode/tasks.json 34 | !.vscode/launch.json 35 | !.vscode/extensions.json 36 | 37 | 38 | #env 39 | /.env 40 | -------------------------------------------------------------------------------- /src/typeorm/user.entity.ts: -------------------------------------------------------------------------------- 1 | import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm'; 2 | 3 | @Entity() 4 | export class User { 5 | @PrimaryGeneratedColumn({ 6 | type: 'bigint', 7 | name: 'user_id', 8 | }) 9 | id: number; 10 | 11 | @Column({ 12 | nullable: false, 13 | default: '', 14 | }) 15 | username: string; 16 | 17 | @Column({ 18 | name: 'email_address', 19 | nullable: false, 20 | default: '', 21 | }) 22 | email: string; 23 | 24 | @Column({ 25 | nullable: false, 26 | default: '', 27 | }) 28 | password: string; 29 | } 30 | -------------------------------------------------------------------------------- /src/users/controllers/users/users.controller.spec.ts: -------------------------------------------------------------------------------- 1 | import { Test, TestingModule } from '@nestjs/testing'; 2 | import { UsersController } from './users.controller'; 3 | 4 | describe('UsersController', () => { 5 | let controller: UsersController; 6 | 7 | beforeEach(async () => { 8 | const module: TestingModule = await Test.createTestingModule({ 9 | controllers: [UsersController], 10 | }).compile(); 11 | 12 | controller = module.get(UsersController); 13 | }); 14 | 15 | it('should be defined', () => { 16 | expect(controller).toBeDefined(); 17 | }); 18 | }); 19 | -------------------------------------------------------------------------------- /src/customers/services/customers/customers.service.spec.ts: -------------------------------------------------------------------------------- 1 | import { Test, TestingModule } from '@nestjs/testing'; 2 | import { CustomersService } from './customers.service'; 3 | 4 | describe('CustomersService', () => { 5 | let service: CustomersService; 6 | 7 | beforeEach(async () => { 8 | const module: TestingModule = await Test.createTestingModule({ 9 | providers: [CustomersService], 10 | }).compile(); 11 | 12 | service = module.get(CustomersService); 13 | }); 14 | 15 | it('should be defined', () => { 16 | expect(service).toBeDefined(); 17 | }); 18 | }); 19 | -------------------------------------------------------------------------------- /src/main.ts: -------------------------------------------------------------------------------- 1 | import { NestFactory } from '@nestjs/core'; 2 | import { AppModule } from './app.module'; 3 | import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger'; 4 | 5 | async function bootstrap() { 6 | const app = await NestFactory.create(AppModule); 7 | 8 | const config = new DocumentBuilder() 9 | .setTitle('Nestjs with Postgres') 10 | .setVersion('1.0') 11 | .addTag('nest-postgres') 12 | .build(); 13 | 14 | const document = SwaggerModule.createDocument(app, config); 15 | SwaggerModule.setup('api', app, document); 16 | 17 | await app.listen(3000); 18 | } 19 | bootstrap(); 20 | -------------------------------------------------------------------------------- /src/customers/controllers/customers/customers.controller.spec.ts: -------------------------------------------------------------------------------- 1 | import { Test, TestingModule } from '@nestjs/testing'; 2 | import { CustomersController } from './customers.controller'; 3 | 4 | describe('CustomersController', () => { 5 | let controller: CustomersController; 6 | 7 | beforeEach(async () => { 8 | const module: TestingModule = await Test.createTestingModule({ 9 | controllers: [CustomersController], 10 | }).compile(); 11 | 12 | controller = module.get(CustomersController); 13 | }); 14 | 15 | it('should be defined', () => { 16 | expect(controller).toBeDefined(); 17 | }); 18 | }); 19 | -------------------------------------------------------------------------------- /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 | "skipLibCheck": true, 15 | "strictNullChecks": false, 16 | "noImplicitAny": false, 17 | "strictBindCallApply": false, 18 | "forceConsistentCasingInFileNames": false, 19 | "noFallthroughCasesInSwitch": false 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /.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/recommended', 10 | 'plugin:prettier/recommended', 11 | ], 12 | root: true, 13 | env: { 14 | node: true, 15 | jest: true, 16 | }, 17 | ignorePatterns: ['.eslintrc.js'], 18 | rules: { 19 | '@typescript-eslint/interface-name-prefix': 'off', 20 | '@typescript-eslint/explicit-function-return-type': 'off', 21 | '@typescript-eslint/explicit-module-boundary-types': 'off', 22 | '@typescript-eslint/no-explicit-any': 'off', 23 | }, 24 | }; 25 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /src/users/services/users/users.service.ts: -------------------------------------------------------------------------------- 1 | import { Injectable } from '@nestjs/common'; 2 | import { InjectRepository } from '@nestjs/typeorm'; 3 | import { User } from 'src/typeorm'; 4 | import { Repository } from 'typeorm'; 5 | import { CreateUserDto } from 'src/users/dtos/CreateUser.dto'; 6 | 7 | @Injectable() 8 | export class UsersService { 9 | constructor( 10 | @InjectRepository(User) private readonly userRepository: Repository, 11 | ) {} 12 | 13 | createUser(createUserDto: CreateUserDto) { 14 | const newUser = this.userRepository.create(createUserDto); 15 | return this.userRepository.save(newUser); 16 | } 17 | 18 | getUsers() { 19 | return this.userRepository.find(); 20 | } 21 | 22 | findUsersById(id: number) { 23 | return this.userRepository.findOne(id); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/customers/services/customers/customers.service.ts: -------------------------------------------------------------------------------- 1 | import { Injectable } from '@nestjs/common'; 2 | import { InjectRepository } from '@nestjs/typeorm'; 3 | import { Customer } from 'src/typeorm'; 4 | import { Repository } from 'typeorm'; 5 | import { CreateCustomerDto } from 'src/customers/dtos/CreateCustomer.dto'; 6 | 7 | @Injectable() 8 | export class CustomersService { 9 | constructor( 10 | @InjectRepository(Customer) 11 | private readonly customerRepository: Repository, 12 | ) {} 13 | 14 | createCustomers(createCustomerDto: CreateCustomerDto) { 15 | const newUser = this.customerRepository.create(createCustomerDto); 16 | return this.customerRepository.save(newUser); 17 | } 18 | 19 | getCustomers() { 20 | return this.customerRepository.find(); 21 | } 22 | 23 | findCustomersById(id: number) { 24 | return this.customerRepository.findOne(id); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/users/controllers/users/users.controller.ts: -------------------------------------------------------------------------------- 1 | import { 2 | Body, 3 | Controller, 4 | Get, 5 | Param, 6 | ParseIntPipe, 7 | Post, 8 | UsePipes, 9 | ValidationPipe, 10 | } from '@nestjs/common'; 11 | import { CreateUserDto } from 'src/users/dtos/CreateUser.dto'; 12 | import { UsersService } from 'src/users/services/users/users.service'; 13 | 14 | @Controller('users') 15 | export class UsersController { 16 | constructor(private readonly userService: UsersService) {} 17 | 18 | @Get() 19 | getUsers() { 20 | return this.userService.getUsers(); 21 | } 22 | 23 | @Get('id/:id') 24 | findUsersById(@Param('id', ParseIntPipe) id: number) { 25 | return this.userService.findUsersById(id); 26 | } 27 | 28 | @Post('create') 29 | @UsePipes(ValidationPipe) 30 | createUsers(@Body() createUserDto: CreateUserDto) { 31 | return this.userService.createUser(createUserDto); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/customers/controllers/customers/customers.controller.ts: -------------------------------------------------------------------------------- 1 | import { 2 | Body, 3 | Controller, 4 | Get, 5 | Param, 6 | ParseIntPipe, 7 | Post, 8 | UsePipes, 9 | ValidationPipe, 10 | } from '@nestjs/common'; 11 | import { CreateCustomerDto } from 'src/customers/dtos/CreateCustomer.dto'; 12 | import { CustomersService } from 'src/customers/services/customers/customers.service'; 13 | 14 | @Controller('customers') 15 | export class CustomersController { 16 | constructor(private readonly customerService: CustomersService) {} 17 | @Get() 18 | getCustomers() { 19 | return this.customerService.getCustomers(); 20 | } 21 | 22 | @Get('id/:id') 23 | findCustomersById(@Param('id', ParseIntPipe) id: number) { 24 | return this.customerService.findCustomersById(id); 25 | } 26 | 27 | @Post('create') 28 | @UsePipes(ValidationPipe) 29 | createCustomers(@Body() createCustomerDto: CreateCustomerDto) { 30 | return this.customerService.createCustomers(createCustomerDto); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/app.module.ts: -------------------------------------------------------------------------------- 1 | import { Module } from '@nestjs/common'; 2 | import { UsersModule } from './users/users.module'; 3 | import { CustomersModule } from './customers/customers.module'; 4 | import { ConfigModule, ConfigService } from '@nestjs/config'; 5 | import { TypeOrmModule } from '@nestjs/typeorm'; 6 | import entities from './typeorm'; 7 | 8 | @Module({ 9 | imports: [ 10 | ConfigModule.forRoot({ isGlobal: true }), 11 | TypeOrmModule.forRootAsync({ 12 | imports: [ConfigModule], 13 | useFactory: (configService: ConfigService) => ({ 14 | type: 'postgres', 15 | host: configService.get('DB_HOST'), 16 | port: +configService.get('DB_PORT'), 17 | username: configService.get('DB_USERNAME'), 18 | password: configService.get('DB_PASSWORD'), 19 | database: configService.get('DB_NAME'), 20 | entities: entities, 21 | synchronize: true, 22 | }), 23 | inject: [ConfigService], 24 | }), 25 | UsersModule, 26 | CustomersModule, 27 | ], 28 | controllers: [], 29 | providers: [], 30 | }) 31 | export class AppModule {} 32 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nest-medium", 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": "^8.0.0", 25 | "@nestjs/config": "^1.2.0", 26 | "@nestjs/core": "^8.0.0", 27 | "@nestjs/platform-express": "^8.0.0", 28 | "@nestjs/swagger": "^5.2.0", 29 | "@nestjs/typeorm": "^8.0.3", 30 | "class-transformer": "^0.5.1", 31 | "class-validator": "^0.13.2", 32 | "dotenv": "^16.0.0", 33 | "pg": "^8.7.3", 34 | "postgres": "^1.0.2", 35 | "reflect-metadata": "^0.1.13", 36 | "rimraf": "^3.0.2", 37 | "rxjs": "^7.2.0", 38 | "swagger-ui-express": "^4.3.0", 39 | "typeorm": "^0.2.45" 40 | }, 41 | "devDependencies": { 42 | "@nestjs/cli": "^8.0.0", 43 | "@nestjs/schematics": "^8.0.0", 44 | "@nestjs/testing": "^8.0.0", 45 | "@types/express": "^4.17.13", 46 | "@types/jest": "27.4.1", 47 | "@types/node": "^16.0.0", 48 | "@types/supertest": "^2.0.11", 49 | "@typescript-eslint/eslint-plugin": "^5.0.0", 50 | "@typescript-eslint/parser": "^5.0.0", 51 | "eslint": "^8.0.1", 52 | "eslint-config-prettier": "^8.3.0", 53 | "eslint-plugin-prettier": "^4.0.0", 54 | "jest": "^27.2.5", 55 | "prettier": "^2.3.2", 56 | "source-map-support": "^0.5.20", 57 | "supertest": "^6.1.3", 58 | "ts-jest": "^27.0.3", 59 | "ts-loader": "^9.2.3", 60 | "ts-node": "^10.0.0", 61 | "tsconfig-paths": "^3.10.1", 62 | "typescript": "^4.3.5" 63 | }, 64 | "jest": { 65 | "moduleFileExtensions": [ 66 | "js", 67 | "json", 68 | "ts" 69 | ], 70 | "rootDir": "src", 71 | "testRegex": ".*\\.spec\\.ts$", 72 | "transform": { 73 | "^.+\\.(t|j)s$": "ts-jest" 74 | }, 75 | "collectCoverageFrom": [ 76 | "**/*.(t|j)s" 77 | ], 78 | "coverageDirectory": "../coverage", 79 | "testEnvironment": "node" 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | Nest Logo 3 |

4 | 5 | [circleci-image]: https://img.shields.io/circleci/build/github/nestjs/nest/master?token=abc123def456 6 | [circleci-url]: https://circleci.com/gh/nestjs/nest 7 | 8 |

A progressive Node.js framework for building efficient and scalable server-side applications.

9 |

10 | NPM Version 11 | Package License 12 | NPM Downloads 13 | CircleCI 14 | Coverage 15 | Discord 16 | Backers on Open Collective 17 | Sponsors on Open Collective 18 | 19 | Support us 20 | 21 |

22 | 24 | 25 | ## Description 26 | 27 | [Nest](https://github.com/nestjs/nest) framework TypeScript starter repository. 28 | 29 | ## Installation 30 | 31 | ```bash 32 | $ npm install 33 | ``` 34 | 35 | ## Running the app 36 | 37 | ```bash 38 | # development 39 | $ npm run start 40 | 41 | # watch mode 42 | $ npm run start:dev 43 | 44 | # production mode 45 | $ npm run start:prod 46 | ``` 47 | 48 | ## Test 49 | 50 | ```bash 51 | # unit tests 52 | $ npm run test 53 | 54 | # e2e tests 55 | $ npm run test:e2e 56 | 57 | # test coverage 58 | $ npm run test:cov 59 | ``` 60 | 61 | ## License 62 | 63 | Nest is [MIT licensed](LICENSE). 64 | --------------------------------------------------------------------------------