chore(db): add logger to mutations

This commit is contained in:
Carl-Gerhard Lindesvärd
2025-02-22 14:27:07 +01:00
parent bc311e35f7
commit 93944fe85f
2 changed files with 51 additions and 8 deletions

View File

@@ -1,16 +1,39 @@
import { createLogger } from '@openpanel/logger';
import { PrismaClient } from '@prisma/client';
import { readReplicas } from '@prisma/extension-read-replicas';
export * from '@prisma/client';
const logger = createLogger({ name: 'db' });
const getPrismaClient = () => {
return new PrismaClient({
const prisma = new PrismaClient({
log: ['error'],
}).$extends(
readReplicas({
url: process.env.DATABASE_URL_REPLICA ?? process.env.DATABASE_URL!,
}),
);
})
.$extends(
readReplicas({
url: process.env.DATABASE_URL_REPLICA ?? process.env.DATABASE_URL!,
}),
)
.$extends({
query: {
async $allOperations({ operation, model, args, query }) {
if (
operation === 'create' ||
operation === 'update' ||
operation === 'delete'
) {
logger.info('Prisma operation', {
operation,
args,
});
}
return query(args);
},
},
});
return prisma;
};
const globalForPrisma = globalThis as unknown as {