mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-15 20:17:22 +00:00
refactor: expose additional kysely postgres options
This commit is contained in:
@@ -28,7 +28,7 @@ async function cleanDatabase(connection: InstanceType<typeof PostgresConnection>
|
|||||||
async function isPostgresRunning() {
|
async function isPostgresRunning() {
|
||||||
try {
|
try {
|
||||||
// Try to actually connect to PostgreSQL
|
// Try to actually connect to PostgreSQL
|
||||||
const conn = pg(new Pool(credentials));
|
const conn = pg({ pool: new Pool(credentials) });
|
||||||
await conn.ping();
|
await conn.ping();
|
||||||
await conn.close();
|
await conn.close();
|
||||||
return true;
|
return true;
|
||||||
@@ -58,8 +58,8 @@ describe("postgres", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe.serial.each([
|
describe.serial.each([
|
||||||
["pg", () => pg(new Pool(credentials))],
|
["pg", () => pg({ pool: new Pool(credentials) })],
|
||||||
["postgresjs", () => postgresJs(postgres(credentials))],
|
["postgresjs", () => postgresJs({ postgres: postgres(credentials) })],
|
||||||
])("%s", (name, createConnection) => {
|
])("%s", (name, createConnection) => {
|
||||||
connectionTestSuite(
|
connectionTestSuite(
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,22 +1,26 @@
|
|||||||
import { Kysely, PostgresDialect } from "kysely";
|
import { Kysely, PostgresDialect, type PostgresDialectConfig as KyselyPostgresDialectConfig } from "kysely";
|
||||||
import { PostgresIntrospector } from "./PostgresIntrospector";
|
import { PostgresIntrospector } from "./PostgresIntrospector";
|
||||||
import { PostgresConnection, plugins } from "./PostgresConnection";
|
import { PostgresConnection, plugins } from "./PostgresConnection";
|
||||||
import { customIntrospector } from "../Connection";
|
import { customIntrospector } from "../Connection";
|
||||||
import type { Pool } from "pg";
|
import type { Pool } from "pg";
|
||||||
|
|
||||||
|
export type PostgresDialectConfig = Omit<KyselyPostgresDialectConfig, "pool"> & {
|
||||||
|
pool: Pool;
|
||||||
|
};
|
||||||
|
|
||||||
export class PgPostgresConnection extends PostgresConnection<Pool> {
|
export class PgPostgresConnection extends PostgresConnection<Pool> {
|
||||||
override name = "pg";
|
override name = "pg";
|
||||||
|
|
||||||
constructor(pool: Pool) {
|
constructor(config: PostgresDialectConfig) {
|
||||||
const kysely = new Kysely({
|
const kysely = new Kysely({
|
||||||
dialect: customIntrospector(PostgresDialect, PostgresIntrospector, {
|
dialect: customIntrospector(PostgresDialect, PostgresIntrospector, {
|
||||||
excludeTables: [],
|
excludeTables: [],
|
||||||
}).create({ pool }),
|
}).create(config),
|
||||||
plugins,
|
plugins,
|
||||||
});
|
});
|
||||||
|
|
||||||
super(kysely);
|
super(kysely);
|
||||||
this.client = pool;
|
this.client = config.pool;
|
||||||
}
|
}
|
||||||
|
|
||||||
override async close(): Promise<void> {
|
override async close(): Promise<void> {
|
||||||
@@ -24,6 +28,6 @@ export class PgPostgresConnection extends PostgresConnection<Pool> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function pg(pool: Pool): PgPostgresConnection {
|
export function pg(config: PostgresDialectConfig): PgPostgresConnection {
|
||||||
return new PgPostgresConnection(pool);
|
return new PgPostgresConnection(config);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,22 +2,21 @@ import { Kysely } from "kysely";
|
|||||||
import { PostgresIntrospector } from "./PostgresIntrospector";
|
import { PostgresIntrospector } from "./PostgresIntrospector";
|
||||||
import { PostgresConnection, plugins } from "./PostgresConnection";
|
import { PostgresConnection, plugins } from "./PostgresConnection";
|
||||||
import { customIntrospector } from "../Connection";
|
import { customIntrospector } from "../Connection";
|
||||||
import { PostgresJSDialect } from "kysely-postgres-js";
|
import { PostgresJSDialect, type PostgresJSDialectConfig } from "kysely-postgres-js";
|
||||||
import type { Sql } from "postgres";
|
|
||||||
|
|
||||||
export class PostgresJsConnection extends PostgresConnection<Sql> {
|
export class PostgresJsConnection extends PostgresConnection<PostgresJSDialectConfig["postgres"]> {
|
||||||
override name = "postgres-js";
|
override name = "postgres-js";
|
||||||
|
|
||||||
constructor(opts: { postgres: Sql }) {
|
constructor(config: PostgresJSDialectConfig) {
|
||||||
const kysely = new Kysely({
|
const kysely = new Kysely({
|
||||||
dialect: customIntrospector(PostgresJSDialect, PostgresIntrospector, {
|
dialect: customIntrospector(PostgresJSDialect, PostgresIntrospector, {
|
||||||
excludeTables: [],
|
excludeTables: [],
|
||||||
}).create({ postgres: opts.postgres }),
|
}).create(config),
|
||||||
plugins,
|
plugins,
|
||||||
});
|
});
|
||||||
|
|
||||||
super(kysely);
|
super(kysely);
|
||||||
this.client = opts.postgres;
|
this.client = config.postgres;
|
||||||
}
|
}
|
||||||
|
|
||||||
override async close(): Promise<void> {
|
override async close(): Promise<void> {
|
||||||
@@ -26,7 +25,7 @@ export class PostgresJsConnection extends PostgresConnection<Sql> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function postgresJs(
|
export function postgresJs(
|
||||||
postgres: Sql,
|
config: PostgresJSDialectConfig,
|
||||||
): PostgresJsConnection {
|
): PostgresJsConnection {
|
||||||
return new PostgresJsConnection({ postgres });
|
return new PostgresJsConnection(config);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -222,9 +222,11 @@ import { pg } from "bknd";
|
|||||||
import { Pool } from "pg";
|
import { Pool } from "pg";
|
||||||
|
|
||||||
serve({
|
serve({
|
||||||
connection: pg(new Pool({
|
connection: pg({
|
||||||
connectionString: "postgresql://user:password@localhost:5432/database",
|
pool: new Pool({
|
||||||
})),
|
connectionString: "postgresql://user:password@localhost:5432/database",
|
||||||
|
}),
|
||||||
|
}),
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -238,7 +240,9 @@ import { postgresJs } from "bknd";
|
|||||||
import postgres from 'postgres'
|
import postgres from 'postgres'
|
||||||
|
|
||||||
serve({
|
serve({
|
||||||
connection: postgresJs(postgres("postgresql://user:password@localhost:5432/database")),
|
connection: postgresJs({
|
||||||
|
postgres: postgres("postgresql://user:password@localhost:5432/database"),
|
||||||
|
}),
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user