Merge branch 'pglite-debug' into 'main'

Add PGLITE_DEBUG environment variable

See merge request soapbox-pub/ditto!493
This commit is contained in:
Alex Gleason 2024-09-16 17:36:50 +00:00
commit e0dee33aec
5 changed files with 16 additions and 5 deletions

View file

@ -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: {

View file

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

View file

@ -10,4 +10,5 @@ export interface DittoDatabase {
export interface DittoDatabaseOpts {
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 { 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<DittoTables>({
dialect: new PgliteDialect({
database: new PGlite(databaseUrl, { extensions: { pg_trgm } }),
database: new PGlite(databaseUrl, {
extensions: { pg_trgm },
debug: opts?.debug,
}),
}),
log: KyselyLogger,
});

View file

@ -21,7 +21,10 @@ export class Storages {
public static async database(): Promise<DittoDatabase> {
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;
})();