百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
N

nestjs-prisma-starter

> 后端框架
开源

NestJS 启动模板包含 GraphQL 与 Prisma 客户端、Passport-JWT 身份验证、Swagger API 和 Docker

2.5K stars0 点赞1 次浏览
访问官网GitHub

工具介绍

NestJS 启动模板包含 GraphQL 与 Prisma 客户端、Passport-JWT 身份验证、Swagger API 和 Docker

Instructions

Starter template for NestJS and Prisma.

Checkout NestJS Prisma Schematics to automatically add Prisma support to your Nest application.

Version

Branch Nest Prisma Graphql
main v9 v4 Code-first
nest-8-prisma-3 v8 v3 Code-first
nest-7 v7 v2 Code-first
nest-6-prisma2-code-first v6 v2-preview Code-first
nest-6-code-first v6 v1 Code-first
nest-6-sdl-first v6 v1 SDL First
nest-5 v5 v1 SDL First

Features

  • GraphQL w/ playground
  • Code-First w/ decorators
  • Prisma for database modelling, migration and type-safe access (Postgres, MySQL & MongoDB)
  • JWT authentication w/ passport-jwt
  • REST API docs w/ Swagger

Overview

  • Instructions
    • Features
    • Overview
    • Prisma Setup
      • 1. Install Dependencies
      • 2. PostgreSQL with Docker
      • 3. Prisma: Prisma Migrate
      • 4. Prisma: Prisma Client JS
      • 5. Seed the database data with this script
      • 6. Start NestJS Server
    • GraphQL Playground
    • Rest Api
    • Docker
    • Schema Development
    • NestJS - Api Schema
      • Resolver
    • GraphQL Client
      • Angular
        • Setup
        • Queries
        • Mutations
        • Subscriptions
        • Authentication

Prisma Setup

1. Install Dependencies

Install Nestjs CLI to start and generate CRUD resources

bash
# npm
npm i -g @nestjs/cli
# yarn
yarn add -g @nestjs/cli

Install the dependencies for the Nest application:

bash
# npm
npm install
# yarn
yarn install

2. PostgreSQL with Docker

Setup a development PostgreSQL with Docker. Copy .env.example and rename to .env - cp .env.example .env - which sets the required environments for PostgreSQL such as POSTGRES_USER, POSTGRES_PASSWORD and POSTGRES_DB. Update the variables as you wish and select a strong password.

Start the PostgreSQL database

bash
docker-compose -f docker-compose.db.yml up -d
# or
npm run docker:db

3. Prisma Migrate

Prisma Migrate is used to manage the schema and migration of the database. Prisma datasource requires an environment variable DATABASE_URL for the connection to the PostgreSQL database. Prisma reads the DATABASE_URL from the root .env file.

Use Prisma Migrate in your development environment to

  1. Creates migration.sql file
  2. Updates Database Schema
  3. Generates Prisma Client
bash
npx prisma migrate dev
# or
npm run migrate:dev

If you like to customize your migration.sql file run the following command. After making your customizations run npx prisma migrate dev to apply it.

bash
npx prisma migrate dev --create-only
# or
npm run migrate:dev:create

If you are happy with your database changes you want to deploy those changes to your production database. Use prisma migrate deploy to apply all pending migrations, can also be used in CI/CD pipelines as it works without prompts.

bash
npx prisma migrate deploy
# or
npm run migrate:deploy

4. Prisma: Prisma Client JS

Prisma Client JS is a type-safe database client auto-generated based on the data model.

Generate Prisma Client JS by running

Note: Every time you update schema.prisma re-generate Prisma Client JS

bash
npx prisma generate
# or
npm run prisma:generate

5. Seed the database data with this script

Execute the script with this command:

bash
npm run seed

6. Start NestJS Server

Run Nest Server in Development mode:

bash
npm run start

# watch mode
npm run start:dev

Run Nest Server in Production mode:

bash
npm run start:prod

GraphQL Playground for the NestJS Server is available here: http://localhost:3000/graphql

⬆ back to top

GraphQL Playground

Open up the example GraphQL queries and copy them to the GraphQL Playground. Some queries and mutations are secured by an auth guard. You have to acquire a JWT token from signup or login. Add the accessTokenas followed to HTTP HEADERS in the playground and replace YOURTOKEN here:

