Skip to content

Commit

Permalink
Pass prisma options to extendedPrismaClient
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonLin0991 committed Jan 22, 2025
1 parent 51ad8bd commit 5361e6c
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions services/app-api/src/postgres/prismaClient.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import { PrismaClient } from '@prisma/client'
import { type Prisma, PrismaClient } from '@prisma/client'

const errorMessages = {
delete: 'Deletion of records is not allowed',
create: 'Creation of new records is not allowed',
}

/**
* Creates an extended instance of the PrismaClient with custom behavior for certain operations.
* Extends PrismaClient with custom behavior for specific models.
*
* This function extends the PrismaClient to throw errors when attempting to perform
* create or delete operations on the `applicationSettings` and `emailSettings` models.
* Overrides delete and create operations on `applicationSettings` and `emailSettings`
* models to throw custom error messages.
*
* @returns {PrismaClient} An extended instance of the PrismaClient with the custom behavior.
* @param {Prisma.PrismaClientOptions} optionArgs - PrismaClient configuration options.
* @returns {PrismaClient} A new PrismaClient instance with extended behavior.
*/
function extendedPrismaClient() {
return new PrismaClient().$extends({
function extendedPrismaClient(optionArgs: Prisma.PrismaClientOptions) {
return new PrismaClient(optionArgs).$extends({
query: {
applicationSettings: {
delete: () => {
Expand Down Expand Up @@ -62,7 +63,13 @@ async function NewPrismaClient(
connURL: string
): Promise<ExtendedPrismaClient | Error> {
try {
const prismaClient = extendedPrismaClient()
const prismaClient = extendedPrismaClient({
datasources: {
db: {
url: connURL,
},
},
})

return prismaClient
} catch (e: unknown) {
Expand Down

0 comments on commit 5361e6c

Please sign in to comment.