@koziatek/pgsql 1.0.1 → 2.0.0

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.js CHANGED
@@ -1,3 +1,6 @@
1
+ require('dotenv').config();
2
+
3
+
1
4
  module.exports = {
2
5
  PGSQL_DATABASE_CONNECTION_STRING: process?.env?.PGSQL_DATABASE_CONNECTION_STRING || false,
3
6
  PGSQL_POOL_MAX: process?.env?.PGSQL_POOL_MAX || 1,
package/index.js CHANGED
@@ -6,6 +6,8 @@ let POOL_PROMISE;
6
6
 
7
7
  class PgSQL {
8
8
 
9
+ static config;
10
+
9
11
  static async GetPool() {
10
12
  if (POOL) return POOL;
11
13
  if (POOL_PROMISE) return POOL_PROMISE;
@@ -20,18 +22,27 @@ class PgSQL {
20
22
  await pool.end();
21
23
  }
22
24
 
23
- static async Open(config=env) {
24
- if(POOL) return POOL;
25
+ static SetConfig({ca, connectionString, pool_max=1, idleTimeourMillis=30_000, connectionTimeoutMillis=5_000, maxUses=5_000}){
26
+ return this.config = {
27
+ connectionString,
28
+ max: pool_max,
29
+ idleTimeourMillis,
30
+ connectionTimeoutMillis,
31
+ maxUses,
32
+ ssl: {
33
+ ca,
34
+ rejectUnauthorized: true,
35
+ }
36
+ }
37
+ }
38
+
25
39
 
26
- if(!config?.PGSQL_DATABASE_CONNECTION_STRING) throw new Error('Missing Connection String: PGSQL_DATABASE_CONNECTION_STRING')
40
+ static async Open() {
41
+ if(POOL) return POOL;
42
+ const config = this.config;
43
+ if(!config?.connectionString) throw new Error('Missing Connection String: connectionString')
27
44
 
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
- });
45
+ return POOL = new Pool(config);
35
46
  }
36
47
 
37
48
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@koziatek/pgsql",
3
- "version": "1.0.1",
3
+ "version": "2.0.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "publishConfig": {
@@ -1,9 +1,17 @@
1
1
  const { PgSQL } = require("..");
2
+ const fs = require("fs");
3
+ const path = require("path");
2
4
 
3
5
 
4
6
 
5
7
  describe('PgSQL Connection', ()=>{
6
8
  it('- can connect to database.', async () => {
9
+ const caPath = path.join(__dirname,'..','ca-certificate.secret.crt');
10
+ console.log('caPath',caPath);
11
+ const ca = fs.readFileSync(caPath, "utf8");
12
+ const connectionString = process.env.PGSQL_DATABASE_CONNECTION_STRING;
13
+ console.log('connectionString',connectionString);
14
+ PgSQL.SetConfig({ca, connectionString})
7
15
  const connected = await PgSQL.CheckConnection();
8
16
  expect(connected).toBe(true);
9
17
  });