- Example with MongoDB
- Example with Prisma Data Platform
If you got any troubles running commands below, add DEBUG=*
as command prefix to see internal logs.
pnpm install
# Optional: Install Prisma CLI Globally
# @prisma/cli has been deprecated and prisma instead should be installed
pnpm install prisma -g
# run all examples in a single command!
pnpm start
# avaliable examples:
# single model
pnpm dev:single
# multi models & relations
pnpm dev:multi
# advanced multi models example
pnpm dev:advanced
# GraphQL example(TypeGraphQL + Apollo-Server)
pnpm dev:graphql
# with TypeORM
pnpm dev:with-typeorm
# generate Prisma Client for all examples
pnpm gen:client
# common prisma commands
# sync Prisma Schema with database(in this repo, will generate sqlite file)
prisma db push
# generate Prisma Client from prisma schema
prisma generate
# execute this command to invoke database migration after Prisma Schema got modified
prisma migrate
# generate Prisma Schema from exist database
prisma introspect
# run test cases(GraphQL example)
pnpm test:graphql
User
->Post
: 1-nUser
->Profile
: 1-1 (Optional in User, Required in Profile)User
->Fragment
: 1-n (Optional in User, Optional in Fragment)Post
->Category
: m-n(connected byCtegoriesOnPosts
model)- Self-Relation:
User.invitor
(1-1) &User.invitation
(1-n)
- Generated Schema
- Generated TypeScript Code
- Prisma Model
- Resolvers
- Explore
pnpm dev
- open GraphiQL in http://localhost:5999/graphql
- Example GraphQL Query / Mutation(arguments in operations are from database initial data seeding)
- GenQL: Type Safe Graphql Query Builder:
pnpm gen:genql
(Require local server online),Example
- Test:
- database in GraphQL example will be reset before running test cases.
-
Create
PrismaService
which extendsPrismaClient
:import { Injectable, OnModuleInit, OnModuleDestroy, OnApplicationBootstrap, OnApplicationShutdown, } from "@nestjs/common"; import { PrismaClient } from "@prisma/client"; @Injectable() export class PrismaService extends PrismaClient implements OnModuleInit, OnApplicationBootstrap, OnModuleDestroy, OnApplicationShutdown { constructor() { super(); } async onModuleInit() { await this.$connect(); } async onApplicationBootstrap() { // await seeds(); } async onModuleDestroy() {} async onApplicationShutdown() { await this.$disconnect(); } }
-
Register
PrismaService
as a provider, andPrismaModule
as a Global module:import { Global, Module } from "@nestjs/common"; import { PrismaService } from "./prisma.service"; @Global() @Module({ providers: [PrismaService], exports: [PrismaService], }) export default class PrismaModule {}
-
Import
PrismaModule
inAppModule
:import PrismaModule from "./prisma/prisma.module"; @Module({ imports: [PrismaModule], }) export class AppModule implements NestModule, OnApplicationBootstrap, OnApplicationShutdown { constructor(private readonly connection: Connection) {} async configure( consumer: MiddlewareConsumer ): Promise<void | MiddlewareConsumer> {} // you can connect/disconnect prisma client here async onApplicationBootstrap() {} async onApplicationShutdown() {} }
-
Use
PrismaService
:import { Injectable } from "@nestjs/common"; import { PrismaService } from "../../prisma/prisma.service"; @Injectable() export class UserService { constructor(private prisma: PrismaService) {} async queryAllUsers() { const res = await this.prisma.user.findMany({}); return res; } }
- see prisma-unit-test folder.