├── .gitignore ├── .prettierrc ├── README.md ├── nest-cli.json ├── nodemon-debug.json ├── nodemon.json ├── package.json ├── schema.gql ├── src ├── app.controller.spec.ts ├── app.controller.ts ├── app.module.ts ├── app.service.ts ├── cats │ ├── cats.module.ts │ ├── cats.resolver.ts │ ├── cats.schema.ts │ ├── cats.service.ts │ ├── dto │ │ └── create-cat.dto.ts │ ├── inputs │ │ └── cat.input.ts │ └── interfaces │ │ └── cat.interface.ts └── main.ts ├── test ├── app.e2e-spec.ts └── jest-e2e.json ├── tsconfig.build.json ├── tsconfig.json ├── tslint.json └── yarn.lock /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "singleQuote": true, 3 | "trailingComma": "all" 4 | } -------------------------------------------------------------------------------- /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 server-side applications, heavily inspired by Angular.

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 | 23 |

24 | 26 | 27 | ## Description 28 | 29 | [Nest](https://github.com/nestjs/nest) framework TypeScript starter repository. 30 | 31 | ## Installation 32 | 33 | ```bash 34 | $ npm install 35 | ``` 36 | 37 | ## Running the app 38 | 39 | ```bash 40 | # development 41 | $ npm run start 42 | 43 | # watch mode 44 | $ npm run start:dev 45 | 46 | # production mode 47 | $ npm run start:prod 48 | ``` 49 | 50 | ## Test 51 | 52 | ```bash 53 | # unit tests 54 | $ npm run test 55 | 56 | # e2e tests 57 | $ npm run test:e2e 58 | 59 | # test coverage 60 | $ npm run test:cov 61 | ``` 62 | 63 | ## Support 64 | 65 | Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please [read more here](https://docs.nestjs.com/support). 66 | 67 | ## Stay in touch 68 | 69 | - Author - [Kamil Myśliwiec](https://kamilmysliwiec.com) 70 | - Website - [https://nestjs.com](https://nestjs.com/) 71 | - Twitter - [@nestframework](https://twitter.com/nestframework) 72 | 73 | ## License 74 | 75 | Nest is [MIT licensed](LICENSE). 76 | -------------------------------------------------------------------------------- /nest-cli.json: -------------------------------------------------------------------------------- 1 | { 2 | "language": "ts", 3 | "collection": "@nestjs/schematics", 4 | "sourceRoot": "src" 5 | } 6 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nest-mongo-graphql", 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/graphql": "^6.0.5", 26 | "@nestjs/mongoose": "^6.0.0", 27 | "@nestjs/platform-express": "^6.0.0", 28 | "apollo-server-express": "^2.4.8", 29 | "graphql": "^14.2.1", 30 | "graphql-tools": "^4.0.4", 31 | "mongoose": "^5.5.1", 32 | "reflect-metadata": "^0.1.12", 33 | "rimraf": "^2.6.2", 34 | "rxjs": "^6.3.3", 35 | "type-graphql": "^0.17.1" 36 | }, 37 | "devDependencies": { 38 | "@nestjs/testing": "^6.0.0", 39 | "@types/express": "^4.16.0", 40 | "@types/jest": "^23.3.13", 41 | "@types/node": "^10.12.18", 42 | "@types/supertest": "^2.0.7", 43 | "jest": "^23.6.0", 44 | "nodemon": "^1.18.9", 45 | "prettier": "^1.15.3", 46 | "supertest": "^3.4.1", 47 | "ts-jest": "^23.10.5", 48 | "ts-node": "^7.0.1", 49 | "tsconfig-paths": "^3.7.0", 50 | "tslint": "5.12.1", 51 | "typescript": "^3.2.4" 52 | }, 53 | "jest": { 54 | "moduleFileExtensions": [ 55 | "js", 56 | "json", 57 | "ts" 58 | ], 59 | "rootDir": "src", 60 | "testRegex": ".spec.ts$", 61 | "transform": { 62 | "^.+\\.(t|j)s$": "ts-jest" 63 | }, 64 | "coverageDirectory": "../coverage", 65 | "testEnvironment": "node" 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /schema.gql: -------------------------------------------------------------------------------- 1 | # ----------------------------------------------- 2 | # !!! THIS FILE WAS GENERATED BY TYPE-GRAPHQL !!! 3 | # !!! DO NOT MODIFY THIS FILE BY YOURSELF !!! 4 | # ----------------------------------------------- 5 | 6 | input CatInput { 7 | name: String! 8 | age: Int! 9 | breed: String! 10 | } 11 | 12 | type CatType { 13 | id: ID! 14 | name: String! 15 | age: Int! 16 | breed: String! 17 | } 18 | 19 | type Mutation { 20 | createCat(input: CatInput!): CatType! 21 | } 22 | 23 | type Query { 24 | hello: String! 25 | cats: [CatType!]! 26 | } 27 | -------------------------------------------------------------------------------- /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 { GraphQLModule } from '@nestjs/graphql'; 3 | import { MongooseModule } from '@nestjs/mongoose'; 4 | import { AppController } from './app.controller'; 5 | import { AppService } from './app.service'; 6 | import { CatsModule } from './cats/cats.module'; 7 | 8 | @Module({ 9 | imports: [ 10 | CatsModule, 11 | GraphQLModule.forRoot({ 12 | autoSchemaFile: 'schema.gql', 13 | }), 14 | MongooseModule.forRoot('mongodb://localhost/nest'), 15 | ], 16 | controllers: [AppController], 17 | providers: [AppService], 18 | }) 19 | export class AppModule {} 20 | -------------------------------------------------------------------------------- /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/cats/cats.module.ts: -------------------------------------------------------------------------------- 1 | import { Module } from '@nestjs/common'; 2 | import { CatsResolver } from './cats.resolver'; 3 | import { MongooseModule } from '@nestjs/mongoose'; 4 | import { CatSchema } from './cats.schema'; 5 | import { CatsService } from './cats.service'; 6 | 7 | @Module({ 8 | imports: [MongooseModule.forFeature([{ name: 'Cat', schema: CatSchema }])], 9 | providers: [CatsResolver, CatsService], 10 | }) 11 | export class CatsModule {} 12 | -------------------------------------------------------------------------------- /src/cats/cats.resolver.ts: -------------------------------------------------------------------------------- 1 | import { Resolver, Query, Mutation, Args } from '@nestjs/graphql'; 2 | import { CatsService } from './cats.service'; 3 | import { CatType } from './dto/create-cat.dto'; 4 | import { CatInput } from './inputs/cat.input'; 5 | 6 | @Resolver() 7 | export class CatsResolver { 8 | constructor(private readonly catsService: CatsService) {} 9 | 10 | @Query(() => String) 11 | async hello() { 12 | return 'hello'; 13 | } 14 | 15 | @Query(() => [CatType]) 16 | async cats() { 17 | return this.catsService.findAll(); 18 | } 19 | 20 | @Mutation(() => CatType) 21 | async createCat(@Args('input') input: CatInput) { 22 | return this.catsService.create(input); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/cats/cats.schema.ts: -------------------------------------------------------------------------------- 1 | import * as mongoose from 'mongoose'; 2 | 3 | export const CatSchema = new mongoose.Schema({ 4 | name: String, 5 | age: Number, 6 | breed: String, 7 | }); 8 | -------------------------------------------------------------------------------- /src/cats/cats.service.ts: -------------------------------------------------------------------------------- 1 | import { Model } from 'mongoose'; 2 | import { Injectable } from '@nestjs/common'; 3 | import { InjectModel } from '@nestjs/mongoose'; 4 | import { Cat } from './interfaces/cat.interface'; 5 | import { CatInput } from './inputs/cat.input'; 6 | 7 | @Injectable() 8 | export class CatsService { 9 | constructor(@InjectModel('Cat') private readonly catModel: Model) {} 10 | 11 | async create(createCatDto: CatInput): Promise { 12 | const createdCat = new this.catModel(createCatDto); 13 | return await createdCat.save(); 14 | } 15 | 16 | async findAll(): Promise { 17 | return await this.catModel.find().exec(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/cats/dto/create-cat.dto.ts: -------------------------------------------------------------------------------- 1 | import { ObjectType, Field, Int, ID } from 'type-graphql'; 2 | 3 | @ObjectType() 4 | export class CatType { 5 | @Field(() => ID) 6 | id: string; 7 | @Field() 8 | readonly name: string; 9 | @Field(() => Int) 10 | readonly age: number; 11 | @Field() 12 | readonly breed: string; 13 | } 14 | -------------------------------------------------------------------------------- /src/cats/inputs/cat.input.ts: -------------------------------------------------------------------------------- 1 | import { InputType, Field, Int } from 'type-graphql'; 2 | 3 | @InputType() 4 | export class CatInput { 5 | @Field() 6 | readonly name: string; 7 | @Field(() => Int) 8 | readonly age: number; 9 | @Field() 10 | readonly breed: string; 11 | } 12 | -------------------------------------------------------------------------------- /src/cats/interfaces/cat.interface.ts: -------------------------------------------------------------------------------- 1 | import { Document } from 'mongoose'; 2 | 3 | export interface Cat extends Document { 4 | readonly name: string; 5 | readonly age: number; 6 | readonly breed: string; 7 | } 8 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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", "**/*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 | "target": "es6", 9 | "sourceMap": true, 10 | "outDir": "./dist", 11 | "baseUrl": "./" 12 | }, 13 | "exclude": ["node_modules"] 14 | } 15 | -------------------------------------------------------------------------------- /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 | --------------------------------------------------------------------------------