Add PostgreSQL support via pg and node-postgres drizzle adapter
This commit is contained in:
@@ -8,6 +8,7 @@
|
||||
# directly (drizzle-kit, scripts). Keep both in step and keep them well below
|
||||
# MariaDB's `max_connections` (151 by default) or the emulator and any second
|
||||
# CMS instance get "Too many connections". README §performance: pool size 25.
|
||||
# DATABASE_URL supports both MySQL/MariaDB ("mysql://...") and PostgreSQL ("postgres://...").
|
||||
DATABASE_URL="mysql://user:password@localhost:3306/dbname?charset=utf8mb4&connection_limit=25&connect_timeout=5"
|
||||
DATABASE_POOL_SIZE=25
|
||||
DATABASE_IDLE_TIMEOUT_MS=60000
|
||||
|
||||
+5
-2
@@ -6,12 +6,15 @@ import { defineConfig } from "drizzle-kit";
|
||||
// This DB is shared with the Arcturus emulator — NEVER run `drizzle-kit migrate`
|
||||
// or `push` against it. CMS DDL stays in drizzle/migrations/*.sql applied by
|
||||
// `pnpm db:migrate`. Use `pnpm db:generate` only for draft SQL under drizzle/drafts/.
|
||||
const dbUrl = process.env.DATABASE_URL ?? "";
|
||||
const isPostgres = dbUrl.startsWith("postgres://") || dbUrl.startsWith("postgresql://");
|
||||
|
||||
export default defineConfig({
|
||||
dialect: "mysql",
|
||||
dialect: isPostgres ? "postgresql" : "mysql",
|
||||
schema: "./src/db/schema.ts",
|
||||
out: "./drizzle/drafts",
|
||||
dbCredentials: {
|
||||
url: process.env.DATABASE_URL ?? "",
|
||||
url: dbUrl,
|
||||
},
|
||||
strict: true,
|
||||
verbose: true,
|
||||
|
||||
@@ -79,6 +79,7 @@
|
||||
"next-auth": "5.0.0-beta.32",
|
||||
"next-intl": "4.14.9",
|
||||
"otplib": "13.5.0",
|
||||
"pg": "^8.23.1",
|
||||
"pino": "10.4.0",
|
||||
"react": "19.3.1",
|
||||
"react-dom": "19.3.1",
|
||||
@@ -100,6 +101,7 @@
|
||||
"@tailwindcss/postcss": "4.3.3",
|
||||
"@tailwindcss/typography": "0.5.20",
|
||||
"@types/node": "26.6.5",
|
||||
"@types/pg": "^8.23.1",
|
||||
"@types/react": "19.3.0",
|
||||
"@types/react-dom": "19.3.0",
|
||||
"@vitest/coverage-v8": "5.0.5",
|
||||
|
||||
Generated
+2502
-4394
File diff suppressed because it is too large
Load Diff
+62
-7
@@ -1,7 +1,9 @@
|
||||
import type { SQL } from "drizzle-orm";
|
||||
import { drizzle } from "drizzle-orm/mysql2";
|
||||
import { drizzle as drizzleMysql } from "drizzle-orm/mysql2";
|
||||
import { drizzle as drizzlePg } from "drizzle-orm/node-postgres";
|
||||
import type { ResultSetHeader } from "mysql2";
|
||||
import mysql from "mysql2/promise";
|
||||
import pg from "pg";
|
||||
import * as relations from "@/db/relations";
|
||||
import * as schema from "@/db/schema";
|
||||
import { env } from "@/env";
|
||||
@@ -16,7 +18,10 @@ const globalForDb = globalThis as unknown as {
|
||||
};
|
||||
|
||||
function createDb() {
|
||||
const url = new URL(env.DATABASE_URL);
|
||||
const urlStr = env.DATABASE_URL;
|
||||
const isPostgres =
|
||||
urlStr.startsWith("postgres://") || urlStr.startsWith("postgresql://");
|
||||
const url = new URL(urlStr);
|
||||
const isBuild =
|
||||
process.env.NEXT_PHASE === "phase-production-build" ||
|
||||
process.env.NEXT_PHASE === "phase-production-compile";
|
||||
@@ -26,6 +31,21 @@ function createDb() {
|
||||
? Math.min(env.DATABASE_CONNECT_TIMEOUT_MS, 5_000)
|
||||
: env.DATABASE_CONNECT_TIMEOUT_MS;
|
||||
|
||||
if (isPostgres) {
|
||||
const { Pool } = pg;
|
||||
const pool = new Pool({
|
||||
connectionString: urlStr,
|
||||
max: connectionLimit,
|
||||
idleTimeoutMillis: env.DATABASE_IDLE_TIMEOUT_MS,
|
||||
connectionTimeoutMillis: connectTimeout,
|
||||
});
|
||||
|
||||
return drizzlePg(instrumentDatabase(pool) as unknown as pg.Pool, {
|
||||
schema: fullSchema,
|
||||
logger: false,
|
||||
}) as unknown as ReturnType<typeof drizzleMysql>;
|
||||
}
|
||||
|
||||
const pool = mysql.createPool({
|
||||
host: url.hostname,
|
||||
port: Number(url.port) || 3306,
|
||||
@@ -46,7 +66,7 @@ function createDb() {
|
||||
supportBigNumbers: true,
|
||||
});
|
||||
|
||||
return drizzle(instrumentDatabase(pool), {
|
||||
return drizzleMysql(instrumentDatabase(pool), {
|
||||
schema: fullSchema,
|
||||
mode: "default",
|
||||
logger: false,
|
||||
@@ -58,8 +78,19 @@ export const db = globalForDb.db ?? createDb();
|
||||
if (env.NODE_ENV !== "production") globalForDb.db = db;
|
||||
|
||||
export function rowsFrom<T>(result: unknown): T[] {
|
||||
const [first] = result as [unknown, unknown];
|
||||
return (Array.isArray(first) ? first : []) as T[];
|
||||
if (!result) return [];
|
||||
if (Array.isArray(result)) {
|
||||
const [first] = result as [unknown, unknown];
|
||||
return (Array.isArray(first) ? first : []) as T[];
|
||||
}
|
||||
if (
|
||||
typeof result === "object" &&
|
||||
"rows" in result &&
|
||||
Array.isArray((result as { rows: unknown }).rows)
|
||||
) {
|
||||
return (result as { rows: T[] }).rows;
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
export async function queryRows<T>(query: SQL | string): Promise<T[]> {
|
||||
@@ -69,8 +100,32 @@ export async function queryRows<T>(query: SQL | string): Promise<T[]> {
|
||||
export async function execResult(
|
||||
query: SQL | string,
|
||||
): Promise<ResultSetHeader> {
|
||||
const [first] = (await db.execute(query)) as unknown as [unknown, unknown];
|
||||
return first as ResultSetHeader;
|
||||
const res = await db.execute(query);
|
||||
if (Array.isArray(res)) {
|
||||
const [first] = res as [unknown, unknown];
|
||||
return first as ResultSetHeader;
|
||||
}
|
||||
if (typeof res === "object" && res !== null) {
|
||||
const r = res as Record<string, unknown>;
|
||||
return {
|
||||
affectedRows: typeof r.rowCount === "number" ? r.rowCount : 0,
|
||||
insertId: typeof r.insertId === "number" ? r.insertId : 0,
|
||||
fieldCount: 0,
|
||||
info: "",
|
||||
serverStatus: 0,
|
||||
warningStatus: 0,
|
||||
changedRows: 0,
|
||||
} as ResultSetHeader;
|
||||
}
|
||||
return {
|
||||
affectedRows: 0,
|
||||
insertId: 0,
|
||||
fieldCount: 0,
|
||||
info: "",
|
||||
serverStatus: 0,
|
||||
warningStatus: 0,
|
||||
changedRows: 0,
|
||||
};
|
||||
}
|
||||
|
||||
export async function affectedRows(query: SQL | string): Promise<number> {
|
||||
|
||||
Reference in New Issue
Block a user