-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
daa42bf
commit faa8460
Showing
8 changed files
with
127 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// context.ts | ||
import { RedisClientType } from 'redis'; | ||
import { Pool, Client as PgClient } from 'pg'; | ||
import { getCacheClient, closeCacheClient } from '../cache'; | ||
import { getDBClient, closeDBClient } from '../repository'; | ||
|
||
export interface AppContext { | ||
cacheClient: RedisClientType | null; | ||
dbClient: Pool | PgClient | null; | ||
} | ||
|
||
let context: AppContext = { | ||
cacheClient: null, | ||
dbClient: null, | ||
}; | ||
|
||
export async function initializeContext(): Promise<void> { | ||
context.cacheClient = await getCacheClient(); | ||
context.dbClient = await getDBClient(); | ||
} | ||
|
||
export function getContext(): AppContext { | ||
return context; | ||
} | ||
|
||
export async function closeContext(): Promise<void> { | ||
await closeCacheClient(); | ||
await closeDBClient(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,62 @@ | ||
import pg from 'pg' | ||
const { Client } = pg | ||
import pg from 'pg'; | ||
const { Pool } = pg; | ||
|
||
let client: pg.Client | null = null | ||
let pool: pg.Pool | null = null; | ||
|
||
; | ||
|
||
export async function getClient(): Promise<pg.Client | null> { | ||
const connectionStr = process.env.DATABASE_URL | ||
export async function getDBClient(): Promise<pg.Pool | null> { | ||
const connectionStr = process.env.DATABASE_URL; | ||
if (!connectionStr) { | ||
console.error('DATABASE_URL so not using database') | ||
console.error('DATABASE_URL is not defined'); | ||
return null; | ||
} | ||
if (client) { | ||
console.error('Already connected to database') | ||
return client | ||
|
||
if (pool) { | ||
return pool; | ||
} | ||
client = new Client({ connectionString: connectionStr, ssl: { rejectUnauthorized: false } }) | ||
client.on('error', (error) => { | ||
console.error('Database error:', error) | ||
}); | ||
client.on('end', () => { | ||
console.log('Database connection closed') | ||
|
||
pool = new Pool({ connectionString: connectionStr, ssl: { rejectUnauthorized: false } }); | ||
pool.on('error', (error) => { | ||
console.error('Database pool error:', error); | ||
}); | ||
client.connect() | ||
|
||
return pool; | ||
} | ||
|
||
export async function closeClient() { | ||
if (client) { | ||
await client.end() | ||
client = null | ||
export async function closeDBClient(): Promise<void> { | ||
if (pool) { | ||
try { | ||
await pool.end(); | ||
} catch (error) { | ||
console.error('Error closing database pool:', error); | ||
} finally { | ||
pool = null; | ||
} | ||
} | ||
} | ||
|
||
export async function getRatingsfromTTIDs(ttids: string[]): Promise<Record<string, Record<string, string>>> { | ||
if (!client) { | ||
throw new Error('Not connected to database') | ||
const pool = await getDBClient(); | ||
if (!pool) { | ||
throw new Error('Not connected to database'); | ||
} | ||
try { | ||
const query = `SELECT ttid, ratings.provider, rating FROM ratings WHERE ttid = ANY($1)` | ||
const { rows } = await client.query(query, [ttids]) | ||
const client = await pool.connect(); | ||
const query = `SELECT ttid, ratings.provider, rating FROM ratings WHERE ttid = ANY($1)`; | ||
const { rows } = await client.query(query, [ttids]); | ||
client.release(); // Release client back to pool | ||
return rows.reduce((acc: Record<string, Record<string, string>>, row: any) => { | ||
if (!acc[row.ttid]) { | ||
acc[row.ttid] = {} | ||
acc[row.ttid] = {}; | ||
} | ||
acc[row.ttid][row.provider] = row.rating | ||
return acc | ||
acc[row.ttid][row.provider] = row.rating; | ||
return acc; | ||
}, {}); | ||
} catch (error) { | ||
console.error('Error fetching ratings from database:', error) | ||
return {} | ||
console.error('Error fetching ratings from database:', error); | ||
return {}; | ||
} | ||
} | ||
|
||
export function isDatabaseConnected(): boolean { | ||
return client != null | ||
return pool != null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters