Skip to content

Commit d4e58e3

Browse files
authored
fix: pg connection string (#55)
* fix: pg connection string * fix: format
1 parent d1287db commit d4e58e3

12 files changed

Lines changed: 234 additions & 53 deletions

File tree

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
"next-plausible": "^3.12.5",
5858
"next-themes": "^0.4.4",
5959
"nuqs": "^2.3.0",
60-
"postgres": "^3.4.8",
6160
"react": "19.2.3",
6261
"react-day-picker": "^9.8.1",
6362
"react-dom": "19.2.3",

pnpm-lock.yaml

Lines changed: 181 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/app/drizzle/api/route.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ export async function GET(req: NextRequest): Promise<Response> {
100100
}
101101
}
102102

103-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
104103
async function getChartData(
105104
db: any,
106105
table: typeof logs,
@@ -144,12 +143,13 @@ async function getChartData(
144143
const intervalMs = evaluateIntervalMs(durationMs);
145144
const intervalSeconds = Math.max(1, Math.floor(intervalMs / 1000));
146145

147-
const result: {
146+
type ChartRow = {
148147
bucket: Date;
149148
success: number;
150149
warning: number;
151150
error: number;
152-
}[] = await db.execute(
151+
};
152+
const raw = await db.execute(
153153
sql`SELECT
154154
date_bin(${intervalSeconds + " seconds"}::interval, ${table.date}, ${minDate.toISOString()}::timestamptz) as bucket,
155155
COUNT(*) FILTER (WHERE ${table.level} = 'success')::int as success,
@@ -160,6 +160,9 @@ async function getChartData(
160160
GROUP BY bucket
161161
ORDER BY bucket ASC`,
162162
);
163+
const result: ChartRow[] = Array.isArray(raw)
164+
? raw
165+
: (raw as { rows: ChartRow[] }).rows;
163166

164167
return result.map((r) => ({
165168
timestamp: new Date(r.bucket).getTime(),

src/content/guide/06-drizzle-orm.mdx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,21 +63,24 @@ export const logs = pgTable("logs", {
6363
});
6464
```
6565

66-
Set up the database client. Use `prepare: false` when connecting through a pooler (Supabase transaction mode, PgBouncer, Neon):
66+
Set up the database client using the `node-postgres` driver:
6767

6868
```ts
6969
// db/drizzle/index.ts
70-
import { drizzle } from "drizzle-orm/postgres-js";
71-
import postgres from "postgres";
70+
import { drizzle } from "drizzle-orm/node-postgres";
7271
import * as schema from "./schema";
7372

74-
const client = postgres(process.env.DATABASE_URL!, {
75-
prepare: false, // Required for connection poolers
76-
});
73+
const connectionString = process.env.DATABASE_URL;
74+
75+
if (!connectionString) {
76+
throw new Error("DATABASE_URL environment variable is not set");
77+
}
7778

78-
export const db = drizzle(client, { schema });
79+
export const db = drizzle(connectionString, { schema });
7980
```
8081

82+
> **Note:** If you're using a connection pooler (Supabase transaction mode, PgBouncer, Neon), make sure to use the transaction pooler URL (port `6543` for Supabase). Install `pg` as a dependency: `pnpm add pg @types/pg`.
83+
8184
### Step 2: Column Mapping
8285

8386
The column mapping bridges your table schema keys to Drizzle columns. This is the **only file that couples UI to DB** — one file per table:

src/db/drizzle/drizzle.config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ export default defineConfig({
88
dbCredentials: {
99
url: process.env.DATABASE_URL!,
1010
},
11+
verbose: true,
12+
strict: true,
1113
});

src/db/drizzle/index.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { drizzle } from "drizzle-orm/postgres-js";
2-
import postgres from "postgres";
1+
import { drizzle } from "drizzle-orm/node-postgres";
32
import * as schema from "./schema";
43

54
const connectionString = process.env.DATABASE_URL;
@@ -8,8 +7,4 @@ if (!connectionString) {
87
throw new Error("DATABASE_URL environment variable is not set");
98
}
109

11-
const client = postgres(connectionString, {
12-
prepare: false, // Required for serverless / connection poolers (PgBouncer, Neon, Supabase)
13-
});
14-
15-
export const db = drizzle(client, { schema });
10+
export const db = drizzle(connectionString, { schema });

0 commit comments

Comments
 (0)