diff --git a/bun.lock b/bun.lock index 58166dd..3f85694 100644 --- a/bun.lock +++ b/bun.lock @@ -20,6 +20,7 @@ "devDependencies": { "@libsql/client": "^0.18.0", "@types/node": "^26.6.5", + "@types/pg": "^8.23.1", "@types/react": "^19.3.0", "@types/react-dom": "^19.3.0", "typescript": "^7.0.2", @@ -153,6 +154,8 @@ "@types/node": ["@types/node@26.6.5", "", { "dependencies": { "undici-types": "~8.9.0" } }, "sha512-uMzsv9stDdofIZBx4UGh4Z9ZgXnGC9seRDscilBcTHAeOFk67MZg/k9bWVfO+bk4qZmG+l52hRnLwUiH2LnJTg=="], + "@types/pg": ["@types/pg@8.23.1", "", { "dependencies": { "@types/node": "*", "pg-protocol": "*", "pg-types": "^2.2.0" } }, "sha512-fKVHpikPdg4GKks3JuLEhvwSyvwzF23hnabPy6DD8ljVbC7+6J5dQzdv4arV6jqq57djnMgs1HKBxX4P8aBI3A=="], + "@types/react": ["@types/react@19.3.0", "", { "dependencies": { "csstype": "^3.2.2" } }, "sha512-N0rFCuH9YoxG9/m61l9MfpJKfmLOVU0em7ipIz6TRgSSkvReLB9vL85GB+yr8Bs5leqpvg96JSwF4ZS1s4viQg=="], "@types/react-dom": ["@types/react-dom@19.3.0", "", { "peerDependencies": { "@types/react": "^19.3.0" } }, "sha512-ZI7bU42mZXXKHn/qNLEw2IrbiINU7X5+vfgdixBHkCNpYWXjKgfQ/P+uyGb5CjOLB9UcnTeg3rylQtV2hym44Q=="], diff --git a/lib/credits.ts b/lib/credits.ts index ce3b9d1..4204d78 100644 --- a/lib/credits.ts +++ b/lib/credits.ts @@ -15,6 +15,8 @@ import crypto from "node:crypto"; import { createRequire } from "node:module"; import { Credits, postgresStore as creditsPg, sqliteStore as creditsSqlite } from "@profullstack/redeem-credits"; import { Affiliates, postgresStore as affPg, sqliteStore as affSqlite } from "@profullstack/affiliates"; +import { connectionSettings } from "@profullstack/libsql-pg"; +import pg from "pg"; import { databaseUrl, db, ensureSchema, getAccountById, type Account } from "./db"; import { grantPaidDays, toMs } from "./plan"; import { sqlTime } from "./sql-time"; @@ -56,11 +58,11 @@ function rawConnection(): { kind: "pg"; pool: any } | { kind: "sqlite"; db: any const url = databaseUrl(); if (POSTGRES_URL.test(url)) { if (!_pool) { - const { Pool } = require_("pg"); // The same sslmode handling as the app's own client (libsql-pg), so both - // connections reach the database the same way. - const { connectionSettings } = require_("@profullstack/libsql-pg"); - _pool = new Pool({ ...connectionSettings(url), max: 4 }); + // connections reach the database the same way. A static import: libsql-pg + // is ESM and a server external, and require() of it from the Next bundle + // returned no connectionSettings (prod 500 "i is not a function"). + _pool = new pg.Pool({ ...connectionSettings(url), max: 4 }); _pool.on("error", () => {}); } return { kind: "pg", pool: _pool }; diff --git a/package.json b/package.json index e508c9f..752b02a 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ "devDependencies": { "@libsql/client": "^0.18.0", "@types/node": "^26.6.5", + "@types/pg": "^8.23.1", "@types/react": "^19.3.0", "@types/react-dom": "^19.3.0", "typescript": "^7.0.2"