mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-16 20:37:21 +00:00
added batching for postgres
This commit is contained in:
@@ -151,4 +151,5 @@ export abstract class Connection<DB = any> {
|
||||
}
|
||||
|
||||
abstract getFieldSchema(spec: FieldSpec, strict?: boolean): SchemaResponse;
|
||||
abstract close(): Promise<void>;
|
||||
}
|
||||
|
||||
@@ -8,7 +8,12 @@ import {
|
||||
} from "kysely";
|
||||
import pg from "pg";
|
||||
import { PostgresIntrospector } from "./PostgresIntrospector";
|
||||
import { type FieldSpec, type SchemaResponse, Connection } from "data/connection/Connection";
|
||||
import {
|
||||
type FieldSpec,
|
||||
type SchemaResponse,
|
||||
Connection,
|
||||
type QB,
|
||||
} from "data/connection/Connection";
|
||||
|
||||
export type PostgresConnectionConfig = pg.PoolConfig;
|
||||
|
||||
@@ -21,16 +26,20 @@ class CustomPostgresDialect extends PostgresDialect {
|
||||
}
|
||||
|
||||
export class PostgresConnection extends Connection {
|
||||
private pool: pg.Pool;
|
||||
|
||||
constructor(config: PostgresConnectionConfig) {
|
||||
const pool = new pg.Pool(config);
|
||||
const kysely = new Kysely({
|
||||
dialect: new CustomPostgresDialect({
|
||||
pool: new pg.Pool(config),
|
||||
pool,
|
||||
}),
|
||||
plugins,
|
||||
//log: ["query", "error"],
|
||||
});
|
||||
|
||||
super(kysely, {}, plugins);
|
||||
this.pool = pool;
|
||||
}
|
||||
|
||||
override supportsIndices(): boolean {
|
||||
@@ -73,4 +82,22 @@ export class PostgresConnection extends Connection {
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
override supportsBatching(): boolean {
|
||||
return true;
|
||||
}
|
||||
|
||||
override async close(): Promise<void> {
|
||||
await this.pool.end();
|
||||
}
|
||||
|
||||
protected override async batch<Queries extends QB[]>(
|
||||
queries: [...Queries],
|
||||
): Promise<{
|
||||
[K in keyof Queries]: Awaited<ReturnType<Queries[K]["execute"]>>;
|
||||
}> {
|
||||
return this.kysely.transaction().execute(async (trx) => {
|
||||
return Promise.all(queries.map((q) => trx.executeQuery(q).then((r) => r.rows)));
|
||||
}) as any;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,4 +47,8 @@ export class SqliteConnection extends Connection {
|
||||
},
|
||||
] as const;
|
||||
}
|
||||
|
||||
override async close(): Promise<void> {
|
||||
// no-op
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user