Display page views and unique visitors.
This commit is contained in:
15
lib/db.js
15
lib/db.js
@@ -1,4 +1,5 @@
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
import chalk from 'chalk';
|
||||
|
||||
const options = {
|
||||
log: [
|
||||
@@ -9,13 +10,21 @@ const options = {
|
||||
],
|
||||
};
|
||||
|
||||
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;
|
||||
@@ -23,12 +32,6 @@ if (process.env.NODE_ENV === 'production') {
|
||||
|
||||
export default prisma;
|
||||
|
||||
prisma.on('query', e => {
|
||||
if (process.env.LOG_QUERY) {
|
||||
console.log(`${e.params} -> ${e.query} (${e.duration}ms)`);
|
||||
}
|
||||
});
|
||||
|
||||
export async function runQuery(query) {
|
||||
return query.catch(e => {
|
||||
console.error(e);
|
||||
|
||||
Reference in New Issue
Block a user