Skip to content

Commit f50d60b

Browse files
authored
Merge pull request #957 from chiscookeke11/issue-832
Missing index on Stream.tokenAddress despite token filter in listStreams
2 parents 0d41728 + 907d3af commit f50d60b

6 files changed

Lines changed: 38 additions & 6 deletions

File tree

backend/.env.example

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
# Database
22
DATABASE_URL="postgresql://user:password@localhost:5433/flowfi?schema=public"
33

4+
# PostgreSQL pool settings
5+
# Maximum database connections per backend process (default: 10)
6+
PG_POOL_MAX=10
7+
# How long an idle connection stays open before being closed (milliseconds, default: 30000)
8+
PG_IDLE_TIMEOUT_MS=30000
9+
# How long to wait when establishing a new database connection (milliseconds, default: 5000)
10+
PG_CONNECTION_TIMEOUT_MS=5000
11+
# Maximum time a PostgreSQL statement may run before cancellation (milliseconds, default: 30000)
12+
PG_STATEMENT_TIMEOUT_MS=30000
13+
414
# Server
515
PORT=3001
616
NODE_ENV=development
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- CreateIndex
2+
CREATE INDEX IF NOT EXISTS "Stream_tokenAddress_idx" ON "Stream"("tokenAddress");

backend/prisma/schema.prisma

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ model Stream {
5151
5252
@@index([sender])
5353
@@index([recipient])
54+
@@index([tokenAddress])
5455
@@index([streamId])
5556
@@index([isActive])
5657
@@index([isPaused])

backend/prisma/seed.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import pg from 'pg';
21
import { PrismaPg } from '@prisma/adapter-pg';
32
import { PrismaClient } from '../src/generated/prisma/index.js';
3+
import { createPgPool } from '../src/lib/pg-pool.js';
44

5-
const connectionString = process.env.DATABASE_URL;
6-
const pool = new pg.Pool({ connectionString });
5+
const pool = createPgPool();
76
const adapter = new PrismaPg(pool);
87
const prisma = new PrismaClient({ adapter });
98

backend/src/lib/pg-pool.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import pg from 'pg';
2+
3+
const parsePositiveIntegerEnv = (name: string, defaultValue: number): number => {
4+
const rawValue = process.env[name];
5+
6+
if (!rawValue) return defaultValue;
7+
8+
const parsedValue = Number.parseInt(rawValue, 10);
9+
10+
return Number.isInteger(parsedValue) && parsedValue > 0 ? parsedValue : defaultValue;
11+
};
12+
13+
export const createPgPoolConfig = (): pg.PoolConfig => ({
14+
connectionString: process.env.DATABASE_URL,
15+
max: parsePositiveIntegerEnv('PG_POOL_MAX', 10),
16+
idleTimeoutMillis: parsePositiveIntegerEnv('PG_IDLE_TIMEOUT_MS', 30_000),
17+
connectionTimeoutMillis: parsePositiveIntegerEnv('PG_CONNECTION_TIMEOUT_MS', 5_000),
18+
statement_timeout: parsePositiveIntegerEnv('PG_STATEMENT_TIMEOUT_MS', 30_000),
19+
});
20+
21+
export const createPgPool = () => new pg.Pool(createPgPoolConfig());

backend/src/lib/prisma.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
import pg from 'pg';
22
import { PrismaPg } from '@prisma/adapter-pg';
33
import { PrismaClient } from '../generated/prisma/index.js';
4+
import { createPgPool } from './pg-pool.js';
45

56
const globalForPrisma = global as unknown as {
67
prisma?: PrismaClient;
78
pool?: pg.Pool;
89
};
910

10-
const connectionString = process.env.DATABASE_URL;
11-
1211
if (!globalForPrisma.pool) {
13-
globalForPrisma.pool = new pg.Pool({ connectionString });
12+
globalForPrisma.pool = createPgPool();
1413
}
1514

1615
const adapter = new PrismaPg(globalForPrisma.pool);

0 commit comments

Comments
 (0)