/Prisma-Examples

Prisma: 下一代ORM, 不仅仅是ORM Demo 仓库

Primary LanguageTypeScriptMIT LicenseMIT

Prisma-Examples

prisma prisma-client

Left Targets

  • Example with MongoDB
  • Example with Prisma Data Platform

Getting Started

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

Advanced Multi-Models Example

Relation

  • User -> Post: 1-n
  • User -> Profile: 1-1 (Optional in User, Required in Profile)
  • User -> Fragment: 1-n (Optional in User, Optional in Fragment)
  • Post -> Category: m-n(connected by CtegoriesOnPosts model)
  • Self-Relation: User.invitor(1-1) & User.invitation(1-n)

With TypeGraphQL + Apollo-Server

With Other ORMs

With TypeORM(SQLite + SQLite)

Example

With NestJS

  • Create PrismaService which extends PrismaClient:

    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, and PrismaModule 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 in AppModule:

    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;
      }
    }

Unit Test