|
1 | 1 | import { LibnestPrismaExtension } from '@douglasneuroinformatics/libnest'; |
2 | | -import type { PrismaModelKey, PrismaModelName } from '@douglasneuroinformatics/libnest'; |
| 2 | +import type { |
| 3 | + ConfigService, |
| 4 | + PrismaModelKey, |
| 5 | + PrismaModelName, |
| 6 | + PrismaModuleOptions |
| 7 | +} from '@douglasneuroinformatics/libnest'; |
| 8 | +import { Injectable } from '@nestjs/common'; |
| 9 | +import type { OnApplicationShutdown } from '@nestjs/common'; |
3 | 10 | import { PrismaClient } from '@prisma/client'; |
| 11 | +import type { MongoMemoryReplSet } from 'mongodb-memory-server'; |
4 | 12 |
|
5 | | -export function createPrismaClient(datasourceUrl: string) { |
6 | | - return new PrismaClient({ datasourceUrl }).$extends(LibnestPrismaExtension); |
| 13 | +@Injectable() |
| 14 | +export class PrismaModuleOptionsFactory implements OnApplicationShutdown { |
| 15 | + private memoryReplSet: MongoMemoryReplSet | null; |
| 16 | + |
| 17 | + constructor(private readonly configService: ConfigService) { |
| 18 | + this.memoryReplSet = null; |
| 19 | + } |
| 20 | + |
| 21 | + async create() { |
| 22 | + let datasourceUrl: string; |
| 23 | + if (this.configService.get('NODE_ENV') === 'test') { |
| 24 | + datasourceUrl = await this.createMemoryConnection(); |
| 25 | + } else { |
| 26 | + datasourceUrl = this.getExternalConnection(); |
| 27 | + } |
| 28 | + const client = new PrismaClient({ |
| 29 | + datasourceUrl, |
| 30 | + omit: { |
| 31 | + user: { |
| 32 | + hashedPassword: true |
| 33 | + } |
| 34 | + } |
| 35 | + }).$extends(LibnestPrismaExtension); |
| 36 | + await client.$connect(); |
| 37 | + return { client } satisfies PrismaModuleOptions; |
| 38 | + } |
| 39 | + |
| 40 | + async onApplicationShutdown() { |
| 41 | + if (this.memoryReplSet) { |
| 42 | + await this.memoryReplSet.stop(); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + private async createMemoryConnection(): Promise<string> { |
| 47 | + // prevent mongodb-memory-server from being included in the production bundle |
| 48 | + const { MongoMemoryReplSet } = await import('mongodb-memory-server'); |
| 49 | + const replSet = await MongoMemoryReplSet.create({ replSet: { count: 1, name: 'rs0' } }); |
| 50 | + return new URL(replSet.getUri('test')).href; |
| 51 | + } |
| 52 | + |
| 53 | + private getExternalConnection(): string { |
| 54 | + const mongoUri = this.configService.get('MONGO_URI'); |
| 55 | + const env = this.configService.get('NODE_ENV'); |
| 56 | + const url = new URL(`${mongoUri.href}/data-capture-${env}`); |
| 57 | + const params = { |
| 58 | + directConnection: this.configService.get('MONGO_DIRECT_CONNECTION'), |
| 59 | + replicaSet: this.configService.get('MONGO_REPLICA_SET'), |
| 60 | + retryWrites: this.configService.get('MONGO_RETRY_WRITES'), |
| 61 | + w: this.configService.get('MONGO_WRITE_CONCERN') |
| 62 | + }; |
| 63 | + for (const [key, value] of Object.entries(params)) { |
| 64 | + if (value) { |
| 65 | + url.searchParams.append(key, String(value)); |
| 66 | + } |
| 67 | + } |
| 68 | + return url.href; |
| 69 | + } |
7 | 70 | } |
8 | 71 |
|
9 | | -export type RuntimePrismaClient = ReturnType<typeof createPrismaClient>; |
| 72 | +export type RuntimePrismaClient = Awaited< |
| 73 | + ReturnType<(typeof PrismaModuleOptionsFactory)['prototype']['create']> |
| 74 | +>['client']; |
10 | 75 |
|
11 | 76 | export type PrismaModelWhereInputMap = { |
12 | 77 | [K in PrismaModelName]: PrismaClient[PrismaModelKey<K>] extends { |
|
0 commit comments