├── .prettierrc ├── nest-cli.json ├── tsconfig.build.json ├── prisma ├── migrations │ ├── migration_lock.toml │ └── 20220311180626_initial_migration │ │ └── migration.sql └── schema.prisma ├── prisma2 ├── migrations │ ├── migration_lock.toml │ └── 20220312181356_initial_migration │ │ └── migration.sql └── schema.prisma ├── db └── init.sql ├── test ├── jest-e2e.json └── app.e2e-spec.ts ├── src ├── main.ts ├── app.module.ts ├── prisma.service.ts ├── prisma2.service.ts ├── app.controller.ts ├── app.service.ts └── app.controller.spec.ts ├── .env.example ├── README.md ├── docker-compose.yml ├── .gitignore ├── tsconfig.json ├── .eslintrc.js └── package.json /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "singleQuote": true, 3 | "trailingComma": "all" 4 | } -------------------------------------------------------------------------------- /nest-cli.json: -------------------------------------------------------------------------------- 1 | { 2 | "collection": "@nestjs/schematics", 3 | "sourceRoot": "src" 4 | } 5 | -------------------------------------------------------------------------------- /tsconfig.build.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.json", 3 | "exclude": ["node_modules", "test", "dist", "**/*spec.ts"] 4 | } 5 | -------------------------------------------------------------------------------- /prisma/migrations/migration_lock.toml: -------------------------------------------------------------------------------- 1 | # Please do not edit this file manually 2 | # It should be added in your version-control system (i.e. Git) 3 | provider = "mysql" -------------------------------------------------------------------------------- /prisma2/migrations/migration_lock.toml: -------------------------------------------------------------------------------- 1 | # Please do not edit this file manually 2 | # It should be added in your version-control system (i.e. Git) 3 | provider = "mysql" -------------------------------------------------------------------------------- /db/init.sql: -------------------------------------------------------------------------------- 1 | CREATE DATABASE IF NOT EXISTS `prisma-shadow`; 2 | GRANT ALL ON `prisma-shadow`.* TO 'prisma'@'%'; 3 | CREATE DATABASE IF NOT EXISTS `prisma2`; 4 | GRANT ALL ON `prisma2`.* TO 'prisma'@'%'; 5 | -------------------------------------------------------------------------------- /test/jest-e2e.json: -------------------------------------------------------------------------------- 1 | { 2 | "moduleFileExtensions": ["js", "json", "ts"], 3 | "rootDir": ".", 4 | "testEnvironment": "node", 5 | "testRegex": ".e2e-spec.ts$", 6 | "transform": { 7 | "^.+\\.(t|j)s$": "ts-jest" 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/main.ts: -------------------------------------------------------------------------------- 1 | import { NestFactory } from '@nestjs/core'; 2 | import { AppModule } from './app.module'; 3 | 4 | async function bootstrap() { 5 | const app = await NestFactory.create(AppModule); 6 | await app.listen(3000); 7 | } 8 | bootstrap(); 9 | -------------------------------------------------------------------------------- /prisma/migrations/20220311180626_initial_migration/migration.sql: -------------------------------------------------------------------------------- 1 | -- CreateTable 2 | CREATE TABLE `User` ( 3 | `id` INTEGER NOT NULL AUTO_INCREMENT, 4 | `name` VARCHAR(191) NOT NULL, 5 | 6 | PRIMARY KEY (`id`) 7 | ) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; 8 | -------------------------------------------------------------------------------- /prisma2/migrations/20220312181356_initial_migration/migration.sql: -------------------------------------------------------------------------------- 1 | -- CreateTable 2 | CREATE TABLE `Blog` ( 3 | `id` INTEGER NOT NULL AUTO_INCREMENT, 4 | `title` VARCHAR(191) NOT NULL, 5 | 6 | PRIMARY KEY (`id`) 7 | ) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; 8 | -------------------------------------------------------------------------------- /.env.example: -------------------------------------------------------------------------------- 1 | MYSQL_ROOT_PASSWORD=prisma 2 | MYSQL_DATABASE=prisma 3 | MYSQL_USER=prisma 4 | MYSQL_PASSWORD=prisma 5 | DATABASE_URL=mysql://prisma:prisma@localhost:3306/prisma 6 | BLOG_DATABASE_URL=mysql://prisma:prisma@localhost:3306/prisma2 7 | SHADOW_DATABASE_URL=mysql://prisma:prisma@localhost:3306/prisma-shadow -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Prisma multi database example 2 | 3 | # Setup 4 | 5 | ```bash 6 | git clone https://github.com/sagarPakhrin/prisma-multidatabase-demo.git 7 | 8 | cd prisma-multidatabase-demo 9 | 10 | cp .env.example .env 11 | 12 | npm i 13 | 14 | docker-compose up -d 15 | 16 | npm run start:dev 17 | 18 | ``` 19 | -------------------------------------------------------------------------------- /prisma/schema.prisma: -------------------------------------------------------------------------------- 1 | generator client { 2 | provider = "prisma-client-js" 3 | } 4 | 5 | datasource db { 6 | provider = "mysql" 7 | url = env("DATABASE_URL") 8 | shadowDatabaseUrl = env("SHADOW_DATABASE_URL") 9 | } 10 | 11 | model User { 12 | id Int @id @default(autoincrement()) 13 | name String 14 | } 15 | -------------------------------------------------------------------------------- /prisma2/schema.prisma: -------------------------------------------------------------------------------- 1 | generator client { 2 | provider = "prisma-client-js" 3 | output = "../node_modules/@internal/prisma/client" 4 | } 5 | 6 | datasource db { 7 | provider = "mysql" 8 | url = env("BLOG_DATABASE_URL") 9 | shadowDatabaseUrl = env("SHADOW_DATABASE_URL") 10 | } 11 | 12 | model Blog { 13 | id Int @id @default(autoincrement()) 14 | title String 15 | } 16 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '3.8' 2 | 3 | services: 4 | mysql: 5 | image: mysql:8 6 | ports: 7 | - '3306:3306' 8 | environment: 9 | MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD} 10 | MYSQL_DATABASE: ${MYSQL_DATABASE} 11 | MYSQL_USER: ${MYSQL_USER} 12 | MYSQL_PASSWORD: ${MYSQL_PASSWORD} 13 | volumes: 14 | - ./tmp:/var/lib/mysql 15 | - ./db:/docker-entrypoint-initdb.d 16 | -------------------------------------------------------------------------------- /src/app.module.ts: -------------------------------------------------------------------------------- 1 | import { Module } from '@nestjs/common'; 2 | import { AppController } from './app.controller'; 3 | import { AppService } from './app.service'; 4 | import { PrismaService } from './prisma.service'; 5 | import { PrismaService as PrismaService2 } from './prisma2.service'; 6 | 7 | @Module({ 8 | imports: [], 9 | controllers: [AppController], 10 | providers: [AppService, PrismaService, PrismaService2], 11 | }) 12 | export class AppModule {} 13 | -------------------------------------------------------------------------------- /src/prisma.service.ts: -------------------------------------------------------------------------------- 1 | import { INestApplication, Injectable, OnModuleInit } from '@nestjs/common'; 2 | import { PrismaClient } from '@prisma/client'; 3 | 4 | @Injectable() 5 | export class PrismaService extends PrismaClient implements OnModuleInit { 6 | async onModuleInit() { 7 | await this.$connect(); 8 | } 9 | 10 | async enableShutdownHooks(app: INestApplication) { 11 | this.$on('beforeExit', async () => { 12 | await app.close(); 13 | }); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/prisma2.service.ts: -------------------------------------------------------------------------------- 1 | import { PrismaClient } from '@internal/prisma/client'; 2 | import { INestApplication, Injectable, OnModuleInit } from '@nestjs/common'; 3 | 4 | @Injectable() 5 | export class PrismaService extends PrismaClient implements OnModuleInit { 6 | async onModuleInit() { 7 | await this.$connect(); 8 | } 9 | 10 | async enableShutdownHooks(app: INestApplication) { 11 | this.$on('beforeExit', async () => { 12 | await app.close(); 13 | }); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/app.controller.ts: -------------------------------------------------------------------------------- 1 | import { Blog } from '@internal/prisma/client'; 2 | import { Controller, Get } from '@nestjs/common'; 3 | import { User } from '@prisma/client'; 4 | import { AppService } from './app.service'; 5 | 6 | @Controller() 7 | export class AppController { 8 | constructor(private readonly appService: AppService) {} 9 | 10 | @Get('/users') 11 | getUsers(): Promise { 12 | return this.appService.getUsers(); 13 | } 14 | 15 | @Get('/blogs') 16 | getBlogs(): Promise { 17 | return this.appService.getBlogs(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # compiled output 2 | /dist 3 | /node_modules 4 | 5 | # Logs 6 | logs 7 | *.log 8 | npm-debug.log* 9 | pnpm-debug.log* 10 | yarn-debug.log* 11 | yarn-error.log* 12 | lerna-debug.log* 13 | 14 | # OS 15 | .DS_Store 16 | 17 | # Tests 18 | /coverage 19 | /.nyc_output 20 | 21 | # IDEs and editors 22 | /.idea 23 | .project 24 | .classpath 25 | .c9/ 26 | *.launch 27 | .settings/ 28 | *.sublime-workspace 29 | 30 | # IDE - VSCode 31 | .vscode/* 32 | !.vscode/settings.json 33 | !.vscode/tasks.json 34 | !.vscode/launch.json 35 | !.vscode/extensions.json 36 | 37 | tmp 38 | .env -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "module": "commonjs", 4 | "declaration": true, 5 | "removeComments": true, 6 | "emitDecoratorMetadata": true, 7 | "experimentalDecorators": true, 8 | "allowSyntheticDefaultImports": true, 9 | "target": "es2017", 10 | "sourceMap": true, 11 | "outDir": "./dist", 12 | "baseUrl": "./", 13 | "incremental": true, 14 | "skipLibCheck": true, 15 | "strictNullChecks": false, 16 | "noImplicitAny": false, 17 | "strictBindCallApply": false, 18 | "forceConsistentCasingInFileNames": false, 19 | "noFallthroughCasesInSwitch": false 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/app.service.ts: -------------------------------------------------------------------------------- 1 | import { Blog } from '@internal/prisma/client'; 2 | import { Injectable } from '@nestjs/common'; 3 | import { User } from '@prisma/client'; 4 | import { PrismaService } from './prisma.service'; 5 | import { PrismaService as PrismaService2 } from './prisma2.service'; 6 | 7 | @Injectable() 8 | export class AppService { 9 | constructor( 10 | private readonly prisma: PrismaService, 11 | private readonly prisma2: PrismaService2, 12 | ) {} 13 | 14 | async getUsers(): Promise { 15 | return this.prisma.user.findMany(); 16 | } 17 | 18 | async getBlogs(): Promise { 19 | return this.prisma2.blog.findMany(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/app.controller.spec.ts: -------------------------------------------------------------------------------- 1 | import { Test, TestingModule } from '@nestjs/testing'; 2 | import { AppController } from './app.controller'; 3 | import { AppService } from './app.service'; 4 | 5 | describe('AppController', () => { 6 | let appController: AppController; 7 | 8 | beforeEach(async () => { 9 | const app: TestingModule = await Test.createTestingModule({ 10 | controllers: [AppController], 11 | providers: [AppService], 12 | }).compile(); 13 | 14 | appController = app.get(AppController); 15 | }); 16 | 17 | describe('root', () => { 18 | it('should return "Hello World!"', () => { 19 | expect(appController.getHello()).toBe('Hello World!'); 20 | }); 21 | }); 22 | }); 23 | -------------------------------------------------------------------------------- /.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | parser: '@typescript-eslint/parser', 3 | parserOptions: { 4 | project: 'tsconfig.json', 5 | sourceType: 'module', 6 | }, 7 | plugins: ['@typescript-eslint/eslint-plugin'], 8 | extends: [ 9 | 'plugin:@typescript-eslint/recommended', 10 | 'plugin:prettier/recommended', 11 | ], 12 | root: true, 13 | env: { 14 | node: true, 15 | jest: true, 16 | }, 17 | ignorePatterns: ['.eslintrc.js'], 18 | rules: { 19 | '@typescript-eslint/interface-name-prefix': 'off', 20 | '@typescript-eslint/explicit-function-return-type': 'off', 21 | '@typescript-eslint/explicit-module-boundary-types': 'off', 22 | '@typescript-eslint/no-explicit-any': 'off', 23 | }, 24 | }; 25 | -------------------------------------------------------------------------------- /test/app.e2e-spec.ts: -------------------------------------------------------------------------------- 1 | import { Test, TestingModule } from '@nestjs/testing'; 2 | import { INestApplication } from '@nestjs/common'; 3 | import * as request from 'supertest'; 4 | import { AppModule } from './../src/app.module'; 5 | 6 | describe('AppController (e2e)', () => { 7 | let app: INestApplication; 8 | 9 | beforeEach(async () => { 10 | const moduleFixture: TestingModule = await Test.createTestingModule({ 11 | imports: [AppModule], 12 | }).compile(); 13 | 14 | app = moduleFixture.createNestApplication(); 15 | await app.init(); 16 | }); 17 | 18 | it('/ (GET)', () => { 19 | return request(app.getHttpServer()) 20 | .get('/') 21 | .expect(200) 22 | .expect('Hello World!'); 23 | }); 24 | }); 25 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "prisma-multi-database", 3 | "version": "0.0.1", 4 | "description": "", 5 | "author": "", 6 | "private": true, 7 | "license": "UNLICENSED", 8 | "scripts": { 9 | "prebuild": "rimraf dist", 10 | "build": "nest build", 11 | "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"", 12 | "start": "nest start", 13 | "start:dev": "nest start --watch", 14 | "start:debug": "nest start --debug --watch", 15 | "start:prod": "node dist/main", 16 | "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix", 17 | "test": "jest", 18 | "test:watch": "jest --watch", 19 | "test:cov": "jest --coverage", 20 | "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand", 21 | "test:e2e": "jest --config ./test/jest-e2e.json", 22 | "migrate": "npx prisma migrate dev", 23 | "schema2:migrate": "npx prisma migrate dev --schema ./prisma2/schema.prisma" 24 | }, 25 | "dependencies": { 26 | "@nestjs/common": "^8.0.0", 27 | "@nestjs/core": "^8.0.0", 28 | "@nestjs/platform-express": "^8.0.0", 29 | "@prisma/client": "^3.10.0", 30 | "reflect-metadata": "^0.1.13", 31 | "rimraf": "^3.0.2", 32 | "rxjs": "^7.2.0" 33 | }, 34 | "devDependencies": { 35 | "@nestjs/cli": "^8.0.0", 36 | "@nestjs/schematics": "^8.0.0", 37 | "@nestjs/testing": "^8.0.0", 38 | "@types/express": "^4.17.13", 39 | "@types/jest": "27.0.2", 40 | "@types/node": "^16.0.0", 41 | "@types/supertest": "^2.0.11", 42 | "@typescript-eslint/eslint-plugin": "^5.0.0", 43 | "@typescript-eslint/parser": "^5.0.0", 44 | "eslint": "^8.0.1", 45 | "eslint-config-prettier": "^8.3.0", 46 | "eslint-plugin-prettier": "^4.0.0", 47 | "jest": "^27.2.5", 48 | "prettier": "^2.3.2", 49 | "prisma": "^3.10.0", 50 | "source-map-support": "^0.5.20", 51 | "supertest": "^6.1.3", 52 | "ts-jest": "^27.0.3", 53 | "ts-loader": "^9.2.3", 54 | "ts-node": "^10.0.0", 55 | "tsconfig-paths": "^3.10.1", 56 | "typescript": "^4.3.5" 57 | }, 58 | "jest": { 59 | "moduleFileExtensions": [ 60 | "js", 61 | "json", 62 | "ts" 63 | ], 64 | "rootDir": "src", 65 | "testRegex": ".*\\.spec\\.ts$", 66 | "transform": { 67 | "^.+\\.(t|j)s$": "ts-jest" 68 | }, 69 | "collectCoverageFrom": [ 70 | "**/*.(t|j)s" 71 | ], 72 | "coverageDirectory": "../coverage", 73 | "testEnvironment": "node" 74 | } 75 | } 76 | --------------------------------------------------------------------------------