Skip to content

Commit 2d82a30

Browse files
update prisma and docs
1 parent d07ce67 commit 2d82a30

11 files changed

Lines changed: 176 additions & 94 deletions

File tree

‎README.md‎

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,57 @@ datasource db {
140140

141141
Then run:
142142
```bash
143-
npm run prisma:generate # Regenerate Prisma Client
144-
npm run db:push # Push schema to new database
143+
# ------------------------------
144+
# Prisma Client & Schema
145+
# ------------------------------
146+
147+
# Regenerate Prisma Client after schema changes
148+
npm run prisma:generate
149+
150+
# Pull the current database schema into Prisma (update schema.prisma)
151+
npm run prisma:pull
152+
153+
# Push the Prisma schema to the database (without migration)
154+
npm run prisma:push
155+
156+
# Open Prisma Studio (GUI to view/edit database)
157+
npm run prisma:studio
158+
159+
# Format Prisma schema file
160+
npm run prisma:format
161+
162+
# Validate Prisma schema for errors
163+
npm run prisma:validate
164+
165+
# ------------------------------
166+
# Database seeding & syncing
167+
# ------------------------------
168+
169+
# Pull data (custom seed script)
170+
npm run db:pull
171+
172+
# Push data (custom seed script)
173+
npm run db:push
174+
175+
# Sync data (custom seed script)
176+
npm run db:sync
177+
178+
# Seed the database with initial data
179+
npm run db:seed
180+
181+
# ------------------------------
182+
# Migrations
183+
# ------------------------------
184+
185+
# Create and apply migrations (development)
186+
npm run migrate:dev
187+
188+
# Apply migrations in production
189+
npm run migrate:deploy
190+
191+
# Drop database and reapply all migrations (destructive)
192+
npm run migrate:reset
193+
145194
```
146195

147196
**Note**: Some schema features may vary between databases. Refer to [Prisma Database Documentation](https://www.prisma.io/docs/orm/reference/supported-databases) for database-specific considerations.

‎api/package-lock.json‎

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎api/package.json‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@
2222
"db:push": "node prisma/seed.js push",
2323
"db:sync": "node prisma/seed.js sync",
2424
"db:seed": "node prisma/seed.js seed",
25+
"migrate:dev": "prisma migrate dev",
26+
"migrate:deploy": "prisma migrate deploy",
27+
"migrate:reset": "prisma migrate reset",
28+
"studio": "prisma studio",
29+
"prisma:format": "prisma format",
30+
"prisma:validate": "prisma validate",
2531
"test": "jest",
2632
"lint": "eslint ."
2733
},
@@ -72,6 +78,7 @@
7278
"winston-daily-rotate-file": "^4.7.1"
7379
},
7480
"devDependencies": {
81+
"@types/node": "^24.10.1",
7582
"eslint": "^8.56.0",
7683
"jest": "^29.7.0",
7784
"nodemon": "^3.0.2",

‎api/prisma.config.ts‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
// This file was generated by Prisma and assumes you have installed the following:
2-
// npm install --save-dev prisma dotenv
3-
import "dotenv/config";
4-
import { defineConfig, env } from "prisma/config";
1+
import { defineConfig } from "prisma/config";
2+
import dotenv from "dotenv";
3+
4+
dotenv.config();
5+
6+
const databaseUrl = process.env.DATABASE_URL;
7+
if (!databaseUrl) throw new Error("DATABASE_URL is missing");
58

69
export default defineConfig({
710
schema: "prisma/schema.prisma",
8-
migrations: {
9-
path: "prisma/migrations",
10-
},
11+
migrations: { path: "prisma/migrations" },
1112
datasource: {
12-
url: env("DATABASE_URL"),
13+
url: databaseUrl,
1314
},
1415
});

‎api/prisma/migrations/20251124184817_init_schema/migration.sql‎

Lines changed: 0 additions & 6 deletions
This file was deleted.

‎api/prisma/migrations/migration_lock.toml‎

Lines changed: 0 additions & 4 deletions
This file was deleted.

‎api/prisma/schema.prisma‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ generator client {
77

88
datasource db {
99
provider = "postgresql"
10-
// Connection URL is configured in prisma.config.ts
1110
}
1211

1312
// User Model - Basic structure for relationships

‎api/prisma/seed.js‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ async function pushSchema() {
4242
try {
4343
logger.info('Pushing schema to database...');
4444

45+
4546
// Use Prisma db push to sync schema
4647
execSync('npx prisma db push', {
4748
stdio: 'inherit',
@@ -59,6 +60,15 @@ async function pushSchema() {
5960
logger.info('Prisma Client generated successfully');
6061
} catch (error) {
6162
logger.error('Failed to push schema', { error: error.message });
63+
64+
// Provide helpful error message for Supabase pooler issue
65+
if (error.message && error.message.includes('prepared statement')) {
66+
logger.error('');
67+
logger.error(' Update DATABASE_URL in your .env file to use direct connection:');
68+
logger.error(' DATABASE_URL=postgresql://postgres:password@db.xxxxx.supabase.co:5432/postgres');
69+
logger.error(' (Use port 5432 for direct connection, not 6543 for pooler)');
70+
}
71+
6272
throw error;
6373
}
6474
}

‎api/src/db/README.md‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ DATABASE_NAME=dbname
4545
DATABASE_USER=user
4646
DATABASE_PASSWORD=password
4747
48+
# IMPORTANT: For Supabase or connection poolers, use DIRECT connection in DATABASE_URL
49+
# Pooler connections (port 6543) don't support prepared statements required by Prisma migrations
50+
# Use direct connection (port 5432) instead
51+
4852
# Optional PostgreSQL settings
4953
DATABASE_SSL=false
5054
DB_POOL_MAX=20
@@ -53,6 +57,46 @@ DB_POOL_IDLE_TIMEOUT=30000
5357
DB_POOL_CONNECTION_TIMEOUT=10000
5458
```
5559

60+
## Supabase & Connection Pooler Configuration
61+
62+
### Issue: Prepared Statement Error with Poolers
63+
64+
If you're using **Supabase** or other connection poolers (like PgBouncer), you may encounter this error when running migrations:
65+
66+
```
67+
ERROR: prepared statement "s1" already exists
68+
```
69+
70+
This happens because:
71+
- Connection poolers don't support prepared statements
72+
- Prisma migrations require prepared statements
73+
- Pooler connections reuse statement names causing conflicts
74+
75+
### Solution: Use Direct Connection in DATABASE_URL
76+
77+
**For Supabase:**
78+
79+
1. **Get your direct connection URL** (not pooler):
80+
- Go to Supabase Dashboard → Project Settings → Database
81+
- Use the **Connection string** under "Connection pooling" → "Direct connection"
82+
- It should use port **5432** (not 6543 for pooler)
83+
84+
2. **Update your `.env` file:**
85+
```env
86+
# Use DIRECT connection (required for Prisma migrations)
87+
# Port 5432 for direct connection, NOT 6543 for pooler
88+
DATABASE_URL=postgresql://postgres:password@db.xxxxx.supabase.co:5432/postgres
89+
```
90+
91+
3. **For other poolers:**
92+
- Use the direct database connection URL (bypassing the pooler)
93+
- Update `DATABASE_URL` to point directly to your PostgreSQL instance
94+
95+
**Note:**
96+
- Use **direct connection** (port 5432) in `DATABASE_URL` for Prisma to work
97+
- Pooler connections (port 6543) will cause migration failures
98+
- Direct connection works for both runtime and migrations
99+
56100
## Usage
57101

58102
### Basic Usage
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Build connection string from individual components or use provided URL
3+
* @returns {string} PostgreSQL connection string
4+
*/
5+
function buildConnectionString() {
6+
// If DATABASE_URL is provided, use it
7+
if (process.env.DATABASE_URL) {
8+
return process.env.DATABASE_URL;
9+
}
10+
11+
// Build from individual components
12+
const host = process.env.DATABASE_HOST || 'nodejs-backend-db';
13+
const port = process.env.DATABASE_PORT || 5432;
14+
const database = process.env.DATABASE_NAME || 'postgres';
15+
const user = process.env.DATABASE_USER || 'postgres';
16+
const password = process.env.DATABASE_PASSWORD || 'postgres';
17+
18+
// URL encode password to handle special characters
19+
const encodedPassword = encodeURIComponent(password);
20+
21+
return `postgresql://${user}:${encodedPassword}@${host}:${port}/${database}?sslmode=require`;
22+
}
23+
24+
/**
25+
* Get connection string with validation and warnings
26+
* For Supabase: Automatically detects pooler and suggests direct connection
27+
* @returns {string} PostgreSQL connection string
28+
*/
29+
function getConnectionString() {
30+
const connectionString = buildConnectionString();
31+
return connectionString;
32+
}
33+
34+
module.exports = {
35+
buildConnectionString,
36+
getConnectionString
37+
};
38+

0 commit comments

Comments
 (0)