Region/district/city bounds routes
This commit is contained in:
37
ems/src/utils/postgres.ts
Normal file
37
ems/src/utils/postgres.ts
Normal file
@ -0,0 +1,37 @@
|
||||
import { Pool } from 'pg';
|
||||
import 'dotenv/config';
|
||||
|
||||
// Environment variables for database configuration
|
||||
const PG_HOST = process.env.PG_HOST || 'localhost';
|
||||
const PG_PORT = Number(process.env.PG_PORT) || 5432;
|
||||
const PG_USER = process.env.PG_USER || 'postgres';
|
||||
const PG_PASSWORD = process.env.PG_PASSWORD || '';
|
||||
const PG_DB = process.env.PG_DB || 'postgres';
|
||||
|
||||
// Create a connection pool
|
||||
const pool = new Pool({
|
||||
host: PG_HOST,
|
||||
port: PG_PORT,
|
||||
user: PG_USER,
|
||||
password: PG_PASSWORD,
|
||||
database: PG_DB,
|
||||
});
|
||||
|
||||
export async function pgQuery(query: string, params: any[] = []) {
|
||||
try {
|
||||
// Get a client from the pool
|
||||
const client = await pool.connect();
|
||||
try {
|
||||
// Execute the query with parameters
|
||||
const result = await client.query(query, params);
|
||||
return result.rows; // Return only the rows
|
||||
} finally {
|
||||
// Release the client back to the pool
|
||||
client.release();
|
||||
}
|
||||
} catch (err) {
|
||||
// Log error and rethrow it
|
||||
console.error(`Error executing query: ${query}`, err);
|
||||
throw err;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user