mirror of
https://github.com/aljazceru/ditto.git
synced 2026-01-12 10:04:20 +01:00
39 lines
1.0 KiB
TypeScript
39 lines
1.0 KiB
TypeScript
import { Kysely } from 'kysely';
|
|
import { PostgresJSDialect, PostgresJSDialectConfig } 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 });
|
|
}
|
|
|
|
if (!this.db) {
|
|
this.db = new Kysely({
|
|
dialect: new PostgresJSDialect({
|
|
postgres: this.postgres as unknown as PostgresJSDialectConfig['postgres'],
|
|
}),
|
|
log: KyselyLogger,
|
|
});
|
|
}
|
|
|
|
return this.db;
|
|
}
|
|
|
|
static get poolSize() {
|
|
return this.postgres?.connections.open ?? 0;
|
|
}
|
|
|
|
static get availableConnections(): number {
|
|
return this.postgres?.connections.idle ?? 0;
|
|
}
|
|
}
|