diff --git a/app/__test__/data/postgres.test.ts b/app/__test__/data/postgres.test.ts index ba089d1..f9016cd 100644 --- a/app/__test__/data/postgres.test.ts +++ b/app/__test__/data/postgres.test.ts @@ -28,7 +28,7 @@ async function cleanDatabase(connection: InstanceType async function isPostgresRunning() { try { // Try to actually connect to PostgreSQL - const conn = pg(new Pool(credentials)); + const conn = pg({ pool: new Pool(credentials) }); await conn.ping(); await conn.close(); return true; @@ -58,8 +58,8 @@ describe("postgres", () => { }); describe.serial.each([ - ["pg", () => pg(new Pool(credentials))], - ["postgresjs", () => postgresJs(postgres(credentials))], + ["pg", () => pg({ pool: new Pool(credentials) })], + ["postgresjs", () => postgresJs({ postgres: postgres(credentials) })], ])("%s", (name, createConnection) => { connectionTestSuite( { diff --git a/app/src/data/connection/postgres/PgPostgresConnection.ts b/app/src/data/connection/postgres/PgPostgresConnection.ts index 5f4b19d..f9b5466 100644 --- a/app/src/data/connection/postgres/PgPostgresConnection.ts +++ b/app/src/data/connection/postgres/PgPostgresConnection.ts @@ -1,22 +1,26 @@ -import { Kysely, PostgresDialect } from "kysely"; +import { Kysely, PostgresDialect, type PostgresDialectConfig as KyselyPostgresDialectConfig } from "kysely"; import { PostgresIntrospector } from "./PostgresIntrospector"; import { PostgresConnection, plugins } from "./PostgresConnection"; import { customIntrospector } from "../Connection"; import type { Pool } from "pg"; +export type PostgresDialectConfig = Omit & { + pool: Pool; +}; + export class PgPostgresConnection extends PostgresConnection { override name = "pg"; - constructor(pool: Pool) { + constructor(config: PostgresDialectConfig) { const kysely = new Kysely({ dialect: customIntrospector(PostgresDialect, PostgresIntrospector, { excludeTables: [], - }).create({ pool }), + }).create(config), plugins, }); super(kysely); - this.client = pool; + this.client = config.pool; } override async close(): Promise { @@ -24,6 +28,6 @@ export class PgPostgresConnection extends PostgresConnection { } } -export function pg(pool: Pool): PgPostgresConnection { - return new PgPostgresConnection(pool); +export function pg(config: PostgresDialectConfig): PgPostgresConnection { + return new PgPostgresConnection(config); } diff --git a/app/src/data/connection/postgres/PostgresJsConnection.ts b/app/src/data/connection/postgres/PostgresJsConnection.ts index 3256f2c..710b033 100644 --- a/app/src/data/connection/postgres/PostgresJsConnection.ts +++ b/app/src/data/connection/postgres/PostgresJsConnection.ts @@ -2,22 +2,21 @@ import { Kysely } from "kysely"; import { PostgresIntrospector } from "./PostgresIntrospector"; import { PostgresConnection, plugins } from "./PostgresConnection"; import { customIntrospector } from "../Connection"; -import { PostgresJSDialect } from "kysely-postgres-js"; -import type { Sql } from "postgres"; +import { PostgresJSDialect, type PostgresJSDialectConfig } from "kysely-postgres-js"; -export class PostgresJsConnection extends PostgresConnection { +export class PostgresJsConnection extends PostgresConnection { override name = "postgres-js"; - constructor(opts: { postgres: Sql }) { + constructor(config: PostgresJSDialectConfig) { const kysely = new Kysely({ dialect: customIntrospector(PostgresJSDialect, PostgresIntrospector, { excludeTables: [], - }).create({ postgres: opts.postgres }), + }).create(config), plugins, }); super(kysely); - this.client = opts.postgres; + this.client = config.postgres; } override async close(): Promise { @@ -26,7 +25,7 @@ export class PostgresJsConnection extends PostgresConnection { } export function postgresJs( - postgres: Sql, + config: PostgresJSDialectConfig, ): PostgresJsConnection { - return new PostgresJsConnection({ postgres }); + return new PostgresJsConnection(config); } diff --git a/docs/content/docs/(documentation)/usage/database.mdx b/docs/content/docs/(documentation)/usage/database.mdx index 46413cc..550f615 100644 --- a/docs/content/docs/(documentation)/usage/database.mdx +++ b/docs/content/docs/(documentation)/usage/database.mdx @@ -222,9 +222,11 @@ import { pg } from "bknd"; import { Pool } from "pg"; serve({ - connection: pg(new Pool({ - connectionString: "postgresql://user:password@localhost:5432/database", - })), + connection: pg({ + pool: new Pool({ + connectionString: "postgresql://user:password@localhost:5432/database", + }), + }), }); ``` @@ -238,7 +240,9 @@ import { postgresJs } from "bknd"; import postgres from 'postgres' serve({ - connection: postgresJs(postgres("postgresql://user:password@localhost:5432/database")), + connection: postgresJs({ + postgres: postgres("postgresql://user:password@localhost:5432/database"), + }), }); ```