Add PGLITE_DEBUG environment variable

This commit is contained in:
Alex Gleason 2024-09-16 12:31:50 -05:00
parent 4d0ae401b3
commit 8e3ddaa056
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
5 changed files with 16 additions and 5 deletions

View file

@ -77,6 +77,10 @@ class Conf {
static get testDatabaseUrl(): string { static get testDatabaseUrl(): string {
return Deno.env.get('TEST_DATABASE_URL') ?? 'memory://'; return Deno.env.get('TEST_DATABASE_URL') ?? 'memory://';
} }
/** PGlite debug level. 0 disables logging. */
static get pgliteDebug(): 0 | 1 | 2 | 3 | 4 | 5 {
return Number(Deno.env.get('PGLITE_DEBUG') || 0) as 0 | 1 | 2 | 3 | 4 | 5;
}
static db = { static db = {
/** Database query timeout configurations. */ /** Database query timeout configurations. */
timeouts: { timeouts: {

View file

@ -16,7 +16,7 @@ export class DittoDB {
switch (protocol) { switch (protocol) {
case 'file:': case 'file:':
case 'memory:': case 'memory:':
return DittoPglite.create(databaseUrl); return DittoPglite.create(databaseUrl, opts);
case 'postgres:': case 'postgres:':
case 'postgresql:': case 'postgresql:':
return DittoPostgres.create(databaseUrl, opts); return DittoPostgres.create(databaseUrl, opts);

View file

@ -10,4 +10,5 @@ export interface DittoDatabase {
export interface DittoDatabaseOpts { export interface DittoDatabaseOpts {
poolSize?: number; poolSize?: number;
debug?: 0 | 1 | 2 | 3 | 4 | 5;
} }

View file

@ -3,15 +3,18 @@ import { pg_trgm } from '@electric-sql/pglite/contrib/pg_trgm';
import { PgliteDialect } from '@soapbox/kysely-pglite'; import { PgliteDialect } from '@soapbox/kysely-pglite';
import { Kysely } from 'kysely'; import { Kysely } from 'kysely';
import { DittoDatabase } from '@/db/DittoDatabase.ts'; import { DittoDatabase, DittoDatabaseOpts } from '@/db/DittoDatabase.ts';
import { DittoTables } from '@/db/DittoTables.ts'; import { DittoTables } from '@/db/DittoTables.ts';
import { KyselyLogger } from '@/db/KyselyLogger.ts'; import { KyselyLogger } from '@/db/KyselyLogger.ts';
export class DittoPglite { export class DittoPglite {
static create(databaseUrl: string): DittoDatabase { static create(databaseUrl: string, opts?: DittoDatabaseOpts): DittoDatabase {
const kysely = new Kysely<DittoTables>({ const kysely = new Kysely<DittoTables>({
dialect: new PgliteDialect({ dialect: new PgliteDialect({
database: new PGlite(databaseUrl, { extensions: { pg_trgm } }), database: new PGlite(databaseUrl, {
extensions: { pg_trgm },
debug: opts?.debug,
}),
}), }),
log: KyselyLogger, log: KyselyLogger,
}); });

View file

@ -21,7 +21,10 @@ export class Storages {
public static async database(): Promise<DittoDatabase> { public static async database(): Promise<DittoDatabase> {
if (!this._database) { if (!this._database) {
this._database = (async () => { this._database = (async () => {
const db = DittoDB.create(Conf.databaseUrl, { poolSize: Conf.pg.poolSize }); const db = DittoDB.create(Conf.databaseUrl, {
poolSize: Conf.pg.poolSize,
debug: Conf.pgliteDebug,
});
await DittoDB.migrate(db.kysely); await DittoDB.migrate(db.kysely);
return db; return db;
})(); })();