Skip to content

Commit

Permalink
Global PrismaService 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
pyxis committed May 11, 2024
1 parent 425330f commit e5b892b
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 43 deletions.
22 changes: 0 additions & 22 deletions apps/api/src/app/app.controller.spec.ts

This file was deleted.

21 changes: 0 additions & 21 deletions apps/api/src/app/app.service.spec.ts

This file was deleted.

7 changes: 7 additions & 0 deletions apps/api/src/app/build/build.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Injectable } from "@nestjs/common";
import { PrismaService } from "../prisma/prisma.service";

@Injectable()
export class BuildService {
private constructor(prisma: PrismaService) {}
}
23 changes: 23 additions & 0 deletions apps/api/src/app/prisma/extended-client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

import { PrismaClient } from "@prisma/client";

import { existsExtension } from "./extensions/exists.extension";

function extendClient(base: PrismaClient) {
// Add as many as you'd like - no ugly types required!
return base.$extends(existsExtension);
}

class UntypedExtendedClient extends PrismaClient {
constructor(options?: ConstructorParameters<typeof PrismaClient>[0]) {
super(options);

return extendClient(this) as this;
}
}

const ExtendedPrismaClient = UntypedExtendedClient as unknown as new (
options?: ConstructorParameters<typeof PrismaClient>[0]
) => ReturnType<typeof extendClient>;

export { ExtendedPrismaClient };
22 changes: 22 additions & 0 deletions apps/api/src/app/prisma/extensions/exists.extension.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Prisma } from "@prisma/client";

export const existsExtension = Prisma.defineExtension({
name: "exists",
model: {
$allModels: {
async exists<TModel, TArgs extends Prisma.Args<TModel, "findUniqueOrThrow">>(
this: TModel,
args?: Prisma.Exact<TArgs, Prisma.Args<TModel, "findUniqueOrThrow">>
): Promise<boolean> {
const context = Prisma.getExtensionContext(this);

try {
await (context as any).findUniqueOrThrow(args);
return true;
} catch {
return false;
}
},
},
},
});
10 changes: 10 additions & 0 deletions apps/api/src/app/prisma/prisma.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Global, Module } from "@nestjs/common";

import { PrismaService } from "./prisma.service";

@Global()
@Module({
providers: [PrismaService],
exports: [PrismaService],
})
export class PrismaModule {}
22 changes: 22 additions & 0 deletions apps/api/src/app/prisma/prisma.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { OnModuleInit, OnModuleDestroy } from "@nestjs/common";
import { Injectable } from "@nestjs/common";

import { ExtendedPrismaClient } from "./extended-client";

@Injectable()
export class PrismaService extends ExtendedPrismaClient implements OnModuleInit, OnModuleDestroy {
constructor() {
super({
log: ['query', 'info'],
errorFormat: 'colorless',
});
}

async onModuleInit() {
await this.$connect();
}

async onModuleDestroy() {
await this.$disconnect();
}
}

0 comments on commit e5b892b

Please sign in to comment.