├── .gitignore ├── .prettierrc ├── src ├── config │ └── keys.ts ├── items │ ├── interfaces │ │ └── item.interface.ts │ ├── dto │ │ └── create-item.dto.ts │ ├── schemas │ │ └── item.schema.ts │ ├── items.module.ts │ ├── items.service.spec.ts │ ├── items.controller.spec.ts │ ├── items.service.ts │ └── items.controller.ts ├── app.service.ts ├── main.ts ├── app.controller.ts ├── app.module.ts └── app.controller.spec.ts ├── nest-cli.json ├── tsconfig.build.json ├── nodemon.json ├── nodemon-debug.json ├── test ├── jest-e2e.json └── app.e2e-spec.ts ├── tsconfig.json ├── README.md ├── tslint.json └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "singleQuote": true, 3 | "trailingComma": "all" 4 | } -------------------------------------------------------------------------------- /src/config/keys.ts: -------------------------------------------------------------------------------- 1 | export default { 2 | mongoURI: 'YOUR_MONGO_URI', 3 | }; 4 | -------------------------------------------------------------------------------- /nest-cli.json: -------------------------------------------------------------------------------- 1 | { 2 | "language": "ts", 3 | "collection": "@nestjs/schematics", 4 | "sourceRoot": "src" 5 | } 6 | -------------------------------------------------------------------------------- /tsconfig.build.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.json", 3 | "exclude": ["node_modules", "test", "**/*spec.ts"] 4 | } 5 | -------------------------------------------------------------------------------- /src/items/interfaces/item.interface.ts: -------------------------------------------------------------------------------- 1 | export interface Item { 2 | id?: string; 3 | name: string; 4 | description?: string; 5 | qty: number; 6 | } 7 | -------------------------------------------------------------------------------- /nodemon.json: -------------------------------------------------------------------------------- 1 | { 2 | "watch": ["src"], 3 | "ext": "ts", 4 | "ignore": ["src/**/*.spec.ts"], 5 | "exec": "ts-node -r tsconfig-paths/register src/main.ts" 6 | } 7 | -------------------------------------------------------------------------------- /src/items/dto/create-item.dto.ts: -------------------------------------------------------------------------------- 1 | export class CreateItemDto { 2 | readonly name: string; 3 | readonly description: string; 4 | readonly qty: number; 5 | } 6 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /nodemon-debug.json: -------------------------------------------------------------------------------- 1 | { 2 | "watch": ["src"], 3 | "ext": "ts", 4 | "ignore": ["src/**/*.spec.ts"], 5 | "exec": "node --inspect-brk -r ts-node/register -r tsconfig-paths/register src/main.ts" 6 | } 7 | -------------------------------------------------------------------------------- /src/items/schemas/item.schema.ts: -------------------------------------------------------------------------------- 1 | import * as mongoose from 'mongoose'; 2 | 3 | export const ItemSchema = new mongoose.Schema({ 4 | name: String, 5 | qty: Number, 6 | description: String, 7 | }); 8 | -------------------------------------------------------------------------------- /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/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/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 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "module": "commonjs", 4 | "declaration": true, 5 | "removeComments": true, 6 | "emitDecoratorMetadata": true, 7 | "experimentalDecorators": true, 8 | "target": "es6", 9 | "sourceMap": true, 10 | "outDir": "./dist", 11 | "baseUrl": "./" 12 | }, 13 | "exclude": ["node_modules"] 14 | } 15 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # NestJS REST API 2 | 3 | A CRUD REST API using the NestJS framework and MongoDB/Mongoose. 4 | 5 | ## Setup 6 | 7 | Add your mongodb uri to the "config/keys.ts file" 8 | 9 | ## Installation 10 | 11 | ```bash 12 | $ npm install 13 | ``` 14 | 15 | ## Running the app 16 | 17 | ```bash 18 | # development 19 | $ npm run start 20 | 21 | # watch mode 22 | $ npm run start:dev 23 | 24 | # production mode 25 | $ npm run start:prod 26 | ``` 27 | -------------------------------------------------------------------------------- /src/items/items.module.ts: -------------------------------------------------------------------------------- 1 | import { Module } from '@nestjs/common'; 2 | import { MongooseModule } from '@nestjs/mongoose'; 3 | import { ItemsController } from './items.controller'; 4 | import { ItemsService } from './items.service'; 5 | import { ItemSchema } from './schemas/item.schema'; 6 | 7 | @Module({ 8 | imports: [MongooseModule.forFeature([{ name: 'Item', schema: ItemSchema }])], 9 | controllers: [ItemsController], 10 | providers: [ItemsService], 11 | }) 12 | export class ItemsModule {} 13 | -------------------------------------------------------------------------------- /tslint.json: -------------------------------------------------------------------------------- 1 | { 2 | "defaultSeverity": "error", 3 | "extends": ["tslint:recommended"], 4 | "jsRules": { 5 | "no-unused-expression": true 6 | }, 7 | "rules": { 8 | "quotemark": [true, "single"], 9 | "member-access": [false], 10 | "ordered-imports": [false], 11 | "max-line-length": [true, 150], 12 | "member-ordering": [false], 13 | "interface-name": [false], 14 | "arrow-parens": false, 15 | "object-literal-sort-keys": false 16 | }, 17 | "rulesDirectory": [] 18 | } 19 | -------------------------------------------------------------------------------- /src/items/items.service.spec.ts: -------------------------------------------------------------------------------- 1 | import { Test, TestingModule } from '@nestjs/testing'; 2 | import { ItemsService } from './items.service'; 3 | 4 | describe('ItemsService', () => { 5 | let service: ItemsService; 6 | 7 | beforeEach(async () => { 8 | const module: TestingModule = await Test.createTestingModule({ 9 | providers: [ItemsService], 10 | }).compile(); 11 | 12 | service = module.get(ItemsService); 13 | }); 14 | 15 | it('should be defined', () => { 16 | expect(service).toBeDefined(); 17 | }); 18 | }); 19 | -------------------------------------------------------------------------------- /src/items/items.controller.spec.ts: -------------------------------------------------------------------------------- 1 | import { Test, TestingModule } from '@nestjs/testing'; 2 | import { ItemsController } from './items.controller'; 3 | 4 | describe('Items Controller', () => { 5 | let controller: ItemsController; 6 | 7 | beforeEach(async () => { 8 | const module: TestingModule = await Test.createTestingModule({ 9 | controllers: [ItemsController], 10 | }).compile(); 11 | 12 | controller = module.get(ItemsController); 13 | }); 14 | 15 | it('should be defined', () => { 16 | expect(controller).toBeDefined(); 17 | }); 18 | }); 19 | -------------------------------------------------------------------------------- /src/app.module.ts: -------------------------------------------------------------------------------- 1 | import { Module } from '@nestjs/common'; 2 | import { AppController } from './app.controller'; 3 | import { AppService } from './app.service'; 4 | import { ItemsController } from './items/items.controller'; 5 | import { ItemsService } from './items/items.service'; 6 | import { ItemsModule } from './items/items.module'; 7 | import { MongooseModule } from '@nestjs/mongoose'; 8 | import config from './config/keys'; 9 | 10 | @Module({ 11 | imports: [ItemsModule, MongooseModule.forRoot(config.mongoURI)], 12 | controllers: [AppController, ItemsController], 13 | providers: [AppService, ItemsService], 14 | }) 15 | export class AppModule {} 16 | -------------------------------------------------------------------------------- /test/app.e2e-spec.ts: -------------------------------------------------------------------------------- 1 | import { Test, TestingModule } from '@nestjs/testing'; 2 | import * as request from 'supertest'; 3 | import { AppModule } from './../src/app.module'; 4 | 5 | describe('AppController (e2e)', () => { 6 | let app; 7 | 8 | beforeEach(async () => { 9 | const moduleFixture: TestingModule = await Test.createTestingModule({ 10 | imports: [AppModule], 11 | }).compile(); 12 | 13 | app = moduleFixture.createNestApplication(); 14 | await app.init(); 15 | }); 16 | 17 | it('/ (GET)', () => { 18 | return request(app.getHttpServer()) 19 | .get('/') 20 | .expect(200) 21 | .expect('Hello World!'); 22 | }); 23 | }); 24 | -------------------------------------------------------------------------------- /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/items/items.service.ts: -------------------------------------------------------------------------------- 1 | import { Injectable } from '@nestjs/common'; 2 | import { Item } from './interfaces/item.interface'; 3 | import { Model } from 'mongoose'; 4 | import { InjectModel } from '@nestjs/mongoose'; 5 | 6 | @Injectable() 7 | export class ItemsService { 8 | constructor(@InjectModel('Item') private readonly itemModel: Model) {} 9 | 10 | async findAll(): Promise { 11 | return await this.itemModel.find(); 12 | } 13 | 14 | async findOne(id: string): Promise { 15 | return await this.itemModel.findOne({ _id: id }); 16 | } 17 | 18 | async create(item: Item): Promise { 19 | const newItem = new this.itemModel(item); 20 | return await newItem.save(); 21 | } 22 | 23 | async delete(id: string): Promise { 24 | return await this.itemModel.findByIdAndRemove(id); 25 | } 26 | 27 | async update(id: string, item: Item): Promise { 28 | return await this.itemModel.findByIdAndUpdate(id, item, { new: true }); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/items/items.controller.ts: -------------------------------------------------------------------------------- 1 | import { 2 | Controller, 3 | Get, 4 | Post, 5 | Put, 6 | Delete, 7 | Body, 8 | Param, 9 | } from '@nestjs/common'; 10 | import { CreateItemDto } from './dto/create-item.dto'; 11 | import { ItemsService } from './items.service'; 12 | import { Item } from './interfaces/item.interface'; 13 | 14 | @Controller('items') 15 | export class ItemsController { 16 | constructor(private readonly itemsService: ItemsService) {} 17 | 18 | @Get() 19 | findAll(): Promise { 20 | return this.itemsService.findAll(); 21 | } 22 | 23 | @Get(':id') 24 | findOne(@Param('id') id): Promise { 25 | return this.itemsService.findOne(id); 26 | } 27 | 28 | @Post() 29 | create(@Body() createItemDto: CreateItemDto): Promise { 30 | return this.itemsService.create(createItemDto); 31 | } 32 | 33 | @Delete(':id') 34 | delete(@Param('id') id): Promise { 35 | return this.itemsService.delete(id); 36 | } 37 | 38 | @Put(':id') 39 | update(@Body() updateItemDto: CreateItemDto, @Param('id') id): Promise { 40 | return this.itemsService.update(id, updateItemDto); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nest-rest-api", 3 | "version": "0.0.1", 4 | "description": "", 5 | "author": "", 6 | "license": "MIT", 7 | "scripts": { 8 | "build": "tsc -p tsconfig.build.json", 9 | "format": "prettier --write \"src/**/*.ts\"", 10 | "start": "ts-node -r tsconfig-paths/register src/main.ts", 11 | "start:dev": "nodemon", 12 | "start:debug": "nodemon --config nodemon-debug.json", 13 | "prestart:prod": "rimraf dist && npm run build", 14 | "start:prod": "node dist/main.js", 15 | "lint": "tslint -p tsconfig.json -c tslint.json", 16 | "test": "jest", 17 | "test:watch": "jest --watch", 18 | "test:cov": "jest --coverage", 19 | "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand", 20 | "test:e2e": "jest --config ./test/jest-e2e.json" 21 | }, 22 | "dependencies": { 23 | "@nestjs/common": "^6.0.0", 24 | "@nestjs/core": "^6.0.0", 25 | "@nestjs/mongoose": "^6.0.0", 26 | "@nestjs/platform-express": "^6.0.0", 27 | "mongoose": "^5.4.19", 28 | "reflect-metadata": "^0.1.12", 29 | "rimraf": "^2.6.2", 30 | "rxjs": "^6.3.3" 31 | }, 32 | "devDependencies": { 33 | "@types/express": "^4.16.0", 34 | "@types/jest": "^23.3.13", 35 | "@types/node": "^10.12.18", 36 | "@types/supertest": "^2.0.7", 37 | "@nestjs/testing": "^6.0.0", 38 | "jest": "^23.6.0", 39 | "nodemon": "^1.18.9", 40 | "prettier": "^1.15.3", 41 | "supertest": "^3.4.1", 42 | "ts-jest": "^23.10.5", 43 | "ts-node": "^7.0.1", 44 | "tsconfig-paths": "^3.7.0", 45 | "tslint": "5.12.1", 46 | "typescript": "^3.2.4" 47 | }, 48 | "jest": { 49 | "moduleFileExtensions": [ 50 | "js", 51 | "json", 52 | "ts" 53 | ], 54 | "rootDir": "src", 55 | "testRegex": ".spec.ts$", 56 | "transform": { 57 | "^.+\\.(t|j)s$": "ts-jest" 58 | }, 59 | "coverageDirectory": "../coverage", 60 | "testEnvironment": "node" 61 | } 62 | } 63 | --------------------------------------------------------------------------------