mirror of
https://github.com/kremalicious/umami.git
synced 2024-11-16 02:05:04 +01:00
34 lines
583 B
JavaScript
34 lines
583 B
JavaScript
import { PrismaClient } from '@prisma/client';
|
|
import chalk from 'chalk';
|
|
|
|
const options = {
|
|
log: [
|
|
{
|
|
emit: 'event',
|
|
level: 'query',
|
|
},
|
|
],
|
|
};
|
|
|
|
function logQuery(e) {
|
|
console.log(chalk.yellow(e.params), '->', e.query, chalk.greenBright(`${e.duration}ms`));
|
|
}
|
|
|
|
let prisma;
|
|
|
|
if (process.env.NODE_ENV === 'production') {
|
|
prisma = new PrismaClient(options);
|
|
} else {
|
|
if (!global.prisma) {
|
|
global.prisma = new PrismaClient(options);
|
|
}
|
|
|
|
prisma = global.prisma;
|
|
}
|
|
|
|
if (process.env.LOG_QUERY) {
|
|
prisma.$on('query', logQuery);
|
|
}
|
|
|
|
export default prisma;
|