nikitarevenco
nikitarevenco
PPrisma
Created by nikitarevenco on 7/15/2024 in #help-and-questions
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.
2 replies
PPrisma
Created by nikitarevenco on 7/12/2024 in #help-and-questions
Prisma Client is not configured to run in the edge runtime
No description
18 replies
PPrisma
Created by nikitarevenco on 7/12/2024 in #help-and-questions
Get type of prisma client before it is defined
In my next.js app I have to use the following approach to prevent multiple instances of PrismaClient being created
import { PrismaClient } from "@prisma/client";

const extension = {
model: {
user: {
example: () => "example",
},
},
};

const globalForPrisma = globalThis as unknown as { db: PrismaClient };
const db = globalForPrisma.db || new PrismaClient().$extends(extension);

export default db;

if (process.env.NODE_ENV !== "production") globalForPrisma.db = db;
import { PrismaClient } from "@prisma/client";

const extension = {
model: {
user: {
example: () => "example",
},
},
};

const globalForPrisma = globalThis as unknown as { db: PrismaClient };
const db = globalForPrisma.db || new PrismaClient().$extends(extension);

export default db;

if (process.env.NODE_ENV !== "production") globalForPrisma.db = db;
The above approach however is problematic because it doesn't generate the correct PrismaClient type. E.g. in my project TS will complain that db.user.example doesn't exist. I tried solving this with typeof new PrismaClient().$extends(extension) but I need globalForPrisma to be defined before the PrismaClient has been defined
12 replies