umami/lib/db.js

34 lines
583 B
JavaScript
Raw Normal View History

2020-07-17 10:03:38 +02:00
import { PrismaClient } from '@prisma/client';
import chalk from 'chalk';
2020-07-17 10:03:38 +02:00
const options = {
2020-07-25 02:00:56 +02:00
log: [
{
emit: 'event',
level: 'query',
},
],
};
function logQuery(e) {
2022-06-19 09:07:01 +02:00
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;
}
2022-06-19 09:07:01 +02:00
if (process.env.LOG_QUERY) {
prisma.$on('query', logQuery);
}
export default prisma;