41 lines
1.1 KiB
TypeScript
41 lines
1.1 KiB
TypeScript
import { Pool } from 'pg';
|
|
|
|
const pool = new Pool({
|
|
connectionString: process.env.DATABASE_URL,
|
|
max: 10,
|
|
idleTimeoutMillis: 30000,
|
|
});
|
|
|
|
export async function query<T = Record<string, unknown>>(
|
|
text: string,
|
|
params?: unknown[]
|
|
): Promise<T[]> {
|
|
const result = await pool.query(text, params);
|
|
return result.rows as T[];
|
|
}
|
|
|
|
export async function getConfig(key: string): Promise<string | null> {
|
|
const rows = await query<{ value: string }>(
|
|
'SELECT value FROM admin_config WHERE key = $1',
|
|
[key]
|
|
);
|
|
return rows[0]?.value ?? null;
|
|
}
|
|
|
|
export async function getAllConfig(): Promise<Record<string, string>> {
|
|
const rows = await query<{ key: string; value: string }>(
|
|
'SELECT key, value FROM admin_config ORDER BY key'
|
|
);
|
|
return Object.fromEntries(rows.map((r) => [r.key, r.value]));
|
|
}
|
|
|
|
export async function setConfig(key: string, value: string): Promise<void> {
|
|
await query(
|
|
`INSERT INTO admin_config (key, value, updated_at) VALUES ($1, $2, NOW())
|
|
ON CONFLICT (key) DO UPDATE SET value = $2, updated_at = NOW()`,
|
|
[key, value]
|
|
);
|
|
}
|
|
|
|
export default pool;
|