added batching for postgres

This commit is contained in:
dswbx
2025-03-07 19:40:58 +01:00
parent 3704cad53a
commit a4cb012ce8
7 changed files with 53 additions and 5 deletions

View File

@@ -151,4 +151,5 @@ export abstract class Connection<DB = any> {
}
abstract getFieldSchema(spec: FieldSpec, strict?: boolean): SchemaResponse;
abstract close(): Promise<void>;
}

View File

@@ -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;
}
}

View File

@@ -47,4 +47,8 @@ export class SqliteConnection extends Connection {
},
] as const;
}
override async close(): Promise<void> {
// no-op
}
}