File tree Expand file tree Collapse file tree 4 files changed +41
-0
lines changed
migrations/20250821055439_add_indexes Expand file tree Collapse file tree 4 files changed +41
-0
lines changed Original file line number Diff line number Diff line change
1
+ -- CreateIndex
2
+ CREATE INDEX "AlertMethod_userId_idx " ON " AlertMethod" (" userId" );
3
+
4
+ -- CreateIndex
5
+ CREATE INDEX "Site_userId_idx " ON " Site" (" userId" );
6
+
7
+ -- CreateIndex
8
+ CREATE INDEX "Site_detectionGeometry_idx " ON " Site" USING GIST (" detectionGeometry" );
9
+
10
+ -- CreateIndex
11
+ CREATE INDEX "SiteAlert_isProcessed_eventDate_idx " ON " SiteAlert" (" isProcessed" , " eventDate" );
Original file line number Diff line number Diff line change @@ -60,6 +60,7 @@ model AlertMethod {
60
60
verificationRequest VerificationRequest ?
61
61
62
62
@@unique ([destination , userId , method ] )
63
+ @@index ([userId ] )
63
64
}
64
65
65
66
model Site {
@@ -85,6 +86,9 @@ model Site {
85
86
project Project ? @relation (fields : [projectId ] , references : [id ] )
86
87
user User ? @relation (fields : [userId ] , references : [id ] , onDelete : Cascade )
87
88
siteRelations SiteRelation []
89
+
90
+ @@index ([userId ] )
91
+ @@index ([detectionGeometry ] , type : Gist )
88
92
}
89
93
90
94
model Project {
@@ -141,6 +145,8 @@ model SiteAlert {
141
145
data Json ?
142
146
notifications Notification []
143
147
site Site @relation (fields : [siteId ] , references : [id ] , onDelete : Cascade )
148
+
149
+ @@index ([isProcessed , eventDate ] )
144
150
}
145
151
146
152
model Notification {
Original file line number Diff line number Diff line change @@ -60,9 +60,14 @@ const server = z.object({
60
60
ALERT_SMS_DISABLED : coerceBooleanWithDefault ( true ) ,
61
61
ALERT_WHATSAPP_DISABLED : coerceBooleanWithDefault ( true ) ,
62
62
63
+
64
+ // Database slow query logging. Disabled by default.
65
+ DATABASE_LOG_SLOWQUERY : coerceBooleanWithDefault ( false ) ,
66
+
63
67
// Notification batch size for processing notifications in batches. Defaults to 10.
64
68
NOTIFICATION_BATCH_SIZE : z . string ( ) . default ( '10' ) ,
65
69
70
+
66
71
// API caching configuration. Enabled by default in production, disabled in development.
67
72
PUBLIC_API_CACHING : z
68
73
. union ( [ z . literal ( 'true' ) , z . literal ( 'false' ) ] )
@@ -118,6 +123,7 @@ const processEnv = {
118
123
WHATSAPP_ENDPOINT_AUTH_TOKEN : process . env . WHATSAPP_ENDPOINT_AUTH_TOKEN ,
119
124
ALERT_SMS_DISABLED : process . env . ALERT_SMS_DISABLED ,
120
125
ALERT_WHATSAPP_DISABLED : process . env . ALERT_WHATSAPP_DISABLED ,
126
+ DATABASE_LOG_SLOWQUERY : process . env . DATABASE_LOG_SLOWQUERY ,
121
127
PUBLIC_API_CACHING : process . env . PUBLIC_API_CACHING ,
122
128
NOTIFICATION_BATCH_SIZE : process . env . NOTIFICATION_BATCH_SIZE ,
123
129
} ;
Original file line number Diff line number Diff line change 1
1
import { PrismaClient } from '@prisma/client' ;
2
2
3
3
import { env } from '../env.mjs' ;
4
+ import { logger } from './logger' ;
4
5
5
6
const globalForPrisma = globalThis as unknown as { prisma : PrismaClient } ;
6
7
@@ -10,6 +11,23 @@ export const prisma =
10
11
log : env . NODE_ENV === 'development' ? [ 'error' ] : [ 'error' ] ,
11
12
} ) ;
12
13
14
+ // Add performance monitoring middleware (only if enabled via environment variable)
15
+ if ( env . DATABASE_LOG_SLOWQUERY ) {
16
+ prisma . $use ( async ( params , next ) => {
17
+ const start = Date . now ( ) ;
18
+ const result = await next ( params ) ;
19
+ const duration = Date . now ( ) - start ;
20
+
21
+ // Log queries that take longer than 1000ms
22
+ if ( duration > 1000 ) {
23
+ const logMessage = `Slow query detected - Model: ${ params . model } , Action: ${ params . action } , Duration: ${ duration } ms` ;
24
+ logger ( logMessage , 'warn' ) ;
25
+ }
26
+
27
+ return result ;
28
+ } ) ;
29
+ }
30
+
13
31
if ( env . NODE_ENV !== 'production' ) {
14
32
globalForPrisma . prisma = prisma ;
15
33
}
You can’t perform that action at this time.
0 commit comments