├── .gitignore ├── README.md ├── index.js ├── nodemon.json ├── package.json ├── src ├── modules │ ├── app.module.ts │ ├── contacts │ │ ├── contacts.controller.ts │ │ ├── contacts.module.ts │ │ ├── contacts.providers.ts │ │ ├── contacts.service.ts │ │ ├── dto │ │ │ └── create-contact.dto.ts │ │ ├── interfaces │ │ │ └── contact.interface.ts │ │ └── schemas │ │ │ └── contact.schema.ts │ └── database │ │ ├── database.module.ts │ │ └── database.providers.ts └── server.ts ├── tsconfig.json └── tslint.json /.gitignore: -------------------------------------------------------------------------------- 1 | # dependencies 2 | /node_modules 3 | 4 | # IDE 5 | /.idea 6 | /.awcache 7 | /.vscode 8 | 9 | # misc 10 | npm-debug.log 11 | 12 | # example 13 | /quick-start 14 | 15 | # tests 16 | /test 17 | /coverage 18 | /.nyc_output 19 | 20 | # dist 21 | /dist -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | Nest Logo 3 |

4 | 5 | [travis-image]: https://api.travis-ci.org/nestjs/nest.svg?branch=master 6 | [travis-url]: https://travis-ci.org/nestjs/nest 7 | [linux-image]: https://img.shields.io/travis/nestjs/nest/master.svg?label=linux 8 | [linux-url]: https://travis-ci.org/nestjs/nest 9 | 10 |

A progressive Node.js framework for building efficient and scalable web applications.

11 |

