This repository was archived by the owner on Oct 7, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnectPostgres.js
More file actions
158 lines (148 loc) · 3.88 KB
/
ConnectPostgres.js
File metadata and controls
158 lines (148 loc) · 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/**
* @module ConnectPostgres
* @description Database connection configuration and query management module for PostgreSQL
* @requires pg
* @requires child_process
*/
const { Pool } = require('pg');
const { execSync } = require('child_process');
/**
* @constant {Object}
* @description Database configuration settings
* @private
*/
const dbConfig = {
database: "postgres",
user: "postgres",
password: "pgres",
host: "postgres", // for docker setup
//host: "localhost", // for local development
port: 5432
};
/**
* Retrieves the current Git branch name
* @private
* @function getGitBranch
* @returns {?string} The current branch name or null if retrieval fails
*/
function getGitBranch() {
try {
const branchName = execSync('git rev-parse --abbrev-ref HEAD', { encoding: 'utf8' }).trim();
return branchName;
} catch (error) {
console.error("Error fetching Git branch:", error);
return null;
}
}
/**
* Creates a new connection pool for a single query
* @private
* @function createPool
* @returns {Pool} A new PostgreSQL connection pool
*/
function createPool() {
return new Pool(dbConfig);
}
/**
* Explicit method to check database connection
* @async
* @function checkConnection
* @returns {Promise<void>}
*/
async function checkConnection() {
const pool = createPool();
try {
const client = await pool.connect();
const branch = getGitBranch();
console.log('Connected to the PostgreSQL database.');
console.log(`Git on branch: ${branch}`);
client.release();
} catch (err) {
console.error('Error connecting to the database:', err);
throw err;
} finally {
await pool.end();
}
}
/**
* @typedef {Object} QueryResult
* @property {Array} rows The rows returned by the query
* @property {number} rowCount Number of rows affected by the query
*/
/**
* Database interface providing configuration and query execution capabilities
* @exports ConnectPostgres
* @type {Object}
*/
module.exports = {
/**
* Database configuration for use in other modules
* @type {Object}
*/
dbConfig,
/**
* Executes a parameterized SQL query
* @async
* @function query
* @param {string} text The SQL query text
* @param {Array} [params] Query parameters
* @returns {Promise<QueryResult>} The query result
* @throws {Error} If query execution fails
*/
query: async (text, params) => {
const pool = createPool();
try {
const result = await pool.query(text, params);
return result;
} catch (err) {
console.error('Error executing query:', err);
throw err;
} finally {
await pool.end();
}
},
/**
* Check database connection status
* @function checkConnection
*/
checkConnection
};
/**
* @example
*
* Basic query execution:
* ```javascript
* try {
* const result = await db.query('SELECT * FROM users WHERE id = $1', [userId]);
* console.log(result.rows);
* } catch (error) {
* console.error('Query failed:', error);
* }
* ```
*
* Using database configuration in other modules:
* ```javascript
* const { dbConfig } = require('./ConnectPostgres');
* const { Sequelize } = require('sequelize');
*
* const sequelize = new Sequelize(
* dbConfig.database,
* dbConfig.user,
* dbConfig.password,
* {
* host: dbConfig.host,
* dialect: 'postgres'
* }
* );
* ```
*
* Checking database connection:
* ```javascript
* try {
* await db.checkConnection();
* console.log('Database connection successful');
* } catch (error) {
* console.error('Unable to connect to the database:', error);
* }
* ```
*/