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

Commit

Permalink
Attempt to fix database SSL issues
Browse files Browse the repository at this point in the history
Signed-off-by: Frank Hinek <[email protected]>
  • Loading branch information
frankhinek committed Jun 26, 2024
1 parent 8ec3807 commit 5cbe906
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 24 deletions.
59 changes: 36 additions & 23 deletions backend/server/utils/database.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,44 @@
import type { PoolConfig } from 'pg';

import pg from 'pg';
import { migrate } from 'drizzle-orm/node-postgres/migrator';
import { NodePgDatabase, drizzle } from 'drizzle-orm/node-postgres';

import * as schema from '~~/db/schema';

const { Pool } = pg;

interface DbConfig extends PoolConfig {
migrationFolder: string;
}

class DbConnect {
static #instance: DbConnect;
#config: DbConfig;
#database: NodePgDatabase<typeof schema>;
#pool: pg.Pool;

private constructor(config: PoolConfig = {}) {
const defaultConfig: PoolConfig = {
private constructor(config: Partial<DbConfig> = {}) {
this.#config = this.#buildConfig(config);
this.#pool = new Pool(this.#config);
this.#database = drizzle(this.#pool, { schema });
}

#buildConfig(partialConfig: Partial<DbConfig>): DbConfig {
const defaultConfig: DbConfig = {
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
host: process.env.DB_HOST,
port: process.env.DB_PORT ? parseInt(process.env.DB_PORT) : 5432,
port: parseInt(process.env.DB_PORT || '5432', 10),
database: process.env.DB_NAME,
migrationFolder: 'db/migrations',
ssl: process.env.DB_SSL === 'true' ? { rejectUnauthorized: false } : undefined,
};

this.#pool = new Pool({ ...defaultConfig, ...config });
this.#database = drizzle(this.#pool, { schema });
return { ...defaultConfig, ...partialConfig };
}

public static getInstance() {
public static getInstance(config?: Partial<DbConfig>) {
if (!DbConnect.#instance) {
DbConnect.#instance = new DbConnect();
DbConnect.#instance = new DbConnect(config);
}
return DbConnect.#instance;
}
Expand All @@ -40,21 +50,24 @@ class DbConnect {
static get database() {
return DbConnect.getInstance().#database;
}

public async runMigrations() {
const client = await this.#pool.connect();
try {
const db = drizzle(client);
await migrate(db, { migrationsFolder: this.#config.migrationFolder });
} catch (error: any) {
console.error('Database migration failed:', error?.message || 'Unknown error');
throw error;
} finally {
client.release();
}
}

}

export const drainDbPool = DbConnect.drainPool;
export const useDatabase = () => DbConnect.database;

export const runDbMigrations = async () => {
const client = new pg.Client({
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
host: process.env.DB_HOST,
port: process.env.DB_PORT ? parseInt(process.env.DB_PORT) : 5432,
database: process.env.DB_NAME,
});
await client.connect();
const db = drizzle(client);
await migrate(db, { migrationsFolder: 'db/migrations' });
await client.end();
}
await DbConnect.getInstance().runMigrations();
};
export const useDatabase = () => DbConnect.database;
8 changes: 7 additions & 1 deletion backend/server/utils/registry-did.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ export function isPortableDid(obj: unknown): obj is PortableDid {

export async function useRegistryDid(event: H3Event<EventHandlerRequest>): Promise<BearerDid> {
// Access the portable DID from the runtime configuration.
const { portableDid } = useRuntimeConfig(event);
let { portableDid } = useRuntimeConfig(event);

// Note: On some platforms, when reading the portable DID from runtime environment variables,
// it may be returned as a string or a string wrapped with extra single quotes.
portableDid = typeof portableDid === 'string'
? JSON.parse(portableDid.replaceAll("'", ""))
: portableDid;

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

0 comments on commit 5cbe906

Please sign in to comment.