@koziatek/pgsql 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.env.example ADDED
@@ -0,0 +1,3 @@
1
+ PGSQL_DATABASE_CONNECTION_STRING=postgres://postgres:<password>@<ip>:5432/<database_name>
2
+ PGSQL_USE_SSL=true
3
+ PGSQL_POOL_MAX=10
package/env.js ADDED
@@ -0,0 +1,7 @@
1
+ module.exports = {
2
+ PGSQL_DATABASE_CONNECTION_STRING: process?.env?.PGSQL_DATABASE_CONNECTION_STRING || false,
3
+ PGSQL_POOL_MAX: process?.env?.PGSQL_POOL_MAX || 1,
4
+ PGSQL_POOL_IDLE_MS: process?.env?.PGSQL_POOL_IDLE_MS || 30_000,
5
+ PGSQL_POOL_CONN_TIMEOUT_MS: process?.env?.PGSQL_POOL_CONN_TIMEOUT_MS || 5_000,
6
+ PGSQL_POOL_MAX_USES: process?.env?.PGSQL_POOL_MAX_USES || 5_000,
7
+ };
package/index.js ADDED
@@ -0,0 +1,77 @@
1
+ const { Pool } = require('pg');
2
+ const env = require('./env');
3
+
4
+ let POOL;
5
+ let POOL_PROMISE;
6
+
7
+ class PgSQL {
8
+
9
+ static async GetPool() {
10
+ if (POOL) return POOL;
11
+ if (POOL_PROMISE) return POOL_PROMISE;
12
+ POOL_PROMISE = PgSQL.Open();
13
+ POOL = await POOL_PROMISE;
14
+ POOL_PROMISE = null;
15
+ return POOL;
16
+ }
17
+
18
+ static async Close() {
19
+ let pool = await PgSQL.GetPool();
20
+ await pool.end();
21
+ }
22
+
23
+ static async Open(config=env) {
24
+ if(POOL) return POOL;
25
+
26
+ if(!config?.PGSQL_DATABASE_CONNECTION_STRING) throw new Error('Missing Connection String: PGSQL_DATABASE_CONNECTION_STRING')
27
+
28
+ return POOL = new Pool({
29
+ connectionString: config.PGSQL_DATABASE_CONNECTION_STRING,
30
+ max: Number(config.PGSQL_POOL_MAX || 1),
31
+ idleTimeoutMillis: Number(config.PGSQL_POOL_IDLE_MS || 30_000),
32
+ connectionTimeoutMillis: Number(config.PGSQL_POOL_CONN_TIMEOUT_MS || 5_000),
33
+ maxUses: Number(config.PGSQL_POOL_MAX_USES || 5_000),
34
+ });
35
+ }
36
+
37
+ /**
38
+ * Runs a SQL query with optional parameters.
39
+ * @param {string} sql - The SQL statement with placeholders (e.g. 'SELECT * FROM users WHERE id = $1')
40
+ * @param {Array} params - The values to safely inject in place of placeholders.
41
+ * @returns {Promise} Resolves with query result rows or rejects with error.
42
+ */
43
+ static async Query(sql, params = []) {
44
+ try {
45
+ let pool = await PgSQL.GetPool();
46
+ const result = await pool.query(sql, params);
47
+ return result.rows;
48
+ } catch (err) {
49
+ console.error('Database query error:', err);
50
+ throw err;
51
+ }
52
+ }
53
+
54
+ static async Exec(sql, params = []) {
55
+ const pool = await PgSQL.GetPool();
56
+ return pool.query(sql, params);
57
+ }
58
+
59
+ /**
60
+ * Checks if the database connection is healthy.
61
+ * @returns {Promise<boolean>}
62
+ */
63
+ static async CheckConnection() {
64
+ try {
65
+ let pool = await PgSQL.GetPool();
66
+ await pool.query('SELECT 1');
67
+ return true;
68
+ } catch (err) {
69
+ console.error('Database connection check failed:', err);
70
+ return false;
71
+ }
72
+ }
73
+ }
74
+
75
+ module.exports = {
76
+ PgSQL
77
+ };
package/package.json ADDED
@@ -0,0 +1,22 @@
1
+ {
2
+ "name": "@koziatek/pgsql",
3
+ "version": "1.0.1",
4
+ "description": "",
5
+ "main": "index.js",
6
+ "publishConfig": {
7
+ "access": "public"
8
+ },
9
+ "scripts": {
10
+ "test": "jest"
11
+ },
12
+ "keywords": [],
13
+ "author": "",
14
+ "license": "ISC",
15
+ "dependencies": {
16
+ "dotenv": "^16.5.0",
17
+ "pg": "^8.15.6"
18
+ },
19
+ "devDependencies": {
20
+ "jest": "^29.7.0"
21
+ }
22
+ }
@@ -0,0 +1,11 @@
1
+ const { PgSQL } = require("..");
2
+
3
+
4
+
5
+ describe('PgSQL Connection', ()=>{
6
+ it('- can connect to database.', async () => {
7
+ const connected = await PgSQL.CheckConnection();
8
+ expect(connected).toBe(true);
9
+ });
10
+
11
+ });