commit
4047a7ec23
378 changed files with 29334 additions and 0 deletions
33
lib/db.js
Normal file
33
lib/db.js
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
import { PrismaClient } from '@prisma/client';
|
||||
import chalk from 'chalk';
|
||||
|
||||
const options = {
|
||||
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;
|
||||
Loading…
Add table
Add a link
Reference in a new issue