{"id":19972608,"url":"https://github.com/jpb06/nestjs-prisma-postgres-sandbox","last_synced_at":"2025-05-04T01:30:51.973Z","repository":{"id":41890247,"uuid":"361155825","full_name":"jpb06/nestjs-prisma-postgres-sandbox","owner":"jpb06","description":"Toying with nestjs ...","archived":false,"fork":false,"pushed_at":"2025-02-15T13:21:01.000Z","size":1312,"stargazers_count":9,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-08T01:01:42.663Z","etag":null,"topics":["nestjs","postgresql","prisma"],"latest_commit_sha":null,"homepage":"https://nestjs-prisma-postgres-sandbox.fly.dev","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jpb06.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-04-24T12:26:25.000Z","updated_at":"2025-02-15T13:21:04.000Z","dependencies_parsed_at":"2024-02-03T19:33:53.347Z","dependency_job_id":"5433da2f-9f49-4415-b3cd-9d92c41be0aa","html_url":"https://github.com/jpb06/nestjs-prisma-postgres-sandbox","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jpb06%2Fnestjs-prisma-postgres-sandbox","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jpb06%2Fnestjs-prisma-postgres-sandbox/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jpb06%2Fnestjs-prisma-postgres-sandbox/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jpb06%2Fnestjs-prisma-postgres-sandbox/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jpb06","download_url":"https://codeload.github.com/jpb06/nestjs-prisma-postgres-sandbox/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252276955,"owners_count":21722447,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["nestjs","postgresql","prisma"],"created_at":"2024-11-13T03:08:41.385Z","updated_at":"2025-05-04T01:30:51.131Z","avatar_url":"https://github.com/jpb06.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# nestjs-prisma-postgres-sandbox\n\n[![Open in Visual Studio Code](https://img.shields.io/static/v1?logo=visualstudiocode\u0026label=\u0026message=Open%20in%20Visual%20Studio%20Code\u0026labelColor=2c2c32\u0026color=007acc\u0026logoColor=007acc)](https://github.dev/jpb06/nestjs-prisma-postgres-sandbox)\n![Github workflow](https://img.shields.io/github/actions/workflow/status/jpb06/nestjs-prisma-postgres-sandbox/checks.yml?branch=master\u0026logo=github-actions\u0026label=last%20workflow)\n![Github workflow](https://img.shields.io/github/actions/workflow/status/jpb06/nestjs-prisma-postgres-sandbox/checks.yml?branch=master\u0026logo=fly\u0026label=Fly%20Deploy)\n[![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=jpb06_nestjs-prisma-postgres-sandbox\u0026metric=sqale_rating)](https://sonarcloud.io/dashboard?id=jpb06_nestjs-prisma-postgres-sandbox)\n[![Security Rating](https://sonarcloud.io/api/project_badges/measure?project=jpb06_nestjs-prisma-postgres-sandbox\u0026metric=security_rating)](https://sonarcloud.io/dashboard?id=jpb06_nestjs-prisma-postgres-sandbox)\n[![Reliability Rating](https://sonarcloud.io/api/project_badges/measure?project=jpb06_nestjs-prisma-postgres-sandbox\u0026metric=reliability_rating)](https://sonarcloud.io/dashboard?id=jpb06_nestjs-prisma-postgres-sandbox)\n[![Code Smells](https://sonarcloud.io/api/project_badges/measure?project=jpb06_nestjs-prisma-postgres-sandbox\u0026metric=code_smells)](https://sonarcloud.io/dashboard?id=jpb06_nestjs-prisma-postgres-sandbox)\n[![Duplicated Lines (%)](https://sonarcloud.io/api/project_badges/measure?project=jpb06_nestjs-prisma-postgres-sandbox\u0026metric=duplicated_lines_density)](https://sonarcloud.io/dashboard?id=jpb06_nestjs-prisma-postgres-sandbox)\n[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=jpb06_nestjs-prisma-postgres-sandbox\u0026metric=coverage)](https://sonarcloud.io/dashboard?id=jpb06_nestjs-prisma-postgres-sandbox)\n![Coverage](./badges/coverage-jest%20coverage.svg)\n![Last deployment](https://img.shields.io/github/deployments/jpb06/nestjs-prisma-postgres-sandbox/nestjs-prisma-postgres?label=last%20deployment\u0026logo=heroku)\n![Last commit](https://img.shields.io/github/last-commit/jpb06/nestjs-prisma-postgres-sandbox?logo=git)\n\nToying with nest and prisma.\n\n\u003c!-- readme-package-icons start --\u003e\n\n\u003cp align=\"left\"\u003e\u003ca href=\"https://docs.github.com/en/actions\" target=\"_blank\"\u003e\u003cimg height=\"50\" src=\"https://raw.githubusercontent.com/jpb06/jpb06/master/icons/GithubActions-Dark.svg\" /\u003e\u003c/a\u003e\u0026nbsp;\u003ca href=\"https://www.typescriptlang.org/docs/\" target=\"_blank\"\u003e\u003cimg height=\"50\" src=\"https://raw.githubusercontent.com/jpb06/jpb06/master/icons/TypeScript.svg\" /\u003e\u003c/a\u003e\u0026nbsp;\u003ca href=\"https://nodejs.org/en/docs/\" target=\"_blank\"\u003e\u003cimg height=\"50\" src=\"https://raw.githubusercontent.com/jpb06/jpb06/master/icons/NodeJS-Dark.svg\" /\u003e\u003c/a\u003e\u0026nbsp;\u003ca href=\"https://yarnpkg.com/api/\" target=\"_blank\"\u003e\u003cimg height=\"50\" src=\"https://raw.githubusercontent.com/jpb06/jpb06/master/icons/Yarn-Dark.svg\" /\u003e\u003c/a\u003e\u0026nbsp;\u003ca href=\"https://fly.io/docs/\" target=\"_blank\"\u003e\u003cimg height=\"50\" src=\"https://raw.githubusercontent.com/jpb06/jpb06/master/icons/FlyIo-Dark.svg\" /\u003e\u003c/a\u003e\u0026nbsp;\u003ca href=\"https://docs.docker.com\" target=\"_blank\"\u003e\u003cimg height=\"50\" src=\"https://raw.githubusercontent.com/jpb06/jpb06/master/icons/Docker.svg\" /\u003e\u003c/a\u003e\u0026nbsp;\u003ca href=\"https://www.postgresql.org/docs/\" target=\"_blank\"\u003e\u003cimg height=\"50\" src=\"https://raw.githubusercontent.com/jpb06/jpb06/master/icons/PostgreSQL-Dark.svg\" /\u003e\u003c/a\u003e\u0026nbsp;\u003ca href=\"https://github.com/conventional-changelog\" target=\"_blank\"\u003e\u003cimg height=\"50\" src=\"https://raw.githubusercontent.com/jpb06/jpb06/master/icons/CommitLint.Dark.svg\" /\u003e\u003c/a\u003e\u0026nbsp;\u003ca href=\"https://eslint.org/docs/latest/\" target=\"_blank\"\u003e\u003cimg height=\"50\" src=\"https://raw.githubusercontent.com/jpb06/jpb06/master/icons/Eslint-Dark.svg\" /\u003e\u003c/a\u003e\u0026nbsp;\u003ca href=\"https://jestjs.io/docs/getting-started\" target=\"_blank\"\u003e\u003cimg height=\"50\" src=\"https://raw.githubusercontent.com/jpb06/jpb06/master/icons/Jest.svg\" /\u003e\u003c/a\u003e\u0026nbsp;\u003ca href=\"https://jwt.io\" target=\"_blank\"\u003e\u003cimg height=\"50\" src=\"https://raw.githubusercontent.com/jpb06/jpb06/master/icons/Jwt-Dark.svg\" /\u003e\u003c/a\u003e\u0026nbsp;\u003ca href=\"https://docs.nestjs.com\" target=\"_blank\"\u003e\u003cimg height=\"50\" src=\"https://raw.githubusercontent.com/jpb06/jpb06/master/icons/NestJS-Dark.svg\" /\u003e\u003c/a\u003e\u0026nbsp;\u003ca href=\"https://www.passportjs.org/docs/\" target=\"_blank\"\u003e\u003cimg height=\"50\" src=\"https://raw.githubusercontent.com/jpb06/jpb06/master/icons/Passport-Dark.svg\" /\u003e\u003c/a\u003e\u0026nbsp;\u003ca href=\"https://prettier.io/docs/en/index.html\" target=\"_blank\"\u003e\u003cimg height=\"50\" src=\"https://raw.githubusercontent.com/jpb06/jpb06/master/icons/Prettier-Dark.svg\" /\u003e\u003c/a\u003e\u0026nbsp;\u003ca href=\"https://www.prisma.io/docs/\" target=\"_blank\"\u003e\u003cimg height=\"50\" src=\"https://raw.githubusercontent.com/jpb06/jpb06/master/icons/Prisma.svg\" /\u003e\u003c/a\u003e\u0026nbsp;\u003ca href=\"https://rxjs.dev/guide/overview\" target=\"_blank\"\u003e\u003cimg height=\"50\" src=\"https://raw.githubusercontent.com/jpb06/jpb06/master/icons/Rxjs-Dark.svg\" /\u003e\u003c/a\u003e\u0026nbsp;\u003ca href=\"https://swagger.io\" target=\"_blank\"\u003e\u003cimg height=\"50\" src=\"https://raw.githubusercontent.com/jpb06/jpb06/master/icons/Swagger-Dark.svg\" /\u003e\u003c/a\u003e\u0026nbsp;\u003ca href=\"https://swc.rs/docs/getting-started\" target=\"_blank\"\u003e\u003cimg height=\"50\" src=\"https://raw.githubusercontent.com/jpb06/jpb06/master/icons/Swc-Dark.svg\" /\u003e\u003c/a\u003e\u0026nbsp;\u003ca href=\"https://github.com/typestack\" target=\"_blank\"\u003e\u003cimg height=\"50\" src=\"https://raw.githubusercontent.com/jpb06/jpb06/master/icons/TypeStack-Dark.svg\" /\u003e\u003c/a\u003e\u003c/p\u003e\n\n\u003c!-- readme-package-icons end --\u003e\n\nYou can find the [deployed app here](https://nestjs-prisma-postgres-sandbox.fly.dev).\n\n## ⚡ What is this repo about?\n\nThis is a sandbox to evaluate nestjs and dig how fun it is to implement typical requirements for a backend, with an enphasis on testing, given how dearly missed this requirement can be in 'real world' examples found here and there.\n\n## ⚡ Usage\n\n### 🔶 run locally\n\n#### 👇 Since our prisma schemas are split within modules, we will have to merge them all in one file prisma can understand. Let's do just that\n\n```bash\nyarn prisma-merge\n```\n\n#### 👇 Now, we need to tell prisma to generate in node_modules the code actually allowing us to interact with the database\n\n```bash\nyarn prisma-gen\n```\n\n#### 👇 You will need docker and docker-compose to get the postgres database up and running. You can use this command to launch the database container\n\n```bash\nyarn docker\n```\n\n#### 👇 Then, let's inject some data in our dev database using\n\n```bash\nyarn prisma-seed\n```\n\n#### 👇 We can now launch the backend in dev\n\n```bash\nyarn dev\n```\n\n#### 😵 You can do the merge, gen \u0026 seed steps all at once using the following command\n\n```bash\nyarn dev-db\n```\n\n### 🔶 test all the things\n\n👇 We can run all the tests and get a coverage report using the following:\n\n```bash\nyarn test-dev\n```\n\n## ⚡ Guidelines\n\n### 🔶 Schema splitting\n\nWe do not want a huge prisma schema. We want to isolate each model (table or set of tables) in its own file.\n\n### 🔶 Well documented routes\n\nLet's have a swagger documenting properly exposed routes, that is mainly for each route:\n\n- a description.\n- the list of possible responses.\n- a definition of the inputs and outputs.\n\n### 🔶 Full testing coverage\n\nWe want to test everything to learn how to properly test, and to face every single difficulty that comes with testing. We will at very least do end to end using superagent, controllers testing, service testing.\n\n### 🔶 No testing against the database\n\nAll tests should run without any interaction with a database.\n\n## ⚡ Database Schema\n\n![Diagram](./docs/schema.png)\n\n## ⚡ Subjects\n\n### 🔶 Authentication\n\nLet's use passport to setup jwt based authentication.\n\n#### 🚀 Routes\n\nTwo routes were defined to demonstrate the use case:\n\n| Route              | Description                   | Documentation                                                                                  |\n| ------------------ | ----------------------------- | ---------------------------------------------------------------------------------------------- |\n| POST /users/login  | The login route               | [Link](https://nestjs-prisma-postgres-sandbox.fly.dev/#/crud-users/UsersController_login)      |\n| GET /users/profile | Logged user profile retrieval | [Link](https://nestjs-prisma-postgres-sandbox.fly.dev/#/crud-users/UsersController_getProfile) |\n\n#### :books: Mock data\n\nWe have two users in database to play with the routes:\n\n- alice@cool.org / alice\n- bob@cool.org / bob\n\n#### 🧪 Tests\n\n- ✅ e2e\n- ✅ controllers\n- ✅ services\n- ✅ local passport strategy\n\n### 🔶 CRUD\n\nLet's create CRUD routes to manage a list of books.\nWe want to make sure to give a proper feedback when foreign keys violations do occur (when we try to delete an entry whose key is referenced in another table or when we try to update an entry with a foreign key that does not exist). Let's use filters for that!\n\n#### 🚀 Routes\n\n| Route                   | Description                             | Documentation                                                                                          |\n| ----------------------- | --------------------------------------- | ------------------------------------------------------------------------------------------------------ |\n| GET /books              | Retrieves all books                     | [Link](https://nestjs-prisma-postgres-sandbox.fly.dev/#/#/crud-books/BooksController_getBooks)         |\n| GET /authors            | Retrieves all authors                   | [Link](https://nestjs-prisma-postgres-sandbox.fly.dev/#/#/crud-authors/AuthorsController_getAuthors)   |\n| GET /authors/{id}/books | Retrieves the book written by an author | [Link](https://nestjs-prisma-postgres-sandbox.fly.dev/#/#/crud-books/AuthorsController_getAuthorBooks) |\n| POST /books             | Creates a book                          | [Link](https://nestjs-prisma-postgres-sandbox.fly.dev/#/#/crud-books/BooksController_createBook)       |\n| POST /authors           | Creates an author                       | [Link](https://nestjs-prisma-postgres-sandbox.fly.dev/#/#/crud-authors/AuthorsController_createAuthor) |\n| PUT /books/{id}         | Updates a book                          | [Link](https://nestjs-prisma-postgres-sandbox.fly.dev/#/#/crud-books/BooksController_updateBook)       |\n| PUT /authors/{id}       | Updates an author                       | [Link](https://nestjs-prisma-postgres-sandbox.fly.dev/#/#/crud-authors/AuthorsController_updateAuthor) |\n| DELETE /books/{id}      | Deletes a book                          | [Link](https://nestjs-prisma-postgres-sandbox.fly.dev/#/#/crud-books/BooksController_deleteBook)       |\n| DELETE /authors/{id}    | Deletes an author                       | [Link](https://nestjs-prisma-postgres-sandbox.fly.dev/#/#/crud-authors/AuthorsController_deleteAuthor) |\n\n#### 🧪 Tests\n\n- ✅ e2e\n- ✅ controllers (turns out these are pretty much useless since we mock the service)\n- ✅ services\n- ✅ validation pipe\n- ✅ filters\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjpb06%2Fnestjs-prisma-postgres-sandbox","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjpb06%2Fnestjs-prisma-postgres-sandbox","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjpb06%2Fnestjs-prisma-postgres-sandbox/lists"}