mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 11:29:46 +00:00
40 lines
1 KiB
TypeScript
40 lines
1 KiB
TypeScript
import { Kysely } from 'kysely';
|
|
import { PostgresJSDialect } from 'kysely-postgres-js';
|
|
import postgres from 'postgres';
|
|
|
|
import { Conf } from '@/config.ts';
|
|
import { DittoTables } from '@/db/DittoTables.ts';
|
|
import { KyselyLogger } from '@/db/KyselyLogger.ts';
|
|
|
|
export class DittoPostgres {
|
|
static db: Kysely<DittoTables> | undefined;
|
|
static postgres: postgres.Sql;
|
|
|
|
// deno-lint-ignore require-await
|
|
static async getInstance(): Promise<Kysely<DittoTables>> {
|
|
if (!this.postgres) {
|
|
this.postgres = postgres(Conf.databaseUrl, { max: Conf.pg.poolSize }) as any;
|
|
}
|
|
|
|
if (!this.db) {
|
|
this.db = new Kysely({
|
|
dialect: new PostgresJSDialect({
|
|
postgres: this.postgres,
|
|
}),
|
|
log: KyselyLogger,
|
|
});
|
|
}
|
|
|
|
return this.db;
|
|
}
|
|
|
|
static get poolSize() {
|
|
return Conf.pg.poolSize;
|
|
}
|
|
|
|
static get availableConnections(): number {
|
|
// blocked by https://github.com/porsager/postgres/pull/911
|
|
// return this.postgres.availableConnections;
|
|
return 1;
|
|
}
|
|
}
|