refactor postgres functions to not rely on the packages

This commit is contained in:
dswbx
2025-11-05 10:21:35 +01:00
parent 108c108d82
commit a333d537b0
7 changed files with 73 additions and 151 deletions

View File

@@ -1,6 +1,8 @@
import { describe, beforeAll, afterAll, expect, test, it, afterEach } from "bun:test";
import { describe, beforeAll, afterAll, test } from "bun:test";
import type { PostgresConnection } from "data/connection/postgres";
import { createApp, em, entity, text, pg, postgresJs } from "bknd";
import { pg, postgresJs } from "bknd";
import { Pool } from "pg";
import postgres from 'postgres'
import { disableConsoleLog, enableConsoleLog, $waitUntil } from "bknd/utils";
import { $ } from "bun";
import { connectionTestSuite } from "data/connection/connection-test-suite";
@@ -26,7 +28,7 @@ async function cleanDatabase(connection: InstanceType<typeof PostgresConnection>
async function isPostgresRunning() {
try {
// Try to actually connect to PostgreSQL
const conn = pg(credentials);
const conn = pg(new Pool(credentials));
await conn.ping();
await conn.close();
return true;
@@ -56,8 +58,8 @@ describe("postgres", () => {
});
describe.serial.each([
["pg", () => pg(credentials)],
["postgresjs", () => postgresJs(credentials)],
["pg", () => pg(new Pool(credentials))],
["postgresjs", () => postgresJs(postgres(credentials))],
])("%s", (name, createConnection) => {
connectionTestSuite(
{

View File

@@ -99,6 +99,7 @@
"@testing-library/jest-dom": "^6.6.3",
"@testing-library/react": "^16.2.0",
"@types/node": "^24.10.0",
"@types/pg": "^8.15.6",
"@types/react": "^19.0.10",
"@types/react-dom": "^19.0.4",
"@vitejs/plugin-react": "^5.1.0",
@@ -110,15 +111,17 @@
"jotai": "^2.12.2",
"jsdom": "^26.1.0",
"kysely-generic-sqlite": "^1.2.1",
"kysely-postgres-js": "^2.0.0",
"kysely-postgres-js": "^2.0.0",
"libsql": "^0.5.22",
"libsql-stateless-easy": "^1.8.0",
"miniflare": "^4.20251011.2",
"open": "^10.2.0",
"openapi-types": "^12.1.3",
"pg": "^8.16.3",
"postcss": "^8.5.3",
"postcss-preset-mantine": "^1.18.0",
"postcss-simple-vars": "^7.0.1",
"postgres": "^3.4.7",
"posthog-js-lite": "^3.6.0",
"react": "^19.0.0",
"react-dom": "^19.0.0",
@@ -143,20 +146,10 @@
"optionalDependencies": {
"@hono/node-server": "^1.19.6"
},
"peerDependencies": {
"react": ">=19",
"react-dom": ">=19",
"pg": "*",
"postgres": "*"
},
"peerDependenciesMeta": {
"pg": {
"optional": true
},
"postgres": {
"optional": true
}
},
"peerDependencies": {
"react": ">=19",
"react-dom": ">=19"
},
"main": "./dist/index.js",
"module": "./dist/index.js",
"types": "./dist/types/index.d.ts",

View File

@@ -2,15 +2,12 @@ import { Kysely, PostgresDialect } from "kysely";
import { PostgresIntrospector } from "./PostgresIntrospector";
import { PostgresConnection, plugins } from "./PostgresConnection";
import { customIntrospector } from "../Connection";
import $pg from "pg";
import type { Pool } from "pg";
export type PgPostgresConnectionConfig = $pg.PoolConfig;
export class PgPostgresConnection extends PostgresConnection<$pg.Pool> {
export class PgPostgresConnection extends PostgresConnection<Pool> {
override name = "pg";
constructor(config: PgPostgresConnectionConfig) {
const pool = new $pg.Pool(config);
constructor(pool: Pool) {
const kysely = new Kysely({
dialect: customIntrospector(PostgresDialect, PostgresIntrospector, {
excludeTables: [],
@@ -27,6 +24,6 @@ export class PgPostgresConnection extends PostgresConnection<$pg.Pool> {
}
}
export function pg(config: PgPostgresConnectionConfig): PgPostgresConnection {
return new PgPostgresConnection(config);
export function pg(pool: Pool): PgPostgresConnection {
return new PgPostgresConnection(pool);
}

View File

@@ -3,14 +3,12 @@ import { PostgresIntrospector } from "./PostgresIntrospector";
import { PostgresConnection, plugins } from "./PostgresConnection";
import { customIntrospector } from "../Connection";
import { PostgresJSDialect } from "kysely-postgres-js";
import $postgresJs, { type Sql, type Options, type PostgresType } from "postgres";
import type { Sql } from "postgres";
export type PostgresJsConfig = Options<Record<string, PostgresType>>;
export class PostgresJsConnection extends PostgresConnection<$postgresJs.Sql> {
export class PostgresJsConnection extends PostgresConnection<Sql> {
override name = "postgres-js";
constructor(opts: { postgres: $postgresJs.Sql }) {
constructor(opts: { postgres: Sql }) {
const kysely = new Kysely({
dialect: customIntrospector(PostgresJSDialect, PostgresIntrospector, {
excludeTables: [],
@@ -28,14 +26,7 @@ export class PostgresJsConnection extends PostgresConnection<$postgresJs.Sql> {
}
export function postgresJs(
connectionString: string,
config?: PostgresJsConfig,
): PostgresJsConnection;
export function postgresJs(config: PostgresJsConfig): PostgresJsConnection;
export function postgresJs(
first: PostgresJsConfig | string,
second?: PostgresJsConfig,
postgres: Sql,
): PostgresJsConnection {
const postgres = typeof first === "string" ? $postgresJs(first, second) : $postgresJs(first);
return new PostgresJsConnection({ postgres });
}

View File

@@ -156,14 +156,12 @@ export { SqliteLocalConnection } from "data/connection/sqlite/SqliteLocalConnect
export {
pg,
PgPostgresConnection,
type PgPostgresConnectionConfig,
} from "data/connection/postgres/PgPostgresConnection";
export { PostgresIntrospector } from "data/connection/postgres/PostgresIntrospector";
export { PostgresConnection } from "data/connection/postgres/PostgresConnection";
export {
postgresJs,
PostgresJsConnection,
type PostgresJsConfig,
} from "data/connection/postgres/PostgresJsConnection";
export {
createCustomPostgresConnection,