json
{
  "Authorization": "Bearer YOURTOKEN"
}

Rest Api

RESTful API documentation available with Swagger.

Docker

Nest server is a Node.js application and it is easily dockerized.

See the Dockerfile on how to build a Docker image of your Nest server.

Now to build a Docker image of your own Nest server simply run:

bash
# give your docker image a name
docker build -t <your username>/nest-prisma-server .
# for example
docker build -t nest-prisma-server .

After Docker build your docker image you are ready to start up a docker container running the nest server:

bash
docker run -d -t -p 3000:3000 --env-file .env nest-prisma-server

Now open up localhost:3000 to verify that your nest server is running.

When you run your NestJS application in a Docker container update your .env file

diff
- DB_HOST=localhost
# replace with name of the database container
+ DB_HOST=postgres

# Prisma database connection
+ DATABASE_URL=postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${DB_HOST}:${DB_PORT}/${POSTGRES_DB}?schema=${DB_SCHEMA}&sslmode=prefer

If DATABASE_URL is missing in the root .env file, which is loaded into the Docker container, the NestJS application will exit with the following error:

bash
(node:19) UnhandledPromiseRejectionWarning: Error: error: Environment variable not found: DATABASE_URL.
  -->  schema.prisma:3
   |
 2 |   provider = "postgresql"
 3 |   url      = env("DATABASE_URL")

Docker Compose

You can also setup a the database and Nest application with the docker-compose

bash
# building new NestJS docker image
docker-compose build
# or
npm run docker:build

# start docker-compose
docker-compose up -d
# or
npm run docker

Schema Development

Update the Prisma schema prisma/schema.prisma and after that run the following two commands:

bash
npx prisma generate
# or in watch mode
npx prisma generate --watch
# or
npm run prisma:generate
npm run prisma:generate:watch

⬆ back to top

NestJS - Api Schema

The schema.graphql is generated with code first approach from the models, resolvers and input classes.

You can use class-validator to validate your inputs and arguments.

Resolver

To implement the new query, a new resolver function needs to be added to users.resolver.ts.

typescript
@Query(returns => User)
async getUser(@Args() args): Promise<User> {
  return await this.prisma.client.user(args);
}

Restart the NestJS server and this time the Query to fetch a user should work.

⬆ back to top

GraphQL Client

A GraphQL client is necessary to consume the GraphQL api provided by the NestJS Server.

Checkout Apollo a popular GraphQL client which offers several clients for React, Angular, Vue.js, Native iOS, Native Android and more.

Angular

Setup

To start using Apollo Angular simply run in an Angular and Ionic project:

bash
ng add apollo-angular

HttpLink from apollo-angular requires the HttpClient. Therefore, you need to add the HttpClientModule to the AppModule:

typescript
imports: [BrowserModule,
    HttpClientModule,
    ...,
    GraphQLModule],

You can also add the GraphQLModule in the AppModule to make Apollo available in your Angular App.

You need to set the URL to the NestJS GraphQL Api. Open the file src/app/graphql.module.ts and update uri:

typescript
const uri = 'http://localhost:3000/graphql';

To use Apollo-Angular you can inject private apollo: Apollo into the constructor of a page, component or service.

⬆ back to top

Queries

To execute a query you can use:

typescript
this.apollo.query({query: YOUR_QUERY});

# or

this.apollo.watchQuery({
  query: YOUR_QUERY
}).valueChanges;

Here is an example how to fetch your profile from the NestJS GraphQL Api:

typescript
const CurrentUserProfile = gql`
  query CurrentUserProfile {
    me {
      id
      email
      name
    }
  }
`;

@Component({
  selector: 'app-home',
  templateUrl: 'home.page.html',
  styleUrls: ['home.page.scss'],
})
export class HomePage implements OnInit {
  data: Observable<any>;

  constructor(private apollo: Apollo) {}

  ngOnInit() {
    this.data = this.apollo.watchQuery({

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

TypeScriptbcryptcode-firstgraphqlnestjs

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类后端框架
定价开源

> 相关工具

N
Node.js
基于 V8 的 JavaScript 运行时
D
Django
Python 高级 Web 框架
S
Spring Boot
Java 生态主流微服务框架