Create prisma client with extension without circular dependency

FOUND SOLUTION: Prisma.defineExtension takes a callback which receives the client --------------- If I have something like this:
const extension = Prisma.defineExtension({
model: {
user: {
// @typescript-eslint/no-use-before-define: 'db' was used before it was defined
example: async () => {
return await db.user.findMany() }
}
}
})

const getExtendedPrismaClient = () => {
return new PrismaClient().$extends(extension)
}

type ExtendedPrismaClient = ReturnType<typeof getExtendedPrismaClient>

const globalForPrisma = globalThis as unknown as { db: ExtendedPrismaClient }

const db = globalForPrisma.db || getExtendedPrismaClient()

export default db;

if (process.env.NODE_ENV !== "production") {
globalForPrisma.db = db
}
const extension = Prisma.defineExtension({
model: {
user: {
// @typescript-eslint/no-use-before-define: 'db' was used before it was defined
example: async () => {
return await db.user.findMany() }
}
}
})

const getExtendedPrismaClient = () => {
return new PrismaClient().$extends(extension)
}

type ExtendedPrismaClient = ReturnType<typeof getExtendedPrismaClient>

const globalForPrisma = globalThis as unknown as { db: ExtendedPrismaClient }

const db = globalForPrisma.db || getExtendedPrismaClient()

export default db;

if (process.env.NODE_ENV !== "production") {
globalForPrisma.db = db
}
Essentially, I am asking how I can perform database operations before I have actually initialised the database.
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server