@nsshunt/stsdatamanagement 1.16.6 → 1.16.9

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/build.sh ADDED
@@ -0,0 +1,29 @@
1
+ #!/bin/sh
2
+ rm -rf dist
3
+ rm -rf types
4
+ npm run build
5
+ RESULT=$?
6
+ if [ $RESULT -eq 0 ]; then
7
+ echo success build
8
+ npm run lint
9
+ RESULT=$?
10
+ if [ $RESULT -eq 0 ]; then
11
+ echo success lint
12
+ npm run test
13
+ RESULT=$?
14
+ if [ $RESULT -eq 0 ]; then
15
+ echo success test
16
+ git commit -a -m "changed"
17
+ npm version patch
18
+ npm i
19
+ git commit -a -m "changed"
20
+ git push
21
+ else
22
+ echo failed test
23
+ fi
24
+ else
25
+ echo failed lint
26
+ fi
27
+ else
28
+ echo failed build
29
+ fi
@@ -6,28 +6,30 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.PGPoolManager = void 0;
7
7
  const stsconfig_1 = require("@nsshunt/stsconfig");
8
8
  const { databaseUrl, connectionString, defaultDatabaseConnectionString, isProduction, poolSize } = (0, stsconfig_1.$Options)();
9
- const { Gauge } = require('@nsshunt/stsinstrumentation');
9
+ const stsutils_1 = require("@nsshunt/stsutils");
10
+ const stsinstrumentation_1 = require("@nsshunt/stsinstrumentation");
10
11
  const debug_1 = __importDefault(require("debug"));
11
12
  const debug = (0, debug_1.default)(`proc:${process.pid}`);
12
13
  require("colors");
13
- const cluster = require('cluster');
14
+ const cluster_1 = __importDefault(require("cluster"));
14
15
  //const pg = require('pg');
15
16
  //pg.types.setTypeParser(20, BigInt); // Type Id 20 = BIGINT | BIGSERIAL
16
17
  // https://stackoverflow.com/questions/39168501/pg-promise-returns-integers-as-strings
