diff --git a/src/config.ts b/src/config.ts index e174c81a..f007341f 100644 --- a/src/config.ts +++ b/src/config.ts @@ -77,6 +77,10 @@ class Conf { static get testDatabaseUrl(): string { 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 = { /** Database query timeout configurations. */ timeouts: { diff --git a/src/db/DittoDB.ts b/src/db/DittoDB.ts index 445c3da2..923a109d 100644 --- a/src/db/DittoDB.ts +++ b/src/db/DittoDB.ts @@ -16,7 +16,7 @@ export class DittoDB { switch (protocol) { case 'file:': case 'memory:': - return DittoPglite.create(databaseUrl); + return DittoPglite.create(databaseUrl, opts); case 'postgres:': case 'postgresql:': return DittoPostgres.create(databaseUrl, opts); diff --git a/src/db/DittoDatabase.ts b/src/db/DittoDatabase.ts index 530d9391..ec9a103d 100644 --- a/src/db/DittoDatabase.ts +++ b/src/db/DittoDatabase.ts @@ -10,4 +10,5 @@ export interface DittoDatabase { export interface DittoDatabaseOpts { poolSize?: number; + debug?: 0 | 1 | 2 | 3 | 4 | 5; } diff --git a/src/db/adapters/DittoPglite.ts b/src/db/adapters/DittoPglite.ts index 0e93075d..2455fc37 100644 --- a/src/db/adapters/DittoPglite.ts +++ b/src/db/adapters/DittoPglite.ts @@ -3,15 +3,18 @@ import { pg_trgm } from '@electric-sql/pglite/contrib/pg_trgm'; import { PgliteDialect } from '@soapbox/kysely-pglite'; import { Kysely } from 'kysely'; -import { DittoDatabase } from '@/db/DittoDatabase.ts'; +import { DittoDatabase, DittoDatabaseOpts } from '@/db/DittoDatabase.ts'; import { DittoTables } from '@/db/DittoTables.ts'; import { KyselyLogger } from '@/db/KyselyLogger.ts'; export class DittoPglite { - static create(databaseUrl: string): DittoDatabase { + static create(databaseUrl: string, opts?: DittoDatabaseOpts): DittoDatabase { const kysely = new Kysely({ dialect: new PgliteDialect({ - database: new PGlite(databaseUrl, { extensions: { pg_trgm } }), + database: new PGlite(databaseUrl, { + extensions: { pg_trgm }, + debug: opts?.debug, + }), }), log: KyselyLogger, }); diff --git a/src/storages.ts b/src/storages.ts index cbafd5aa..073b6135 100644 --- a/src/storages.ts +++ b/src/storages.ts @@ -21,7 +21,10 @@ export class Storages { public static async database(): Promise { if (!this._database) { 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); return db; })();