Skip to content

Commit 8544582

Browse files
authored
Merge pull request #233 from Plant-for-the-Planet-org/feature/add-indexes
Feature/add indexes
2 parents 729cd65 + 3467426 commit 8544582

File tree

4 files changed

+41
-0
lines changed

4 files changed

+41
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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");

apps/server/prisma/schema.prisma

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ model AlertMethod {
6060
verificationRequest VerificationRequest?
6161
6262
@@unique([destination, userId, method])
63+
@@index([userId])
6364
}
6465

6566
model Site {
@@ -85,6 +86,9 @@ model Site {
8586
project Project? @relation(fields: [projectId], references: [id])
8687
user User? @relation(fields: [userId], references: [id], onDelete: Cascade)
8788
siteRelations SiteRelation[]
89+
90+
@@index([userId])
91+
@@index([detectionGeometry], type: Gist)
8892
}
8993

9094
model Project {
@@ -141,6 +145,8 @@ model SiteAlert {
141145
data Json?
142146
notifications Notification[]
143147
site Site @relation(fields: [siteId], references: [id], onDelete: Cascade)
148+
149+
@@index([isProcessed, eventDate])
144150
}
145151

146152
model Notification {

apps/server/src/env.mjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,14 @@ const server = z.object({
6060
ALERT_SMS_DISABLED: coerceBooleanWithDefault(true),
6161
ALERT_WHATSAPP_DISABLED: coerceBooleanWithDefault(true),
6262

63+
64+
// Database slow query logging. Disabled by default.
65+
DATABASE_LOG_SLOWQUERY: coerceBooleanWithDefault(false),
66+
6367
// Notification batch size for processing notifications in batches. Defaults to 10.
6468
NOTIFICATION_BATCH_SIZE: z.string().default('10'),
6569

70+
6671
// API caching configuration. Enabled by default in production, disabled in development.
6772
PUBLIC_API_CACHING: z
6873
.union([z.literal('true'), z.literal('false')])
@@ -118,6 +123,7 @@ const processEnv = {
118123
WHATSAPP_ENDPOINT_AUTH_TOKEN: process.env.WHATSAPP_ENDPOINT_AUTH_TOKEN,
119124
ALERT_SMS_DISABLED: process.env.ALERT_SMS_DISABLED,
120125
ALERT_WHATSAPP_DISABLED: process.env.ALERT_WHATSAPP_DISABLED,
126+
DATABASE_LOG_SLOWQUERY: process.env.DATABASE_LOG_SLOWQUERY,
121127
PUBLIC_API_CACHING: process.env.PUBLIC_API_CACHING,
122128
NOTIFICATION_BATCH_SIZE: process.env.NOTIFICATION_BATCH_SIZE,
123129
};

apps/server/src/server/db.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {PrismaClient} from '@prisma/client';
22

33
import {env} from '../env.mjs';
4+
import {logger} from './logger';
45

56
const globalForPrisma = globalThis as unknown as {prisma: PrismaClient};
67

@@ -10,6 +11,23 @@ export const prisma =
1011
log: env.NODE_ENV === 'development' ? ['error'] : ['error'],
1112
});
1213

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+
1331
if (env.NODE_ENV !== 'production') {
1432
globalForPrisma.prisma = prisma;
1533
}

0 commit comments

Comments
 (0)