{"id":13847363,"url":"https://github.com/tduyng/nestjs-graphql-prisma","last_synced_at":"2025-07-12T09:31:42.151Z","repository":{"id":49200935,"uuid":"338115769","full_name":"tduyng/nestjs-graphql-prisma","owner":"tduyng","description":"Boilerplate backend NestJS GraphQL project using Prisma 2 \u0026 PostgreSQL","archived":true,"fork":false,"pushed_at":"2021-06-23T21:04:04.000Z","size":2145,"stargazers_count":36,"open_issues_count":0,"forks_count":12,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-11-04T12:03:18.188Z","etag":null,"topics":["clean-architecture","nestjs-boilerplate","nestjs-graphql","nestjs-graphql-prisma","nestjs-postgres","nestjs-prisma","nestjs-starter-template","nestjs-testing","production-ready","redis","refresh-token"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tduyng.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-02-11T18:27:23.000Z","updated_at":"2024-10-26T23:03:28.000Z","dependencies_parsed_at":"2022-09-12T23:50:31.042Z","dependency_job_id":null,"html_url":"https://github.com/tduyng/nestjs-graphql-prisma","commit_stats":null,"previous_names":["tduyng/nestjs-graphql-prisma","tienduy-nguyen/nestjs-graphql-prisma"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tduyng%2Fnestjs-graphql-prisma","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tduyng%2Fnestjs-graphql-prisma/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tduyng%2Fnestjs-graphql-prisma/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tduyng%2Fnestjs-graphql-prisma/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tduyng","download_url":"https://codeload.github.com/tduyng/nestjs-graphql-prisma/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225812882,"owners_count":17528081,"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":["clean-architecture","nestjs-boilerplate","nestjs-graphql","nestjs-graphql-prisma","nestjs-postgres","nestjs-prisma","nestjs-starter-template","nestjs-testing","production-ready","redis","refresh-token"],"created_at":"2024-08-04T18:01:18.030Z","updated_at":"2024-11-21T22:30:18.702Z","avatar_url":"https://github.com/tduyng.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"# NestJS - GraphQL - Prisma\n\n\n\u003cdiv align=\"center\"\u003e\n\u003ca href=\"https://graphql.org/\" target=\"blank\"\u003e\u003cimg src=\"docs/img/GraphQL_Logo.svg\" width=\"120\" alt=\"Nest Logo\" /\u003e\u003c/a\u003e \u003ca href=\"https://www.prisma.io/\" target=\"blank\"\u003e\u003cimg src=\"docs/img/prisma-seeklogo.com.svg\" width=\"90\" alt=\"Nest Logo\" /\u003e\u003c/a\u003e \u003ca href=\"http://nestjs.com/\" target=\"blank\"\u003e\u003cimg src=\"https://nestjs.com/img/logo-small.svg\" width=\"120\" alt=\"Nest Logo\" /\u003e\u003c/a\u003e\n\u003c/div\u003e\n\n\n\u003c!-- \u003cdiv align=\"center\"\u003e\n  \u003ca href=\"http://nestjs.com/\" target=\"blank\"\u003e\u003cimg src=\"docs/img/logo1.png\" width=\"320\" alt=\"Nest Logo\" /\u003e\u003c/a\u003e\n\u003c/div\u003e --\u003e\n\n\u003cdiv align=\"center\"\u003e\nExample backend \u003ca href=\"https://nestjs.com/\"\u003eNestJS\u003c/a\u003e - \u003ca href=\"https://graphql.org/\"\u003eGraphQL\u003c/a\u003e project using \u003ca href=\"https://prisma.io/\"\u003ePrisma 2\u003c/a\u003e \u0026 \u003ca href=\"https://postgresql.org/\" \u003ePostgreSQL\u003c/a\u003e\n\n\u003c/div\u003e\n\n\n\n\n\u003cdiv align=\"center\"\u003e\n\u003cp\u003e\u003c/p\u003e\n\n\n\u003ca href=\"https://github.com/tienduy-nguyen/nestjs-graphql-prisma/workflows/Build/badge.svg\" target=\"_blank\"\u003e\u003cimg src=\"https://github.com/tienduy-nguyen/nestjs-graphql-prisma/workflows/Build/badge.svg\" alt=\"Build github action\" /\u003e \u003ca href='https://github.com/tienduy-nguyen/nestjs-graphql-prisma/workflows/Lint/badge.svg'\u003e\u003cimg src='https://github.com/tienduy-nguyen/nestjs-graphql-prisma/workflows/Lint/badge.svg' alt='Lint github action' /\u003e\u003c/a\u003e \u003ca href='https://github.com/tienduy-nguyen/nestjs-graphql-prisma/workflows/Unit%20tests/badge.svg'\u003e\u003cimg src='https://github.com/tienduy-nguyen/nestjs-graphql-prisma/workflows/Unit%20tests/badge.svg' alt='Lint github action' /\u003e\u003c/a\u003e \u003ca href='https://img.shields.io/badge/Licence-MIT-green'\u003e\u003cimg src=\"https://img.shields.io/badge/Licence-MIT-green\" alt=\"License\"/\u003e\u003c/a\u003e\n\n\n\n\u003c/div\u003e\n\n\n---\n\n**Table of contents**\n\n\u003cdetails\u003e\n\u003csummary\u003eClick here to expand\u003c/summary\u003e\n\n- [NestJS - GraphQL - Prisma](#nestjs---graphql---prisma)\n  - [Features](#features)\n  - [Prerequisites](#prerequisites)\n  - [Getting started](#getting-started)\n    - [Installation](#installation)\n    - [Command lines](#command-lines)\n  - [Project structure](#project-structure)\n    - [Tree project](#tree-project)\n    - [Structure details](#structure-details)\n  - [Resolvers of Project (Endpoints)](#resolvers-of-project-endpoints)\n  - [Docker](#docker)\n    - [Command line](#command-line)\n    - [PgAdmin4](#pgadmin4)\n  - [Todo](#todo)\n  - [Thanks](#thanks)\n\n\n\u003c/details\u003e\n\n\n\n## Features\n\nI create this repo for learning, practicing and understanding how to work NestJS, GraphQL \u0026 Prisma together. Project also how to build a clean architecture scalable \u0026 testable.\n\nProject inspired from [Nestjs-prisma-starter](https://github.com/fivethree-team/nestjs-prisma-starter) of [fivethree-team](https://github.com/fivethree-team).\n\nFeatures of project:\n\n- GraphQL with [apollo-server-express](https://github.com/apollographql/apollo-server), solution [code first](https://docs.nestjs.com/graphql/quick-start#code-first) and using [GraphQL play ground](https://github.com/graphql/graphql-playground)\n- Using [PostgreQL](https://github.com/brianc/node-postgres) for database and [Prisma 2](https://github.com/prisma/prisma) as ORM\n- CRUD operations, database relationship, pagination\n- [NestJS Authentication](https://docs.nestjs.com/security/authentication) with  Redis ~~JWT~~\n- Refresh token solution\n- Role base system: Admin \u0026 User\n- Database seeding\n- Multi-languages with I18n\n- Rating limit API with [express-rate-limit](https://github.com/nfriedly/express-rate-limit)\n- Email verification\n- Using custom exceptions filter, custom decorators\n- Docker solution for PostgreSQL, PGAdmin, Prisma\n- Testing: Unit testing, Integration testing, End to End testing (working in progress)\n- CI-CD with Travis-ci, Github actions\n- Solve N+1 problem graphql (using [@paljs/plugin](https://github.com/paljs/prisma-tools/blob/master/packages/plugins/README.md))\n\n## Prerequisites\n\nIf you want to develop project only on your local, your need have:\n- [NodeJS](https://nodejs.org/en/download/): I use NodeJS lts version (14.15.1)\n- npm or yarn: [yarn](https://classic.yarnpkg.com/en/docs/install/#debian-stable) recommend\n- [@prisma/cli](https://www.prisma.io/docs/concepts/components/prisma-cli/installation)\n- [PostgreSQL](https://www.postgresql.org/download/)\n- [PgAdmin(optional)](https://www.pgadmin.org/download/): UI tool to manage database from PostgreSQL\n\nIf you don't want to install all these tools, you can check the [docker solution](#docker) below.\n## Getting started\n\n### Installation\n\n- Clone project from this repository\n- Create and update `.env` file for variables environment\n  ```bash\n  $ cp .env.example .env\n  ```\n\n  Then modify the variables as you want.\n\n  Example of `.env` files:\n  ```env\n  SERVER_PORT=1776\n  DB_CONNECTOR=postgres\n  DB_HOST=postgres\n  DB_USER=postgres\n  DB_PASSWORD=postgres\n  DB_DATABASE=nest_graphql\n  DB_PORT=5432\n  PGADMIN_DEFAULT_EMAIL=admin@admin.com\n  PGADMIN_DEFAULT_PASSWORD=admin\n\n\n  DATABASE_URL=postgresql://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_DATABASE}?schema=public\n  ```\n- Install dependencies\n  ```bash\n  $ yarn\n  # or npm install\n  ```\n- Generate prisma schema\n  Make sure you generate prisma schema before run server:\n  ```bash\n  $ yarn prisma generate\n  ```\n\n  Check [Prisma Client](https://www.prisma.io/docs/concepts/components/prisma-client) for more details how `prisma generate works`\n\n- Run server locally\n  ```bash\n  $ yarn start:dev\n  # or npm run start:dev\n  ```\n\n### Command lines\n\nOther useful commands to work with project:\n- Run production\n  ```ts\n  $ yarn start:prod\n  ```\n- Run testing:\n  ```bash\n  $ yarn test # for running the the Unit $ integration testing\n  $ yarn test:e2e  # for running end to end testing\n  ```\n- Prisma migrate\n  [Prisma migrate](https://www.prisma.io/docs/concepts/components/prisma-migrate) is an imperative database schema migration tool that enables you to make changes to your database schema.\n\n  Migrate auto your schema:\n  ```bash\n  $ yarn prisma migrate dev --preview-feature\n  ```\n\n  Migrate with the name schema:\n  ```bash\n  $ yarn prisma migrate dev --name initDb --preview-feature\n  ```\n\n  When we run `prisma generate` or `prisma migrate`, `prisma/cli` will try connect with your database first. So you need to connect successfully with your database through your variable `DATABASE_URL` given in `prisma.schema`:\n\n  For example, you have configuration in your `prisma.schema` like below:\n\n  ```prisma\n  // prisma.schema\n  datasource db {\n    provider = \"postgresql\"\n    url      = env(\"DATABASE_URL\")\n  }\n  ```\n\n  So you need to provide this value in `.env` file:\n\n  ```\n  DB_HOST=postgres\n  DB_USER=postgres\n  DB_PASSWORD=postgres\n  DB_DATABASE=nest_graphql\n  DB_PORT=5432\n  DATABASE_URL=\"postgresql://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_DATABASE}?schema=public\"\n\n  ```\n\n  Check more information at [Prisma migrate](https://www.prisma.io/docs/concepts/components/prisma-migrate)\n\n- Seeding database(optional)\n\n  ```bash\n  $ yarn seed\n  ```\n- Prisma studio\n  A Visual Interface for Your Database\n  [Prisma Studio](https://www.prisma.io/blog/prisma-studio-3rtf78dg99fe) helps developers manage their application data. We're excited to share that it is now part of the stable Prisma release. Try out the [online demo](https://prisma.studio/) or connect it to your existing database.\n\n  So with Prisma studio, you maybe don't need **pgAdmin** anymore.\n  \u003cdiv align=\"center\"\u003e\n    \u003cimg src=\"docs/img/prisma-studio.png\" alt=\"prisma-studio\"/\u003e\n  \u003c/div\u003e\n\n  Ton run Prisma studio:\n  ```bash\n  $ yarn prisma:studio\n  # or npx prisma studio\n  # or yarn prisma studio\n  ```\n  Then check out at http://localhost:5555\n\n- **Precommit** with [husky](https://github.com/typicode/husky#readme) \u0026 [lint-staged](https://github.com/okonet/lint-staged#readme)\n  Two awesome packages help us to improve our git commits. We can run linters  and check test before commit changed files.\n\n  So now, each time when we make a commit:\n\n  ```bash\n  $ git add .\n  # or git add files\n  $ git commit -m \"Message commit\"\n  ```\n  All files in your projects will be check and fix automatically with Eslint, Prettier. You can also add `run test` in `precommit` command  to check test before commit.\n\n  **Hint**: Sometime, if you need to commit your code and you don't want to verify them. You can do with flag `--no-verify`:\n\n  ```bash\n  $ git commit - \"Message commit\" --no-verify\n  ```\n\n\n  You can check more other commands in section `scripts` of `package.json`.\n\n## Project structure\n\n### Tree project\n\n\u003cdetails\u003e\n\u003csummary\u003eClick to expand sections\u003c/summary\u003e\n\n```tree\n.\n├── docker\n│   ├── Dockerfile\n│   ├── Dockerfile.prod\n│   └── nginx\n│       ├── Dockerfile.nginx\n│       └── nginx.conf\n├── docker-compose.production.yml\n├── docker-compose.test.yml\n├── docker-compose.yml\n├── graphql\n├── jest.config.js\n├── LICENSE\n├── nest-cli.json\n├── package.json\n├── prisma\n│   ├── migrations\n│   ├── schema.prisma\n│   └── seed.ts\n├── README.md\n├── src\n│   ├── app\n│   │   ├── app.controller.ts\n│   │   ├── app.module.ts\n│   │   ├── app.resolver.ts\n│   │   ├── app.service.ts\n│   ├── common\n│   │   ├── abstract-model\n│   │   ├── configs\n│   │   ├── @generated\n│   │   │   ├── category\n│   │   │   ├── post\n│   │   │   ├── prisma\n│   │   │   ├── profile\n│   │   │   └── user\n│   │   └── types\n│   │       └── node.d.ts\n│   ├── main.ts\n│   ├── modules\n│   │   ├── category\n│   │   ├── email\n│   │   ├── post\n│   │   ├── prisma\n│   │   ├── profile\n│   │   └── user\n│   └── schema.gql\n├── test\n│   ├── app.e2e-spec.ts\n│   └── jest-e2e.json\n├── tsconfig.build.json\n├── tsconfig.json\n└── yarn.lock\n\n```\n\n\u003c/details\u003e\n\n### Structure details\n\n\u003cdetails\u003e\n\u003csummary\u003e\nClick here to expand section\n\u003c/summary\u003e\n\n- `src`:\n  - `app`: contains NestJS app files\n  - `common`: contains shared or general files, type, model, config ... of application\n    - `@generated`: contains generated inputs graphql that match with input of prisma. Thanks [Roman Vasilev](https://github.com/unlight) for this [https://github.com/unlight/prisma-nestjs-graphql] tool.\n    - `configs`: contains configurations of project  as Graphql, Prisma, TypeORM ...\n    - `types`: define general types using for project here\n  - `modules`: contain modules (NestJS) of projects. We separate module for each unit. Each module contains its own data like: model, interface, dto, service, controller, resolver, repository, types...\n    - model\n    - service\n    - resolver\n    - controller\n    - module\n    - interfaces\n    - dto\n    - types\n    - test\n    - decorator\n    - args\n  - `main.ts`: main file to run server\n- `.vscode`: debug configuration in vscode\n- `docker`: contains all files related Docker as `Dockerfile`, `nginx` (except docker-compose)\n- `prisma`:  contains `prisma.shema`, migrations, and seeding\n- `graphql`: contains example queries \u0026 mutations of graphql for project\n- `test`: for end to end testing\n- `docker-compose*.yml`: docker compose files to run different environment docker\n- `.env*`: different environment variables files\n- `.eslint*, prettier*, .editorconfig`: Style \u0026 format code\n- `jest.config.js`: configuration for testing with jest\n- `package.json`\n- `tsconfig.json`: configuration for TypeScript\n- `.circle-ci.yml, .travis.yml, .github/workflows`: CI-CD\n\n\n\u003c/details\u003e\n\n\n## Resolvers of Project (Endpoints)\n\n- [ ] Todo\n\n**Working in progress: Resolvers or Endpoints of project**\n\n---\n## Docker\n\nTo start working with **Docker**, you need have [Docker](https://docs.docker.com/engine/install/) installed locally and also the package manage as **npm** ro **yarn**\n\nIn the **development** mode, we have already **PostgreSQL** and **PgAdmin4** in our `docker-compose` file. You can use the same values environment in your `.env` like before.\n\n### Command line\n\nI think all of you know already how to run docker with `docker-compose`:\nWe have some simple commands:\n\n```bash\n$ docker-compose build # Build all services in docker-compose.yml file\n$ docker-compose up # Run all services in docker-compose.yml\n$ docker-compose down # Stop all services\n$ docker-compose up nest-api # Run only a  service\n```\n\nBut that is not the think I want to say here, we need work with docker like we work locally as `prisma generate`, `prisma migration`, seeding, run test ...\nIn that case how it work?\n\nWell we  will run directly docker-compose file for our service `nest-api` and with `yarn` or `npm` we use as when we work locally.\n\n```\n$ docker-compose -f docker-compose.yml run --rm nest-api yarn prisma:generate\n```\n  - `-f`: file --\u003e using with the name `docker-compose.***.yml` file\n  - `--rm`: flag to stop automatically docker service after executing.\n  - `nest-api`: name service of our server (specify in docker-compose file)\n  - `yarn prisma:generate`: command that we want to execute\n\nBut if you always write command like that, it will be take a lot of time and difficult to remember. So we will prepare theme in section `scripts` of `package.json`.\n\n**Scripts of package.json**\n```json\n{\n  \"script\":{\n    // .....\n    \"docker:build\": \"docker-compose -f docker-compose.yml build --no-cache\",\n    \"docker:prisma:generate\": \"docker-compose -f docker-compose.yml run --rm nest-api yarn prisma:generate\",\n    \"docker:migrate\": \"docker-compose -f docker-compose.yml run --rm nest-api yarn migrate:dev\",\n    \"docker:seed\": \"docker-compose -f docker-compose.yml run --rm nest-api yarn seed\",\n    \"docker:prisma:studio\": \"docker-compose -f docker-compose.yml run nest-api -d yarn prisma:studio\",\n    \"docker:start:dev\": \"docker-compose up\",\n    \"docker:test:seed\": \"docker-compose -f docker-compose.test.yml run --rm nest-api yarn seed\",\n    \"docker:test:migrate\": \"docker-compose -f docker-compose.test.yml run --rm nest-api yarn migrate:dev\",\n    \"docker:test:build\": \"docker-compose -f docker-compose.test.yml build --no-cache\",\n    \"docker:test:run\": \"docker-compose -f docker-compose.test.yml run --rm nest-api yarn test\"\n\n  }\n}\n\n```\n--\u003e You can write your own scripts to work better with docker.\n\n**Hint**: For this moment **integration testing** or **end to end testing** with Prisma is not flexible. Because `env(DATABASE_URI)` take the variable only in `.env` file. I don't want to change manually it when I want to run integration test and e2e test, so I think Docker is very good solution to solve this issue.\n\n**Hint**: You can check [Portainer tool](https://www.portainer.io/) to manage easier multiple docker services. I think you will find it useful too.\n\n### PgAdmin4\n\nA little information to start with docker PgAdmin.\n\nPgAdmin4 is database management tool for PostgreSQL. Because it is a web application based, so PgAdmin is work really well with docker. We can access directly from our browser.\n\nAfter running successfully server with docker, we will open `http://localhost:8080` (Port `8080` is port specified for PgAdmin in `docker-compose` file).\n\nIf you remember, in `.env` file, we have the variables of PgAdmin:\n```\nPGADMIN_DEFAULT_EMAIL=admin@admin.com\nPGADMIN_DEFAULT_PASSWORD=admin\n```\nWe  will use these credentials to connect our PgAdmin.\n\n\n\u003cdiv align=\"center\"\u003e\n\u003cimg src=\"docs/img/pgadmin.png\" alt =\"pgadmin\"/\u003e\n\u003cimg src=\"docs/img/pgadmin-2.png\" alt =\"pgadmin\"/\u003e\n\u003c/div\u003e\n\n\n---\n\n## Todo\n\n- [x] CI-CD\n- [x] Update Auth\n- [x] Multi-language with i18n options\n- [x] Solve N+1 problem graphql (using @paljs/plugin) (make sure use prisma @id as @cuid(), not @uuid()). @uuid() does not work when we query with Prisma select with join table\n- [x] Mailer service\n- [ ] Integrate with AWS service (S3, RDS)\n- [ ] Update In-memory caching or caching with Redis ?\n- [ ] Integration \u0026 end to end testing\n- [ ] Enhanced docker, build images ?\n- [ ] Add Event/Subscribers module ?\n- [ ] Integrate with REST API ? --\u003e create controllers\n- [ ] ...\n## Thanks\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftduyng%2Fnestjs-graphql-prisma","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftduyng%2Fnestjs-graphql-prisma","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftduyng%2Fnestjs-graphql-prisma/lists"}