12 | NPM Version 13 | Package License 14 | NPM Downloads 15 | Travis 16 | Linux 17 | Coverage 18 | Gitter 19 | Backers on Open Collective 20 | Sponsors on Open Collective 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 | ## Start 36 | 37 | ``` 38 | $ npm run start 39 | ``` 40 | 41 | ## People 42 | 43 | - Author - [Kamil Myśliwiec](https://kamilmysliwiec.com) 44 | - Website - [https://nestjs.com](https://nestjs.com/) 45 | 46 | ## Backers 47 | 48 | I am on a mission to provide an architecture to create truly flexible, scalable and loosely coupled systems using the Node.js platform. It takes a lot of time, so if you want to support me, please [become a backer / sponsor]((https://opencollective.com/nest#backer)). I appreciate your help. Thanks! :heart_eyes: 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | require('ts-node/register'); 2 | require('./src/server'); 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /nodemon.json: -------------------------------------------------------------------------------- 1 | { 2 | "watch": ["src"], 3 | "ext": "ts", 4 | "ignore": ["src/**/*.spec.ts"], 5 | "exec": "node ./index" 6 | } 7 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nest-typescript-starter", 3 | "version": "1.0.0", 4 | "description": "Nest TypeScript starter repository", 5 | "license": "MIT", 6 | "scripts": { 7 | "start": "node index.js", 8 | "start:watch": "nodemon", 9 | "prestart:prod": "tsc", 10 | "start:prod": "node dist/server.js" 11 | }, 12 | "dependencies": { 13 | "@nestjs/common": "^4.0.0", 14 | "@nestjs/core": "^4.0.0", 15 | "@nestjs/microservices": "^4.0.0", 16 | "@nestjs/testing": "^4.0.0", 17 | "@nestjs/websockets": "^4.0.0", 18 | "mongoose": "^4.13.4", 19 | "redis": "^2.7.1", 20 | "reflect-metadata": "^0.1.10", 21 | "rxjs": "^5.4.0", 22 | "typescript": "^2.5.2" 23 | }, 24 | "devDependencies": { 25 | "@types/mongoose": "^4.7.27", 26 | "@types/node": "^8.0.28", 27 | "nodemon": "^1.12.1", 28 | "ts-node": "^3.3.0" 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/modules/app.module.ts: -------------------------------------------------------------------------------- 1 | import { Module } from '@nestjs/common'; 2 | 3 | import { ContactsModule } from './contacts/contacts.module'; 4 | 5 | @Module({ 6 | modules: [ContactsModule], 7 | }) 8 | export class ApplicationModule {} -------------------------------------------------------------------------------- /src/modules/contacts/contacts.controller.ts: -------------------------------------------------------------------------------- 1 | import { Body, Controller, Get, Post } from '@nestjs/common'; 2 | 3 | import { ContactsService } from './contacts.service'; 4 | import { CreateContactDto } from './dto/create-contact.dto'; 5 | import { Contact } from './interfaces/contact.interface'; 6 | 7 | @Controller('contacts') 8 | export class ContactsController { 9 | constructor(private readonly contactsService: ContactsService) {} 10 | 11 | @Post() 12 | async create(@Body() createContactDto: CreateContactDto) { 13 | this.contactsService.create(createContactDto); 14 | } 15 | 16 | @Get() 17 | async findAll(): Promise { 18 | return this.contactsService.findAll(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/modules/contacts/contacts.module.ts: -------------------------------------------------------------------------------- 1 | import { Module } from '@nestjs/common'; 2 | 3 | import { DatabaseModule } from '../database/database.module'; 4 | import { ContactsController } from './contacts.controller'; 5 | import { contactsProviders } from './contacts.providers'; 6 | import { ContactsService } from './contacts.service'; 7 | 8 | @Module({ 9 | modules: [DatabaseModule], 10 | controllers: [ContactsController], 11 | components: [ContactsService, ...contactsProviders] 12 | }) 13 | export class ContactsModule {} 14 | -------------------------------------------------------------------------------- /src/modules/contacts/contacts.providers.ts: -------------------------------------------------------------------------------- 1 | import { Connection } from 'mongoose'; 2 | 3 | import { ContactSchema } from './schemas/contact.schema'; 4 | 5 | export const contactsProviders = [ 6 | { 7 | provide: 'ContactModelToken', 8 | useFactory: (connection: Connection) => 9 | connection.model('Contact', ContactSchema), 10 | inject: ['DbConnectionToken'] 11 | } 12 | ]; 13 | -------------------------------------------------------------------------------- /src/modules/contacts/contacts.service.ts: -------------------------------------------------------------------------------- 1 | import { Model } from 'mongoose'; 2 | import { Component, Inject } from '@nestjs/common'; 3 | import { Contact } from './interfaces/contact.interface'; 4 | import { CreateContactDto } from './dto/create-contact.dto'; 5 | 6 | @Component() 7 | export class ContactsService { 8 | constructor( 9 | @Inject('ContactModelToken') private readonly contactModel: Model 10 | ) {} 11 | 12 | async create(createContactDto: CreateContactDto): Promise { 13 | const createdContact = new this.contactModel(createContactDto); 14 | return await createdContact.save(); 15 | } 16 | 17 | async findAll(): Promise { 18 | return await this.contactModel.find().exec(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/modules/contacts/dto/create-contact.dto.ts: -------------------------------------------------------------------------------- 1 | export class CreateContactDto { 2 | readonly name: string; 3 | readonly email: string; 4 | readonly phone: string; 5 | } 6 | -------------------------------------------------------------------------------- /src/modules/contacts/interfaces/contact.interface.ts: -------------------------------------------------------------------------------- 1 | import { Document } from 'mongoose'; 2 | 3 | export interface Contact extends Document { 4 | readonly name: string; 5 | readonly email: string; 6 | readonly phone: string; 7 | } 8 | -------------------------------------------------------------------------------- /src/modules/contacts/schemas/contact.schema.ts: -------------------------------------------------------------------------------- 1 | import * as mongoose from 'mongoose'; 2 | 3 | export const ContactSchema = new mongoose.Schema( 4 | { 5 | name: { type: String, require: true, unique: true }, 6 | email: String, 7 | phone: String 8 | }, 9 | { 10 | collection: 'contacts', 11 | read: 'nearest' 12 | } 13 | ); 14 | -------------------------------------------------------------------------------- /src/modules/database/database.module.ts: -------------------------------------------------------------------------------- 1 | import { Module } from '@nestjs/common'; 2 | import { databaseProviders } from './database.providers'; 3 | 4 | @Module({ 5 | components: [...databaseProviders], 6 | exports: [...databaseProviders] 7 | }) 8 | export class DatabaseModule {} 9 | -------------------------------------------------------------------------------- /src/modules/database/database.providers.ts: -------------------------------------------------------------------------------- 1 | import * as mongoose from 'mongoose'; 2 | 3 | export const databaseProviders = [ 4 | { 5 | provide: 'DbConnectionToken', 6 | useFactory: async (): Promise => { 7 | (mongoose as any).Promise = global.Promise; 8 | return await mongoose.connect('mongodb://localhost:27017/contacts', { 9 | useMongoClient: true 10 | }); 11 | } 12 | } 13 | ]; 14 | -------------------------------------------------------------------------------- /src/server.ts: -------------------------------------------------------------------------------- 1 | import { NestFactory } from '@nestjs/core'; 2 | 3 | import { ApplicationModule } from './modules/app.module'; 4 | 5 | async function bootstrap() { 6 | const app = await NestFactory.create(ApplicationModule); 7 | await app.listen(3000); 8 | } 9 | bootstrap(); 10 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "module": "commonjs", 4 | "declaration": false, 5 | "noImplicitAny": false, 6 | "removeComments": true, 7 | "noLib": false, 8 | "emitDecoratorMetadata": true, 9 | "experimentalDecorators": true, 10 | "target": "es6", 11 | "sourceMap": true, 12 | "allowJs": true, 13 | "outDir": "./dist" 14 | }, 15 | "include": [ 16 | "src/**/*" 17 | ], 18 | "exclude": [ 19 | "node_modules", 20 | "**/*.spec.ts" 21 | ] 22 | } -------------------------------------------------------------------------------- /tslint.json: -------------------------------------------------------------------------------- 1 | { 2 | "defaultSeverity": "error", 3 | "extends": [ 4 | "tslint:recommended" 5 | ], 6 | "jsRules": { 7 | "no-unused-expression": true 8 | }, 9 | "rules": { 10 | "eofline": false, 11 | "quotemark": [ 12 | true, 13 | "single" 14 | ], 15 | "member-access": [ 16 | false 17 | ], 18 | "ordered-imports": [ 19 | false 20 | ], 21 | "max-line-length": [ 22 | 150 23 | ], 24 | "member-ordering": [ 25 | false 26 | ], 27 | "curly": false, 28 | "interface-name": [ 29 | false 30 | ], 31 | "array-type": [ 32 | false 33 | ], 34 | "no-empty-interface": false, 35 | "no-empty": false, 36 | "arrow-parens": false, 37 | "object-literal-sort-keys": false, 38 | "no-unused-expression": false, 39 | "max-classes-per-file": [ 40 | false 41 | ], 42 | "variable-name": [ 43 | false 44 | ], 45 | "one-line": [ 46 | false 47 | ], 48 | "one-variable-per-declaration": [ 49 | false 50 | ], 51 | "trailing-comma": false 52 | }, 53 | "rulesDirectory": [] 54 | } --------------------------------------------------------------------------------