Skip to content
Merged
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
10 changes: 10 additions & 0 deletions backend/.env.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
# Database
DATABASE_URL="postgresql://user:password@localhost:5433/flowfi?schema=public"

# PostgreSQL pool settings
# Maximum database connections per backend process (default: 10)
PG_POOL_MAX=10
# How long an idle connection stays open before being closed (milliseconds, default: 30000)
PG_IDLE_TIMEOUT_MS=30000
# How long to wait when establishing a new database connection (milliseconds, default: 5000)
PG_CONNECTION_TIMEOUT_MS=5000
# Maximum time a PostgreSQL statement may run before cancellation (milliseconds, default: 30000)
PG_STATEMENT_TIMEOUT_MS=30000

# Server
PORT=3001
NODE_ENV=development
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- CreateIndex
CREATE INDEX IF NOT EXISTS "Stream_tokenAddress_idx" ON "Stream"("tokenAddress");
1 change: 1 addition & 0 deletions backend/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ model Stream {

@@index([sender])
@@index([recipient])
@@index([tokenAddress])
@@index([streamId])
@@index([isActive])
@@index([isPaused])
Expand Down
5 changes: 2 additions & 3 deletions backend/prisma/seed.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import pg from 'pg';
import { PrismaPg } from '@prisma/adapter-pg';
import { PrismaClient } from '../src/generated/prisma/index.js';
import { createPgPool } from '../src/lib/pg-pool.js';

const connectionString = process.env.DATABASE_URL;
const pool = new pg.Pool({ connectionString });
const pool = createPgPool();
const adapter = new PrismaPg(pool);
const prisma = new PrismaClient({ adapter });

Expand Down
21 changes: 21 additions & 0 deletions backend/src/lib/pg-pool.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import pg from 'pg';

const parsePositiveIntegerEnv = (name: string, defaultValue: number): number => {
const rawValue = process.env[name];

if (!rawValue) return defaultValue;

const parsedValue = Number.parseInt(rawValue, 10);

return Number.isInteger(parsedValue) && parsedValue > 0 ? parsedValue : defaultValue;
};

export const createPgPoolConfig = (): pg.PoolConfig => ({
connectionString: process.env.DATABASE_URL,
max: parsePositiveIntegerEnv('PG_POOL_MAX', 10),
idleTimeoutMillis: parsePositiveIntegerEnv('PG_IDLE_TIMEOUT_MS', 30_000),
connectionTimeoutMillis: parsePositiveIntegerEnv('PG_CONNECTION_TIMEOUT_MS', 5_000),
statement_timeout: parsePositiveIntegerEnv('PG_STATEMENT_TIMEOUT_MS', 30_000),
});

export const createPgPool = () => new pg.Pool(createPgPoolConfig());
5 changes: 2 additions & 3 deletions backend/src/lib/prisma.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import pg from 'pg';
import { PrismaPg } from '@prisma/adapter-pg';
import { PrismaClient } from '../generated/prisma/index.js';
import { createPgPool } from './pg-pool.js';

const globalForPrisma = global as unknown as {
prisma?: PrismaClient;
pool?: pg.Pool;
};

const connectionString = process.env.DATABASE_URL;

if (!globalForPrisma.pool) {
globalForPrisma.pool = new pg.Pool({ connectionString });
globalForPrisma.pool = createPgPool();
}

const adapter = new PrismaPg(globalForPrisma.pool);
Expand Down
Loading