Hello! I'm trying to use prisma.$on to log queries...
# orm-help
p
Hello! I'm trying to use prisma.$on to log queries, but it show me several requests/logs with the same timestamp. Is this normal?
r
@P V 👋 Are you performing the queries at different times? Also can you share how you have configured the
$on
handler?
👍 1
p
Nope, i'm using nextjs and i'm calling it this way:
export async function getServerSideProps({ params, res }) {
const users = await prisma.users.findMany({
select: {
id: true,
name: true
},
take: 10
});
return {
props: { users: JSON.parse(JSON.stringify(users)) },
}
}
$on handler:
import { PrismaClient } from '@prisma/client';
let prisma;
const config = {
log: [
{
emit: 'event',
level: 'query',
},
{
emit: 'stdout',
level: 'error',
},
{
emit: 'stdout',
level: 'info',
},
{
emit: 'stdout',
level: 'warn',
},
],
};
if (process.env.NODE_ENV === 'production') {
prisma = new PrismaClient(config);
} else {
if (!global.prisma) {
global.prisma = new PrismaClient(config);
}
prisma = global.prisma;
}
prisma.$on('query', (e) => {
console.log(e);
})
export default prisma;
When I built for production, this doesn't happening anymore. So, it's solved, thank you!