From d938596efe30e43421c8f775a79912b3361b1908 Mon Sep 17 00:00:00 2001 From: keyan Date: Thu, 31 Mar 2022 13:05:11 -0500 Subject: [PATCH] log query performance and reduce full item resolver calls --- api/models/index.js | 15 ++++++++++++--- api/resolvers/item.js | 3 +++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/api/models/index.js b/api/models/index.js index fa66c1e6..fce42d8a 100644 --- a/api/models/index.js +++ b/api/models/index.js @@ -1,7 +1,16 @@ import { PrismaClient } from '@prisma/client' -global.prisma ||= new PrismaClient({ - log: ['warn', 'error'] -}) +if (!global.prisma) { + global.prisma = new PrismaClient({ + log: [{ level: 'query', emit: 'event' }, 'warn', 'error'] + }) + global.prisma.$on('query', (e) => { + if (e.duration > 50) { + console.log('Query: ' + e.query) + console.log('Params: ' + e.params) + console.log('Duration: ' + e.duration + 'ms') + } + }) +} export default global.prisma diff --git a/api/resolvers/item.js b/api/resolvers/item.js index ce8d3e18..76f83fac 100644 --- a/api/resolvers/item.js +++ b/api/resolvers/item.js @@ -672,6 +672,9 @@ export default { return count || 0 }, comments: async (item, args, { models }) => { + if (item.comments) { + return item.comments + } return comments(models, item.id, 'hot') }, sats: async (item, args, { models }) => {