umami/lib/db.js

41 lines
742 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) {
if (process.env.LOG_QUERY) {
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);
prisma.$on('query', logQuery);
} else {
if (!global.prisma) {
global.prisma = new PrismaClient(options);
global.prisma.$on('query', logQuery);
}
prisma = global.prisma;
}
export default prisma;
2020-07-25 02:00:56 +02:00
2020-07-17 10:03:38 +02:00
export async function runQuery(query) {
2020-08-19 09:01:07 +02:00
return query.catch(e => {
console.error(e);
2020-08-25 08:49:14 +02:00
throw e;
2020-08-19 09:01:07 +02:00
});
2020-07-17 10:03:38 +02:00
}