NicoFish
NicoFish8mo ago

Change db url in ctx with an API call

I want to have a switch in my frontend to change from dev db to prod db. I'm using prisma. Have 2 prisma instances (one for prod and the other for dev). Is there any way to override the prisma instance in the ctx with an API call?
1 Reply
NicoFish
NicoFishOP8mo ago
This is how I create the context:
export const createTRPCContext = async (opts: { headers: Headers }) => {
const session = await getServerAuthSession();

return {
session,
prisma,
...opts,
};
};
export const createTRPCContext = async (opts: { headers: Headers }) => {
const session = await getServerAuthSession();

return {
session,
prisma,
...opts,
};
};
And this is my prisma def:
import { PrismaClient } from '@prisma/client';

declare global {
// eslint-disable-next-line no-var
var prisma: PrismaClient | undefined;
}

export let prisma =
global.prisma ||
new PrismaClient({
log: process.env.NODE_ENV === 'development' ? ['error', 'warn'] : ['error'],
});

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

declare global {
// eslint-disable-next-line no-var
var prisma: PrismaClient | undefined;
}

export let prisma =
global.prisma ||
new PrismaClient({
log: process.env.NODE_ENV === 'development' ? ['error', 'warn'] : ['error'],
});

if (process.env.NODE_ENV !== 'production') {
global.prisma = prisma;
}