├── .eslintrc.js ├── .gitignore ├── .prettierrc ├── README.md ├── nest-cli.json ├── package-lock.json ├── package.json ├── products.ts ├── src ├── app.controller.spec.ts ├── app.controller.ts ├── app.module.ts ├── app.service.ts └── main.ts ├── test ├── app.e2e-spec.ts └── jest-e2e.json ├── tsconfig.build.json └── tsconfig.json /.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 | -------------------------------------------------------------------------------- /.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 35 | 36 | # Prisma SQLite 37 | prisma/dev.db* -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "singleQuote": true, 3 | "trailingComma": "all" 4 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Building a REST API with NestJS and prisma 2 | 3 | This repository contains the starter project for the **Building a REST API with NestJS and Prisma** workshop by [Marc Stammerjohann](https://twitter.com/mrcjln). 4 | ## Getting Started 5 | 6 | 1. Clone this repository 7 | 8 | You can clone this repository with the following command: 9 | 10 | ```bash 11 | # SSH 12 | git clone git@github.com:marcjulian/nestjs-prisma-workshop.git 13 | 14 | # HTTPS 15 | git clone https://github.com/marcjulian/nestjs-prisma-workshop.git 16 | 17 | # GitHub CLI 18 | gh repo clone marcjulian/nestjs-prisma-workshop 19 | ``` 20 | 21 | 2. Install dependencies 22 | 23 | ```bash 24 | cd nestjs-prisma-workshop 25 | npm install 26 | ``` 27 | 28 | 3. Install NestJS CLI if you haven't already 29 | 30 | ```bash 31 | npm i -g @nestjs/cli 32 | ``` 33 | 34 | ## Workshop 35 | 36 | Use this repo as the starting point and follow the lessons for the [workshop](https://pris.ly/day2021-nestjs). 37 | The recorded version is available on Youtube [Building a REST API with NestJS and Prisma](https://www.youtube.com/watch?v=mmbd5hcQUaY). 38 | -------------------------------------------------------------------------------- /nest-cli.json: -------------------------------------------------------------------------------- 1 | { 2 | "collection": "@nestjs/schematics", 3 | "sourceRoot": "src" 4 | } 5 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nestjs-prisma-workshop", 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.6.17", 25 | "@nestjs/core": "^7.6.17", 26 | "@nestjs/platform-express": "^7.6.17", 27 | "@nestjs/swagger": "^4.8.0", 28 | "class-transformer": "^0.4.0", 29 | "class-validator": "^0.13.1", 30 | "reflect-metadata": "^0.1.13", 31 | "rimraf": "^3.0.2", 32 | "rxjs": "^6.6.6", 33 | "swagger-ui-express": "^4.1.6" 34 | }, 35 | "devDependencies": { 36 | "@nestjs/cli": "^7.6.0", 37 | "@nestjs/schematics": "^7.3.1", 38 | "@nestjs/testing": "^7.6.17", 39 | "@types/express": "^4.17.11", 40 | "@types/jest": "^26.0.22", 41 | "@types/node": "^14.14.36", 42 | "@types/supertest": "^2.0.10", 43 | "@typescript-eslint/eslint-plugin": "^4.19.0", 44 | "@typescript-eslint/parser": "^4.19.0", 45 | "eslint": "^7.22.0", 46 | "eslint-config-prettier": "^8.1.0", 47 | "eslint-plugin-prettier": "^3.3.1", 48 | "jest": "^26.6.3", 49 | "prettier": "^2.2.1", 50 | "supertest": "^6.1.3", 51 | "ts-jest": "^26.5.4", 52 | "ts-loader": "^8.0.18", 53 | "ts-node": "^9.1.1", 54 | "tsconfig-paths": "^3.9.0", 55 | "typescript": "^4.2.3" 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 | "collectCoverageFrom": [ 69 | "**/*.(t|j)s" 70 | ], 71 | "coverageDirectory": "../coverage", 72 | "testEnvironment": "node" 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /products.ts: -------------------------------------------------------------------------------- 1 | // database product seed 2 | export const products = [ 3 | { 4 | name: "SUP Board 12'", 5 | description: "Inflatable SUP Board 12' in RedOrange.", 6 | price: 549.99, 7 | sku: 'sku_supboard_12_red', 8 | published: true, 9 | }, 10 | { 11 | name: "SUP Board 14'", 12 | description: "Inflatable SUP Board 14' in all new Blue color.", 13 | price: 629.95, 14 | sku: 'sku_sup_board14_blue', 15 | published: true, 16 | }, 17 | { 18 | name: "SUP Board 10' Youth", 19 | description: 20 | 'Short and narrow touring board for children, perfect for fast paddling.', 21 | price: 550.95, 22 | sku: 'sku_sup_board10_youth', 23 | published: true, 24 | }, 25 | { 26 | name: "SUP Board 10'", 27 | description: 'New short and narrow touring board.', 28 | price: 570, 29 | sku: 'sku_sup_board10', 30 | published: false, 31 | }, 32 | { 33 | name: 'SUP Glass Paddle', 34 | price: 79.0, 35 | sku: 'sku_sup_paddle_glass', 36 | published: true, 37 | }, 38 | { 39 | name: 'SUP Carbon Paddle', 40 | description: 'Perfect suited for racing.', 41 | price: 159.49, 42 | sku: 'sku_sup_paddle_carbon', 43 | published: true, 44 | }, 45 | { 46 | name: 'SUP NEW Carbon Paddle', 47 | description: 'Lighter than ever.', 48 | price: 229.0, 49 | sku: 'sku_sup_paddle_new_carbon', 50 | published: false, 51 | }, 52 | { 53 | name: 'SUP Paddle Kids', 54 | description: 'Suitable for kids from 7 to 12 years.', 55 | price: 59.0, 56 | sku: 'sku_sup_paddle_kids', 57 | published: true, 58 | }, 59 | { 60 | name: 'Dry Bag 10L Red', 61 | description: '10 Liter dry bag keeping your valuables dry.', 62 | price: 17.99, 63 | sku: 'sku_dry_bag_10l_red', 64 | published: true, 65 | }, 66 | { 67 | name: 'Dry Bag 10L Black', 68 | description: '10 Liter dry bag keeping your valuables dry.', 69 | price: 15.99, 70 | sku: 'sku_dry_bag_10l_black', 71 | published: true, 72 | }, 73 | { 74 | name: 'Dry Bag 35L', 75 | description: '35 Liter dry bag keeping your valuables and clothes dry.', 76 | price: 23.99, 77 | sku: 'sku_dry_bag_35l', 78 | published: true, 79 | }, 80 | { 81 | name: 'Dry Bag 100L', 82 | description: '100 Liter dry bag perfect for long trips.', 83 | price: 36.99, 84 | sku: 'sku_dry_bag_100l', 85 | published: true, 86 | }, 87 | { 88 | name: 'Dry Bag 50L', 89 | description: 90 | '50 Liter dry bag extra room to carry valuables of your friends.', 91 | price: 30.99, 92 | sku: 'sku_dry_bag_50l', 93 | published: false, 94 | }, 95 | { 96 | name: 'Flex Touring Fin Blue', 97 | price: 26.9, 98 | sku: 'sku_fin_flex_touring_blue', 99 | published: true, 100 | }, 101 | { 102 | name: 'Touring Fin Red', 103 | price: 20.9, 104 | sku: 'sku_fin_touring_red', 105 | published: false, 106 | }, 107 | ]; 108 | -------------------------------------------------------------------------------- /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 | 5 | @Module({ 6 | imports: [], 7 | controllers: [AppController], 8 | providers: [AppService], 9 | }) 10 | export class AppModule {} 11 | -------------------------------------------------------------------------------- /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 { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'; 3 | import { AppModule } from './app.module'; 4 | 5 | async function bootstrap() { 6 | const app = await NestFactory.create(AppModule); 7 | 8 | const config = new DocumentBuilder() 9 | .setTitle('Prisma Day - NestJS Prisma Workshop') 10 | .setDescription('Building a REST API with NestJS and Prisma') 11 | .setVersion('1.0') 12 | .build(); 13 | const document = SwaggerModule.createDocument(app, config); 14 | SwaggerModule.setup('api', app, document, { customSiteTitle: 'Prisma Day' }); 15 | 16 | await app.listen(3000); 17 | } 18 | bootstrap(); 19 | -------------------------------------------------------------------------------- /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": [ 4 | "node_modules", 5 | "test", 6 | "dist", 7 | "**/*spec.ts", 8 | "products.ts", 9 | "prisma" 10 | ] 11 | } 12 | -------------------------------------------------------------------------------- /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 | --------------------------------------------------------------------------------