Skip to content
This repository was archived by the owner on Dec 12, 2024. It is now read-only.

Commit 5cbe906

Browse files
committed
Attempt to fix database SSL issues
Signed-off-by: Frank Hinek <[email protected]>
1 parent 8ec3807 commit 5cbe906

File tree

2 files changed

+43
-24
lines changed

2 files changed

+43
-24
lines changed

backend/server/utils/database.ts

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,44 @@
11
import type { PoolConfig } from 'pg';
2-
32
import pg from 'pg';
43
import { migrate } from 'drizzle-orm/node-postgres/migrator';
54
import { NodePgDatabase, drizzle } from 'drizzle-orm/node-postgres';
6-
75
import * as schema from '~~/db/schema';
86

97
const { Pool } = pg;
108

9+
interface DbConfig extends PoolConfig {
10+
migrationFolder: string;
11+
}
12+
1113
class DbConnect {
1214
static #instance: DbConnect;
15+
#config: DbConfig;
1316
#database: NodePgDatabase<typeof schema>;
1417
#pool: pg.Pool;
1518

16-
private constructor(config: PoolConfig = {}) {
17-
const defaultConfig: PoolConfig = {
19+
private constructor(config: Partial<DbConfig> = {}) {
20+
this.#config = this.#buildConfig(config);
21+
this.#pool = new Pool(this.#config);
22+
this.#database = drizzle(this.#pool, { schema });
23+
}
24+
25+
#buildConfig(partialConfig: Partial<DbConfig>): DbConfig {
26+
const defaultConfig: DbConfig = {
1827
user: process.env.DB_USER,
1928
password: process.env.DB_PASSWORD,
2029
host: process.env.DB_HOST,
21-
port: process.env.DB_PORT ? parseInt(process.env.DB_PORT) : 5432,
30+
port: parseInt(process.env.DB_PORT || '5432', 10),
2231
database: process.env.DB_NAME,
32+
migrationFolder: 'db/migrations',
33+
ssl: process.env.DB_SSL === 'true' ? { rejectUnauthorized: false } : undefined,
2334
};
2435

25-
this.#pool = new Pool({ ...defaultConfig, ...config });
26-
this.#database = drizzle(this.#pool, { schema });
36+
return { ...defaultConfig, ...partialConfig };
2737
}
2838

29-
public static getInstance() {
39+
public static getInstance(config?: Partial<DbConfig>) {
3040
if (!DbConnect.#instance) {
31-
DbConnect.#instance = new DbConnect();
41+
DbConnect.#instance = new DbConnect(config);
3242
}
3343
return DbConnect.#instance;
3444
}
@@ -40,21 +50,24 @@ class DbConnect {
4050
static get database() {
4151
return DbConnect.getInstance().#database;
4252
}
53+
54+
public async runMigrations() {
55+
const client = await this.#pool.connect();
56+
try {
57+
const db = drizzle(client);
58+
await migrate(db, { migrationsFolder: this.#config.migrationFolder });
59+
} catch (error: any) {
60+
console.error('Database migration failed:', error?.message || 'Unknown error');
61+
throw error;
62+
} finally {
63+
client.release();
64+
}
65+
}
66+
4367
}
4468

4569
export const drainDbPool = DbConnect.drainPool;
46-
export const useDatabase = () => DbConnect.database;
47-
4870
export const runDbMigrations = async () => {
49-
const client = new pg.Client({
50-
user: process.env.DB_USER,
51-
password: process.env.DB_PASSWORD,
52-
host: process.env.DB_HOST,
53-
port: process.env.DB_PORT ? parseInt(process.env.DB_PORT) : 5432,
54-
database: process.env.DB_NAME,
55-
});
56-
await client.connect();
57-
const db = drizzle(client);
58-
await migrate(db, { migrationsFolder: 'db/migrations' });
59-
await client.end();
60-
}
71+
await DbConnect.getInstance().runMigrations();
72+
};
73+
export const useDatabase = () => DbConnect.database;

backend/server/utils/registry-did.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@ export function isPortableDid(obj: unknown): obj is PortableDid {
1313

1414
export async function useRegistryDid(event: H3Event<EventHandlerRequest>): Promise<BearerDid> {
1515
// Access the portable DID from the runtime configuration.
16-
const { portableDid } = useRuntimeConfig(event);
16+
let { portableDid } = useRuntimeConfig(event);
17+
18+
// Note: On some platforms, when reading the portable DID from runtime environment variables,
19+
// it may be returned as a string or a string wrapped with extra single quotes.
20+
portableDid = typeof portableDid === 'string'
21+
? JSON.parse(portableDid.replaceAll("'", ""))
22+
: portableDid;
1723

1824
if (!isPortableDid(portableDid)) {
1925
throw new Error('Failed to access portable DID from runtime configuration')

0 commit comments

Comments
 (0)