Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .env.default
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ MIGRATE_FROM_OW=false
MIGRATION_WITH_LIMITS=true

# Logging
LOG_LEVEL=debug
LOG_LEVEL=info
LOG_TO_CONSOLE=true
LOG_TO_FILE=true
LOG_TO_FOLDER=true
LOG_MAX_FILES=365
LOG_MAX_FILES=365d

# NextAuth
NEXTAUTH_URL="http://localhost"
Expand Down
1 change: 1 addition & 0 deletions docker-compose.base.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ services:
LOG_LEVEL: ${LOG_LEVEL}
LOG_TO_CONSOLE: ${LOG_TO_CONSOLE}
LOG_TO_FOLDER: ${LOG_TO_FOLDER}
LOG_TO_FILE: ${LOG_TO_FILE}
LOG_MAX_FILES: ${LOG_MAX_FILES}
NEXTAUTH_URL: ${NEXTAUTH_URL}
NEXTAUTH_SECRET: ${NEXTAUTH_SECRET}
Expand Down
111 changes: 101 additions & 10 deletions src/prisma/client.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,109 @@
import { PrismaPg } from '@prisma/adapter-pg'
import logger from '@/lib/logger'
import { PrismaClient } from '@/prisma-generated-pn-client'
import { PrismaPg } from '@prisma/adapter-pg'

// To prevent hot reloading from creating new instances of PrismaClient it is stored in the global object.
// Read more about it in the section "Prevent hot reloading from creating new instances of PrismaClient" here:
// Read more about it in the section 'Prevent hot reloading from creating new instances of PrismaClient' here:
// https://www.prisma.io/docs/orm/prisma-client/setup-and-configuration/databases-connections

function newPrisma() {
const prisma = new PrismaClient({
log: [
{
emit: 'event',
level: 'query',
},
{
emit: 'event',
level: 'error',
},
{
emit: 'event',
level: 'info',
},
{
emit: 'event',
level: 'warn',
},
],
adapter: new PrismaPg(
{ connectionString: process.env.DB_URI },
{ schema: process.env.DB_SCHEMA },
)
})

prisma.$on('query', (e) => {
if (e.query.includes('SELECT')) { // Sets select query to debug as it generates an unreasonable amount of logs
logger.log({
level: 'debug',
message:
`${e.timestamp}
Type: Query
Params: ${e.params}
Query: ${e.query}
Duration: ${e.duration}
Target: ${e.target}
`,
})
} else {
logger.log({
level: 'info',
message:
`${e.timestamp}
Type: Query
Params: ${e.params}
Query: ${e.query}
Duration: ${e.duration}
Target: ${e.target}
`,
})
}
})

prisma.$on('error', (e) => {
logger.log({
level: 'error',
message:
`${e.timestamp}
Type: Error
Message: ${e.message}
Target: ${e.target}
`,
})
})

prisma.$on('info', (e) => {
logger.log({
level: 'info',
message:
`${e.timestamp}
Type: Info
Message: ${e.message}
Target: ${e.target}
`,
})
})

prisma.$on('warn', (e) => {
logger.log({
level: 'warn',
message:
`${e.timestamp}
Type: Info
Message: ${e.message}
Target: ${e.target}
`,
})
})

return prisma
}

// This is how the Prisma docs recommend doing it
const globalForPrisma = global as unknown as { prisma: PrismaClient }

export const prisma = globalForPrisma.prisma || new PrismaClient({
adapter: new PrismaPg(
{ connectionString: process.env.DB_URI },
{ schema: process.env.DB_SCHEMA },
)
})
const globalForPrisma = global as unknown as {
prisma: ReturnType<typeof newPrisma>
}

export const prisma = globalForPrisma.prisma || newPrisma()

if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = prisma
Loading