2020-07-17 10:03:38 +02:00
|
|
|
import { PrismaClient } from '@prisma/client';
|
2020-07-29 04:04:45 +02:00
|
|
|
import chalk from 'chalk';
|
2020-07-17 10:03:38 +02:00
|
|
|
|
2022-07-07 14:55:43 +02:00
|
|
|
BigInt.prototype.toJSON = function () {
|
2022-07-16 08:53:31 +02:00
|
|
|
return Number(this);
|
2022-07-07 14:55:43 +02:00
|
|
|
};
|
|
|
|
|
2020-07-28 10:17:45 +02:00
|
|
|
const options = {
|
2020-07-25 02:00:56 +02:00
|
|
|
log: [
|
|
|
|
{
|
|
|
|
emit: 'event',
|
|
|
|
level: 'query',
|
|
|
|
},
|
|
|
|
],
|
2020-07-28 10:17:45 +02:00
|
|
|
};
|
|
|
|
|
2020-07-29 04:04:45 +02:00
|
|
|
function logQuery(e) {
|
2022-06-19 09:07:01 +02:00
|
|
|
console.log(chalk.yellow(e.params), '->', e.query, chalk.greenBright(`${e.duration}ms`));
|
2020-07-29 04:04:45 +02:00
|
|
|
}
|
|
|
|
|
2022-07-16 08:53:31 +02:00
|
|
|
function getClient(options) {
|
|
|
|
const prisma = new PrismaClient(options);
|
2020-07-28 10:17:45 +02:00
|
|
|
|
2022-07-16 08:53:31 +02:00
|
|
|
if (process.env.LOG_QUERY) {
|
|
|
|
prisma.$on('query', logQuery);
|
2020-07-28 10:17:45 +02:00
|
|
|
}
|
|
|
|
|
2022-07-16 08:53:31 +02:00
|
|
|
return prisma;
|
2020-07-28 10:17:45 +02:00
|
|
|
}
|
|
|
|
|
2022-07-16 08:53:31 +02:00
|
|
|
const prisma = global.prisma || getClient(options);
|
|
|
|
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
global.prisma = prisma;
|
2022-06-19 09:07:01 +02:00
|
|
|
}
|
|
|
|
|
2020-07-28 10:17:45 +02:00
|
|
|
export default prisma;
|