PingZing
PingZing
PPrisma
Created by PingZing on 12/17/2024 in #help-and-questions
Declaring a PrismaClient with conditional logging
Hi folks! I'm trying to instantiate a PrismaClient in a TypeScript project. I'm using v6.0.1, and have the following code:
export class DatabaseClient {
private _prismaClient;

constructor(databaseUrl: string, appInsightsClient?: appInsights.TelemetryClient) {
const logDefinitions: Prisma.LogDefinition[] = [
{
level: "error",
emit: "event",
},
{
level: "warn",
emit: "event",
},
];

this._prismaClient = new PrismaClient({
datasourceUrl: databaseUrl,
log: logDefinitions,
});

if (someCondition) {
this._prismaClient.$on("error", (evt: Prisma.LogEvent) => {
// Do som error logging stuff
});
}
}
export class DatabaseClient {
private _prismaClient;

constructor(databaseUrl: string, appInsightsClient?: appInsights.TelemetryClient) {
const logDefinitions: Prisma.LogDefinition[] = [
{
level: "error",
emit: "event",
},
{
level: "warn",
emit: "event",
},
];

this._prismaClient = new PrismaClient({
datasourceUrl: databaseUrl,
log: logDefinitions,
});

if (someCondition) {
this._prismaClient.$on("error", (evt: Prisma.LogEvent) => {
// Do som error logging stuff
});
}
}
Unfortunately, on the line where I set up the error logging, I get the following error: Argument of type '"error"' is not assignable to parameter of type 'never'. I can fix that if I declare my array of LogDefinitions inline with the PrismaClient but I'd like to conditionally set up my log levels, which means I can't do everything inline. Is there a way around this?
7 replies