17
- const { Pool } = require('pg');
18
+ const pg_1 = require("pg");
18
19
  class PGPoolManager {
19
- constructor(options = null, instruments = null) {
20
+ constructor(options) {
20
21
  this._options = null;
21
22
  this._observer = null;
22
- this._instruments = null;
23
23
  this._pool = null;
24
- if (options === null) {
25
- options = { usedefault: false };
24
+ if (!options) {
25
+ options = {
26
+ usedefaultdb: false
27
+ };
26
28
  }
27
29
  this._options = options;
28
30
  const connectionStringURI = isProduction ? databaseUrl : (options.usedefaultdb === true ? defaultDatabaseConnectionString : connectionString);
29
31
  // https://mylifedigital.co.uk/securing-node-postgres-with-ssl/
30
- this._pool = new Pool({
32
+ this._pool = new pg_1.Pool({
31
33
  connectionString: connectionStringURI,
32
34
  ssl: isProduction,
33
35
  max: poolSize // Default is 10 - This will be the number in a pool per thread (worker)
@@ -48,7 +50,7 @@ class PGPoolManager {
48
50
  const sep = usernamepassword.split(':');
49
51
  const usernamepasswordReplacement = usernamepassword.replace(sep[1], '*****************************');
50
52
  const safeConnectionStringURI = connectionStringURI.replace(usernamepassword, usernamepasswordReplacement);
51
- if (cluster.isMaster) {
53
+ if (cluster_1.default.isPrimary) {
52
54
  debug(`Created Database Pool with Master Thread, PID: [${process.pid}]`.yellow);
53
55
  }
54
56
  else {
@@ -65,45 +67,60 @@ class PGPoolManager {
65
67
  process.exit(-1);
66
68
  });
67
69
  this._pool.on('connect', () => {
68
- this.UpdateInstruments(this);
70
+ this.UpdateInstruments();
69
71
  });
70
72
  this._pool.on('acquire', () => {
71
- this.UpdateInstruments(this);
73
+ this.UpdateInstruments();
72
74
  });
73
75
  this._pool.on('remove', () => {
74
- this.UpdateInstruments(this);
76
+ this.UpdateInstruments();
75
77
  });
76
- this.AttachInstruments(instruments);
78
+ this.AttachInstruments();
77
79
  }
78
80
  get pool() {
79
81
  return this._pool;
80
82
  }
81
- get instruments() {
83
+ /*
84
+ get instruments()
85
+ {
82
86
  return this._instruments;
83
87
  }
84
- UpdateInstruments(poolManager) {
85
- if (poolManager.instruments !== null &&
86
- typeof poolManager.instruments[Gauge.CONNECTION_POOL_TOTAL_GAUGE] !== 'undefined' &&
87
- typeof poolManager.instruments[Gauge.CONNECTION_POOL_IDLE_GAUGE] !== 'undefined' &&
88
- typeof poolManager.instruments[Gauge.CONNECTION_POOL_WAITING_GAUGE] !== 'undefined') {
89
- poolManager.instruments[Gauge.CONNECTION_POOL_TOTAL_GAUGE].val = poolManager.pool.totalCount;
90
- poolManager.instruments[Gauge.CONNECTION_POOL_IDLE_GAUGE].val = poolManager.pool.idleCount;
91
- poolManager.instruments[Gauge.CONNECTION_POOL_WAITING_GAUGE].val = poolManager.pool.waitingCount;
88
+ */
89
+ UpdateInstruments() {
90
+ if (stsutils_1.$stsgd.app) {
91
+ stsutils_1.$stsgd.app.UpdateInstrument(stsinstrumentation_1.Gauge.CONNECTION_POOL_TOTAL_GAUGE, {
92
+ val: this.pool.totalCount
93
+ });
94
+ stsutils_1.$stsgd.app.UpdateInstrument(stsinstrumentation_1.Gauge.CONNECTION_POOL_IDLE_GAUGE, {
95
+ val: this.pool.idleCount
96
+ });
97
+ stsutils_1.$stsgd.app.UpdateInstrument(stsinstrumentation_1.Gauge.CONNECTION_POOL_WAITING_GAUGE, {
98
+ val: this.pool.waitingCount
99
+ });
92
100
  }
93
101
  }
94
- AttachInstruments(instruments, interval = 1000) {
102
+ AttachInstruments(interval = 1000) {
103
+ if (stsutils_1.$stsgd.app) {
104
+ this._observer = setInterval(() => {
105
+ this.UpdateInstruments();
106
+ }, interval).unref();
107
+ }
108
+ /*
95
109
  this._instruments = instruments;
96
110
  if (instruments !== null &&
97
111
  typeof instruments[Gauge.CONNECTION_POOL_TOTAL_GAUGE] !== 'undefined' &&
98
112
  typeof instruments[Gauge.CONNECTION_POOL_IDLE_GAUGE] !== 'undefined' &&
99
- typeof instruments[Gauge.CONNECTION_POOL_WAITING_GAUGE] !== 'undefined') {
100
- this._observer = setInterval(() => {
113
+ typeof instruments[Gauge.CONNECTION_POOL_WAITING_GAUGE] !== 'undefined')
114
+ {
115
+ this._observer = setInterval(() =>
116
+ {
101
117
  this.UpdateInstruments(this);
102
118
  }, interval).unref();
103
119
  }
120
+ */
104
121
  }
105
122
  DetachInstruments() {
106
- this._instruments = null;
123
+ //this._instruments = null;
107
124
  clearInterval(this._observer);
108
125
  this._observer = null;
109
126
  }
@@ -1 +1 @@
1
- {"version":3,"file":"pgpoolmanager.js","sourceRoot":"","sources":["../pgpoolmanager.ts"],"names":[],"mappings":";;;;;;AAAA,kDAA6C;AAC7C,MAAM,EAAE,WAAW,EAAE,gBAAgB,EAAE,+BAA+B,EAAE,YAAY,EAAE,QAAQ,EAAE,GAAG,IAAA,oBAAQ,GAAE,CAAA;AAE7G,MAAM,EAAE,KAAK,EAAE,GAAG,OAAO,CAAC,6BAA6B,CAAC,CAAC;AAEzD,kDAA0B;AAC1B,MAAM,KAAK,GAAG,IAAA,eAAK,EAAC,QAAQ,OAAO,CAAC,GAAG,EAAE,CAAC,CAAC;AAE3C,kBAAe;AAEf,MAAM,OAAO,GAAG,OAAO,CAAC,SAAS,CAAC,CAAC;AAEnC,2BAA2B;AAC3B,wEAAwE;AACxE,sFAAsF;AACtF,MAAM,EAAE,IAAI,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;AAE/B,MAAa,aAAa;IAOzB,YAAY,OAAO,GAAG,IAAI,EAAE,WAAW,GAAG,IAAI;QALtC,aAAQ,GAAG,IAAI,CAAC;QAChB,cAAS,GAAiB,IAAI,CAAC;QAC/B,iBAAY,GAAG,IAAI,CAAC;QACpB,UAAK,GAAG,IAAI,CAAC;QAIpB,IAAI,OAAO,KAAK,IAAI,EAAE;YACrB,OAAO,GAAG,EAAE,UAAU,EAAE,KAAK,EAAE,CAAC;SAChC;QACD,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;QAExB,MAAM,mBAAmB,GAAG,YAAY,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,YAAY,KAAK,IAAI,CAAC,CAAC,CAAC,+BAA+B,CAAC,CAAC,CAAC,gBAAgB,CAAE,CAAC;QAC/I,+DAA+D;QAC/D,IAAI,CAAC,KAAK,GAAG,IAAI,IAAI,CAAC;YAC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AAU;aACG,CAAC,CAAC;YAE/B,iBAAM,CAAC,GAAG,CAAC,gBAAgB,CAAC,0BAAK,CAAC,0BAA0B,EAAE;gBAC7D,GAAG,EAAE,IAAI,CAAC,IAAI,CAAC,SAAS;aACI,CAAC,CAAC;YAE/B,iBAAM,CAAC,GAAG,CAAC,gBAAgB,CAAC,0BAAK,CAAC,6BAA6B,EAAE;gBAChE,GAAG,EAAE,IAAI,CAAC,IAAI,CAAC,YAAY;aACC,CAAC,CAAC;SAC/B;IACF,CAAC;IAED,iBAAiB,CAAC,QAAQ,GAAG,IAAI;QAEhC,IAAI,iBAAM,CAAC,GAAG,EAAE;YACf,IAAI,CAAC,SAAS,GAAG,WAAW,CAAC,GAAG,EAAE;gBAEjC,IAAI,CAAC,iBAAiB,EAAE,CAAC;YAC1B,CAAC,EAAE,QAAQ,CAAC,CAAC,KAAK,EAAE,CAAC;SACrB;QAED;;;;;;;;;;;;UAYE;IACH,CAAC;IAED,iBAAiB;QAEhB,2BAA2B;QAC3B,aAAa,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;QAC9B,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC;IACvB,CAAC;CACD;AAtID,sCAsIC"}
package/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "@nsshunt/stsdatamanagement",
3
- "version": "1.16.6",
3
+ "version": "1.16.9",
4
4
  "description": "STS Data Management Modules, Utilities and Services",
5
5
  "main": "dist/dbaccess.js",
6
6
  "dependencies": {
7
7
  "@nsshunt/stsconfig": "^1.24.9",
8
8
  "@nsshunt/stsinstrumentation": "^6.10.1",
9
9
  "@nsshunt/stsutils": "^1.14.9",
10
+ "@types/pg": "^8.6.5",
10
11
  "axios": "^0.27.2",
11
12
  "bcryptjs": "^2.4.3",
12
13
  "cli-progress": "^3.11.0",
package/pgpoolmanager.ts CHANGED
@@ -1,35 +1,41 @@
1
1
  import { $Options } from '@nsshunt/stsconfig'
2
2
  const { databaseUrl, connectionString, defaultDatabaseConnectionString, isProduction, poolSize } = $Options()
3
3
 
4
- const { Gauge } = require('@nsshunt/stsinstrumentation');
4
+ import { $stsgd } from '@nsshunt/stsutils'
5
+ import { Gauge, InstrumentGaugeTelemetry } from '@nsshunt/stsinstrumentation'
5
6
 
6
7
  import Debug from "debug";
7
8
  const debug = Debug(`proc:${process.pid}`);
8
9
 
9
10
  import 'colors'
10
11
 
11
- const cluster = require('cluster');
12
+ import cluster from 'cluster'
12
13
 
13
14
  //const pg = require('pg');
14
15
  //pg.types.setTypeParser(20, BigInt); // Type Id 20 = BIGINT | BIGSERIAL
15
16
  // https://stackoverflow.com/questions/39168501/pg-promise-returns-integers-as-strings
16
- const { Pool } = require('pg');
17
+ import { Pool, PoolConfig } from 'pg'
18
+
19
+ export interface PGPoolManagerOptions {
20
+ usedefaultdb: boolean
21
+ }
17
22
 
18
23
  export class PGPoolManager
19
24
  {
20
- private _options = null;
25
+ private _options: PGPoolManagerOptions = null;
21
26
  private _observer: NodeJS.Timer = null;
22
- private _instruments = null;
23
- private _pool = null;
27
+ private _pool: Pool = null;
24
28
 
25
- constructor(options = null, instruments = null)
29
+ constructor(options?: PGPoolManagerOptions)
26
30
  {
27
- if (options === null) {
28
- options = { usedefault: false };
31
+ if (!options) {
32
+ options = {
33
+ usedefaultdb: false
34
+ }
29
35
  }
30
36
  this._options = options;
31
37
 
32
- const connectionStringURI = isProduction ? databaseUrl : (options.usedefaultdb === true ? defaultDatabaseConnectionString : connectionString );
38
+ const connectionStringURI: string = isProduction ? databaseUrl : (options.usedefaultdb === true ? defaultDatabaseConnectionString : connectionString );
33
39
  // https://mylifedigital.co.uk/securing-node-postgres-with-ssl/
34
40
  this._pool = new Pool({
35
41
  connectionString: connectionStringURI,
@@ -46,15 +52,15 @@ export class PGPoolManager
46
52
  //cert: fs.readFileSync('/path/to/client-certificates/postgresql.crt').toString(),
47
53
  }
48
54
  */
49
- });
55
+ } as PoolConfig);
50
56
 
51
- const re = new RegExp('//(.*)@').exec(connectionStringURI);
52
- const usernamepassword = re[1];
53
- const sep = usernamepassword.split(':');
54
- const usernamepasswordReplacement = usernamepassword.replace(sep[1], '*****************************');
55
- const safeConnectionStringURI = connectionStringURI.replace(usernamepassword, usernamepasswordReplacement);
57
+ const re: RegExpExecArray = new RegExp('//(.*)@').exec(connectionStringURI);
58
+ const usernamepassword: string = re[1];
59
+ const sep: string[] = usernamepassword.split(':');
60
+ const usernamepasswordReplacement: string = usernamepassword.replace(sep[1], '*****************************');
61
+ const safeConnectionStringURI: string = connectionStringURI.replace(usernamepassword, usernamepasswordReplacement);
56
62
 
57
- if (cluster.isMaster) {
63
+ if (cluster.isPrimary) {
58
64
  debug(`Created Database Pool with Master Thread, PID: [${process.pid}]`.yellow);
59
65
  } else {
60
66
  debug(`Created Database Pool with Worker Thread, PID: [${process.pid}]`.yellow);
@@ -66,54 +72,68 @@ export class PGPoolManager
66
72
 
67
73
  // the pool will emit an error on behalf of any idle clients
68
74
  // it contains if a backend error or network partition happens
69
- this._pool.on('error', (error) => {
75
+ this._pool.on('error', (error: Error) => {
70
76
  console.error(`Unexpected error on idle client: [${error}]`.red);
71
77
  process.exit(-1)
72
78
  });
73
79
 
74
80
  this._pool.on('connect', () =>
75
81
  {
76
- this.UpdateInstruments(this);
82
+ this.UpdateInstruments();
77
83
  });
78
84
 
79
85
  this._pool.on('acquire', () =>
80
86
  {
81
- this.UpdateInstruments(this);
87
+ this.UpdateInstruments();
82
88
  });
83
89
 
84
90
  this._pool.on('remove', () =>
85
91
  {
86
- this.UpdateInstruments(this);
92
+ this.UpdateInstruments();
87
93
  });
88
94
 
89
- this.AttachInstruments(instruments);
95
+ this.AttachInstruments();
90
96
  }
91
97
 
92
- get pool()
98
+ get pool(): Pool
93
99
  {
94
100
  return this._pool;
95
101
  }
96
102
 
103
+ /*
97
104
  get instruments()
98
105
  {
99
106
  return this._instruments;
100
107
  }
108
+ */
101
109
 
102
- UpdateInstruments(poolManager)
110
+ UpdateInstruments()
103
111
  {
104
- if (poolManager.instruments !== null &&
105
- typeof poolManager.instruments[Gauge.CONNECTION_POOL_TOTAL_GAUGE] !== 'undefined' &&
106
- typeof poolManager.instruments[Gauge.CONNECTION_POOL_IDLE_GAUGE] !== 'undefined' &&
107
- typeof poolManager.instruments[Gauge.CONNECTION_POOL_WAITING_GAUGE] !== 'undefined')
108
- {
109
- poolManager.instruments[Gauge.CONNECTION_POOL_TOTAL_GAUGE].val = poolManager.pool.totalCount;
110
- poolManager.instruments[Gauge.CONNECTION_POOL_IDLE_GAUGE].val = poolManager.pool.idleCount;
111
- poolManager.instruments[Gauge.CONNECTION_POOL_WAITING_GAUGE].val = poolManager.pool.waitingCount;
112
+ if ($stsgd.app) {
113
+ $stsgd.app.UpdateInstrument(Gauge.CONNECTION_POOL_TOTAL_GAUGE, {
114
+ val: this.pool.totalCount
115
+ } as InstrumentGaugeTelemetry);
116
+
117
+ $stsgd.app.UpdateInstrument(Gauge.CONNECTION_POOL_IDLE_GAUGE, {
118
+ val: this.pool.idleCount
119
+ } as InstrumentGaugeTelemetry);
120
+
121
+ $stsgd.app.UpdateInstrument(Gauge.CONNECTION_POOL_WAITING_GAUGE, {
122
+ val: this.pool.waitingCount
123
+ } as InstrumentGaugeTelemetry);
112
124
  }
113
125
  }
114
126
 
115
- AttachInstruments(instruments, interval = 1000)
127
+ AttachInstruments(interval = 1000)
116
128
  {
129
+ if ($stsgd.app) {
130
+ this._observer = setInterval(() =>
131
+ {
132
+ this.UpdateInstruments();
133
+ }, interval).unref();
134
+ }
135
+
136
+ /*
117
137
  this._instruments = instruments;
118
138
  if (instruments !== null &&
119
139
  typeof instruments[Gauge.CONNECTION_POOL_TOTAL_GAUGE] !== 'undefined' &&
@@ -125,11 +145,12 @@ export class PGPoolManager
125
145
  this.UpdateInstruments(this);
126
146
  }, interval).unref();
127
147
  }
148
+ */
128
149
  }
129
150
 
130
151
  DetachInstruments()
131
152
  {
132
- this._instruments = null;
153
+ //this._instruments = null;
133
154
  clearInterval(this._observer);
134
155
  this._observer = null;
135
156
  }
@@ -1,14 +1,16 @@
1
1
  import 'colors';
2
+ import { Pool } from 'pg';
3
+ export interface PGPoolManagerOptions {
4
+ usedefaultdb: boolean;
5
+ }
2
6
  export declare class PGPoolManager {
3
7
  private _options;
4
8
  private _observer;
5
- private _instruments;
6
9
  private _pool;
7
- constructor(options?: any, instruments?: any);
8
- get pool(): any;
9
- get instruments(): any;
10
- UpdateInstruments(poolManager: any): void;
11
- AttachInstruments(instruments: any, interval?: number): void;
10
+ constructor(options?: PGPoolManagerOptions);
11
+ get pool(): Pool;
12
+ UpdateInstruments(): void;
13
+ AttachInstruments(interval?: number): void;
12
14
  DetachInstruments(): void;
13
15
  }
14
16
  //# sourceMappingURL=pgpoolmanager.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"pgpoolmanager.d.ts","sourceRoot":"","sources":["../pgpoolmanager.ts"],"names":[],"mappings":"AAQA,OAAO,QAAQ,CAAA;AASf,qBAAa,aAAa;IAEzB,OAAO,CAAC,QAAQ,CAAQ;IACxB,OAAO,CAAC,SAAS,CAAsB;IACvC,OAAO,CAAC,YAAY,CAAQ;IAC5B,OAAO,CAAC,KAAK,CAAQ;gBAET,OAAO,MAAO,EAAE,WAAW,MAAO;IAmE9C,IAAI,IAAI,QAGP;IAED,IAAI,WAAW,QAGd;IAED,iBAAiB,CAAC,WAAW,KAAA;IAa7B,iBAAiB,CAAC,WAAW,KAAA,EAAE,QAAQ,SAAO;IAe9C,iBAAiB;CAMjB"}
1
+ {"version":3,"file":"pgpoolmanager.d.ts","sourceRoot":"","sources":["../pgpoolmanager.ts"],"names":[],"mappings":"AASA,OAAO,QAAQ,CAAA;AAOf,OAAO,EAAE,IAAI,EAAc,MAAM,IAAI,CAAA;AAErC,MAAM,WAAW,oBAAoB;IACpC,YAAY,EAAE,OAAO,CAAA;CACrB;AAED,qBAAa,aAAa;IAEzB,OAAO,CAAC,QAAQ,CAA8B;IAC9C,OAAO,CAAC,SAAS,CAAsB;IACvC,OAAO,CAAC,KAAK,CAAc;gBAEf,OAAO,CAAC,EAAE,oBAAoB;IAqE1C,IAAI,IAAI,IAAI,IAAI,CAGf;IASD,iBAAiB;IAiBjB,iBAAiB,CAAC,QAAQ,SAAO;IAwBjC,iBAAiB;CAMjB"}
@@ -1,6 +1,6 @@
1
1
  export declare class PGUtils {
2
2
  static CheckDatabase(): Promise<void>;
3
- static createdatabase(): Promise<any>;
4
- static dropdatabase(): Promise<any>;
3
+ static createdatabase(): Promise<import("pg").QueryResult<any>>;
4
+ static dropdatabase(): Promise<import("pg").QueryResult<any>>;
5
5
  }
6
6
  //# sourceMappingURL=pgutils.d.ts.map