@oino-ts/db-postgresql 0.5.2 → 0.6.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.
@@ -93,20 +93,21 @@ class OINODbPostgresql extends db_1.OINODb {
93
93
  if (this._params.type !== "OINODbPostgresql") {
94
94
  throw new Error(db_1.OINO_ERROR_PREFIX + ": Not OINODbPostgresql-type: " + this._params.type);
95
95
  }
96
- const ssl_enabled = !(params.url == "localhost" || params.url == "127.0.0.1");
97
- this._pool = new pg_1.Pool({ host: params.url, database: params.database, port: params.port, user: params.user, password: params.password, ssl: ssl_enabled });
96
+ const ssl_enabled = !(this._params.url == "localhost" || this._params.url == "127.0.0.1");
97
+ this._pool = new pg_1.Pool({ host: this._params.url, database: this._params.database, port: this._params.port, user: this._params.user, password: this._params.password, ssl: ssl_enabled });
98
+ delete this._params.password;
98
99
  this._pool.on("error", (err) => {
99
- db_1.OINOLog.error("OINODbPostgresql error", { err: err });
100
- });
101
- this._pool.on("connect", (message) => {
102
- // OINOLog.info("OINODbPostgresql connect")
103
- });
104
- this._pool.on("release", (message) => {
105
- // OINOLog.info("OINODbPostgresql notice")
106
- });
107
- this._pool.on("acquire", () => {
108
- // OINOLog.info("OINODbPostgresql end")
100
+ db_1.OINOLog.error("OINODbPostgresql error event", { err: err });
109
101
  });
102
+ // this._pool.on("connect", (message: any) => {
103
+ // OINOLog.info("OINODbPostgresql connect")
104
+ // })
105
+ // this._pool.on("release", (message: any) => {
106
+ // OINOLog.info("OINODbPostgresql notice")
107
+ // })
108
+ // this._pool.on("acquire", () => {
109
+ // OINOLog.info("OINODbPostgresql end")
110
+ // })
110
111
  }
111
112
  _parseFieldLength(fieldLength) {
112
113
  let result = parseInt((fieldLength || "0").toString());
@@ -225,17 +226,50 @@ class OINODbPostgresql extends db_1.OINODb {
225
226
  *
226
227
  */
227
228
  async connect() {
229
+ let result = new db_1.OINOResult();
228
230
  try {
229
231
  // make sure that any items are correctly URL encoded in the connection string
230
232
  // OINOLog.debug("OINODbPostgresql.connect")
231
- // await this._pool.connect()
232
- // await this._client.connect()
233
- return Promise.resolve(true);
233
+ await this._pool.connect();
234
+ this.isConnected = true;
235
+ }
236
+ catch (err) {
237
+ result.setError(500, "Exception connecting to database: " + err.message, "OINODbPostgresql.connect");
238
+ db_1.OINOLog.error(result.statusMessage, { error: err });
239
+ }
240
+ return result;
241
+ }
242
+ /**
243
+ * Validate connection to database is working.
244
+ *
245
+ */
246
+ async validate() {
247
+ db_1.OINOBenchmark.start("OINODb", "validate");
248
+ let result = new db_1.OINOResult();
249
+ try {
250
+ const sql = this._getValidateSql(this._params.database);
251
+ // OINOLog.debug("OINODbPostgresql.validate", {sql:sql})
252
+ const sql_res = await this.sqlSelect(sql);
253
+ // OINOLog.debug("OINODbPostgresql.validate", {sql_res:sql_res})
254
+ if (sql_res.isEmpty()) {
255
+ result.setError(400, "DB returned no rows for select!", "OINODbPostgresql.validate");
256
+ }
257
+ else if (sql_res.getRow().length == 0) {
258
+ result.setError(400, "DB returned no values for database!", "OINODbPostgresql.validate");
259
+ }
260
+ else if (sql_res.getRow()[0] == "0") {
261
+ result.setError(400, "DB returned no schema for database!", "OINODbPostgresql.validate");
262
+ }
263
+ else {
264
+ this.isValidated = true;
265
+ }
234
266
  }
235
267
  catch (err) {
236
- // ... error checks
237
- throw new Error(db_1.OINO_ERROR_PREFIX + ": Error connecting to Postgresql server: " + err);
268
+ result.setError(500, "Exception validating connection: " + err.message, "OINODbPostgresql.validate");
269
+ db_1.OINOLog.error(result.statusMessage, { error: err });
238
270
  }
271
+ db_1.OINOBenchmark.end("OINODb", "validate");
272
+ return result;
239
273
  }
240
274
  /**
241
275
  * Execute a select operation.
@@ -303,6 +337,25 @@ LEFT JOIN LATERAL
303
337
  WHERE col.table_catalog = '${dbName}' AND col.table_name = '${tableName}'`;
304
338
  return sql;
305
339
  }
340
+ _getValidateSql(dbName) {
341
+ const sql = `SELECT
342
+ count(col.column_name) AS column_count
343
+ FROM information_schema.columns col
344
+ LEFT JOIN LATERAL
345
+ (select kcu.column_name, STRING_AGG(tco.constraint_type,',') as constraint_type
346
+ from
347
+ information_schema.table_constraints tco,
348
+ information_schema.key_column_usage kcu
349
+ where
350
+ kcu.constraint_name = tco.constraint_name
351
+ and kcu.constraint_schema = tco.constraint_schema
352
+ and tco.table_catalog = col.table_catalog
353
+ and tco.table_name = col.table_name
354
+ and (tco.constraint_type = 'PRIMARY KEY' OR tco.constraint_type = 'FOREIGN KEY')
355
+ group by kcu.column_name) con on col.column_name = con.column_name
356
+ WHERE col.table_catalog = '${dbName}'`;
357
+ return sql;
358
+ }
306
359
  /**
307
360
  * Initialize a data model by getting the SQL schema and populating OINODbDataFields of
308
361
  * the model.
@@ -3,7 +3,7 @@
3
3
  * License, v. 2.0. If a copy of the MPL was not distributed with this
4
4
  * file, You can obtain one at https://mozilla.org/MPL/2.0/.
5
5
  */
6
- import { OINODb, OINODbDataSet, OINOBooleanDataField, OINONumberDataField, OINOStringDataField, OINO_ERROR_PREFIX, OINOBenchmark, OINODatetimeDataField, OINOBlobDataField, OINOLog } from "@oino-ts/db";
6
+ import { OINODb, OINODbDataSet, OINOBooleanDataField, OINONumberDataField, OINOStringDataField, OINO_ERROR_PREFIX, OINOBenchmark, OINODatetimeDataField, OINOBlobDataField, OINOLog, OINOResult } from "@oino-ts/db";
7
7
  import { Pool } from "pg";
8
8
  const EMPTY_ROW = [];
9
9
  /**
@@ -90,20 +90,21 @@ export class OINODbPostgresql extends OINODb {
90
90
  if (this._params.type !== "OINODbPostgresql") {
91
91
  throw new Error(OINO_ERROR_PREFIX + ": Not OINODbPostgresql-type: " + this._params.type);
92
92
  }
93
- const ssl_enabled = !(params.url == "localhost" || params.url == "127.0.0.1");
94
- this._pool = new Pool({ host: params.url, database: params.database, port: params.port, user: params.user, password: params.password, ssl: ssl_enabled });
93
+ const ssl_enabled = !(this._params.url == "localhost" || this._params.url == "127.0.0.1");
94
+ this._pool = new Pool({ host: this._params.url, database: this._params.database, port: this._params.port, user: this._params.user, password: this._params.password, ssl: ssl_enabled });
95
+ delete this._params.password;
95
96
  this._pool.on("error", (err) => {
96
- OINOLog.error("OINODbPostgresql error", { err: err });
97
- });
98
- this._pool.on("connect", (message) => {
99
- // OINOLog.info("OINODbPostgresql connect")
100
- });
101
- this._pool.on("release", (message) => {
102
- // OINOLog.info("OINODbPostgresql notice")
103
- });
104
- this._pool.on("acquire", () => {
105
- // OINOLog.info("OINODbPostgresql end")
97
+ OINOLog.error("OINODbPostgresql error event", { err: err });
106
98
  });
99
+ // this._pool.on("connect", (message: any) => {
100
+ // OINOLog.info("OINODbPostgresql connect")
101
+ // })
102
+ // this._pool.on("release", (message: any) => {
103
+ // OINOLog.info("OINODbPostgresql notice")
104
+ // })
105
+ // this._pool.on("acquire", () => {
106
+ // OINOLog.info("OINODbPostgresql end")
107
+ // })
107
108
  }
108
109
  _parseFieldLength(fieldLength) {
109
110
  let result = parseInt((fieldLength || "0").toString());
@@ -222,17 +223,50 @@ export class OINODbPostgresql extends OINODb {
222
223
  *
223
224
  */
224
225
  async connect() {
226
+ let result = new OINOResult();
225
227
  try {
226
228
  // make sure that any items are correctly URL encoded in the connection string
227
229
  // OINOLog.debug("OINODbPostgresql.connect")
228
- // await this._pool.connect()
229
- // await this._client.connect()
230
- return Promise.resolve(true);
230
+ await this._pool.connect();
231
+ this.isConnected = true;
232
+ }
233
+ catch (err) {
234
+ result.setError(500, "Exception connecting to database: " + err.message, "OINODbPostgresql.connect");
235
+ OINOLog.error(result.statusMessage, { error: err });
236
+ }
237
+ return result;
238
+ }
239
+ /**
240
+ * Validate connection to database is working.
241
+ *
242
+ */
243
+ async validate() {
244
+ OINOBenchmark.start("OINODb", "validate");
245
+ let result = new OINOResult();
246
+ try {
247
+ const sql = this._getValidateSql(this._params.database);
248
+ // OINOLog.debug("OINODbPostgresql.validate", {sql:sql})
249
+ const sql_res = await this.sqlSelect(sql);
250
+ // OINOLog.debug("OINODbPostgresql.validate", {sql_res:sql_res})
251
+ if (sql_res.isEmpty()) {
252
+ result.setError(400, "DB returned no rows for select!", "OINODbPostgresql.validate");
253
+ }
254
+ else if (sql_res.getRow().length == 0) {
255
+ result.setError(400, "DB returned no values for database!", "OINODbPostgresql.validate");
256
+ }
257
+ else if (sql_res.getRow()[0] == "0") {
258
+ result.setError(400, "DB returned no schema for database!", "OINODbPostgresql.validate");
259
+ }
260
+ else {
261
+ this.isValidated = true;
262
+ }
231
263
  }
232
264
  catch (err) {
233
- // ... error checks
234
- throw new Error(OINO_ERROR_PREFIX + ": Error connecting to Postgresql server: " + err);
265
+ result.setError(500, "Exception validating connection: " + err.message, "OINODbPostgresql.validate");
266
+ OINOLog.error(result.statusMessage, { error: err });
235
267
  }
268
+ OINOBenchmark.end("OINODb", "validate");
269
+ return result;
236
270
  }
237
271
  /**
238
272
  * Execute a select operation.
@@ -300,6 +334,25 @@ LEFT JOIN LATERAL
300
334
  WHERE col.table_catalog = '${dbName}' AND col.table_name = '${tableName}'`;
301
335
  return sql;
302
336
  }
337
+ _getValidateSql(dbName) {
338
+ const sql = `SELECT
339
+ count(col.column_name) AS column_count
340
+ FROM information_schema.columns col
341
+ LEFT JOIN LATERAL
342
+ (select kcu.column_name, STRING_AGG(tco.constraint_type,',') as constraint_type
343
+ from
344
+ information_schema.table_constraints tco,
345
+ information_schema.key_column_usage kcu
346
+ where
347
+ kcu.constraint_name = tco.constraint_name
348
+ and kcu.constraint_schema = tco.constraint_schema
349
+ and tco.table_catalog = col.table_catalog
350
+ and tco.table_name = col.table_name
351
+ and (tco.constraint_type = 'PRIMARY KEY' OR tco.constraint_type = 'FOREIGN KEY')
352
+ group by kcu.column_name) con on col.column_name = con.column_name
353
+ WHERE col.table_catalog = '${dbName}'`;
354
+ return sql;
355
+ }
303
356
  /**
304
357
  * Initialize a data model by getting the SQL schema and populating OINODbDataFields of
305
358
  * the model.
@@ -1,4 +1,4 @@
1
- import { OINODb, OINODbParams, OINODbDataSet, OINODbApi, OINODataCell } from "@oino-ts/db";
1
+ import { OINODb, OINODbParams, OINODbDataSet, OINODbApi, OINODataCell, OINOResult } from "@oino-ts/db";
2
2
  /**
3
3
  * Implementation of Postgresql-database.
4
4
  *
@@ -56,7 +56,12 @@ export declare class OINODbPostgresql extends OINODb {
56
56
  * Connect to database.
57
57
  *
58
58
  */
59
- connect(): Promise<boolean>;
59
+ connect(): Promise<OINOResult>;
60
+ /**
61
+ * Validate connection to database is working.
62
+ *
63
+ */
64
+ validate(): Promise<OINOResult>;
60
65
  /**
61
66
  * Execute a select operation.
62
67
  *
@@ -72,6 +77,7 @@ export declare class OINODbPostgresql extends OINODb {
72
77
  */
73
78
  sqlExec(sql: string): Promise<OINODbDataSet>;
74
79
  private _getSchemaSql;
80
+ private _getValidateSql;
75
81
  /**
76
82
  * Initialize a data model by getting the SQL schema and populating OINODbDataFields of
77
83
  * the model.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oino-ts/db-postgresql",
3
- "version": "0.5.2",
3
+ "version": "0.6.1",
4
4
  "description": "OINO TS package for using Postgresql databases.",
5
5
  "author": "Matias Kiviniemi (pragmatta)",
6
6
  "license": "MPL-2.0",
@@ -20,7 +20,7 @@
20
20
  "module": "./dist/esm/index.js",
21
21
  "types": "./dist/types/index.d.ts",
22
22
  "dependencies": {
23
- "@oino-ts/db": "0.5.2",
23
+ "@oino-ts/db": "0.6.1",
24
24
  "pg": "^8.11.3"
25
25
  },
26
26
  "devDependencies": {
@@ -4,7 +4,7 @@
4
4
  * file, You can obtain one at https://mozilla.org/MPL/2.0/.
5
5
  */
6
6
 
7
- import { OINODb, OINODbParams, OINODbDataSet, OINODbApi, OINOBooleanDataField, OINONumberDataField, OINOStringDataField, OINODbDataFieldParams, OINO_ERROR_PREFIX, OINODataRow, OINODataCell, OINOBenchmark, OINODatetimeDataField, OINOBlobDataField, OINOLog } from "@oino-ts/db";
7
+ import { OINODb, OINODbParams, OINODbDataSet, OINODbApi, OINOBooleanDataField, OINONumberDataField, OINOStringDataField, OINODbDataFieldParams, OINO_ERROR_PREFIX, OINODataRow, OINODataCell, OINOBenchmark, OINODatetimeDataField, OINOBlobDataField, OINOLog, OINOResult } from "@oino-ts/db";
8
8
 
9
9
  import { Pool, QueryResult } from "pg";
10
10
 
@@ -101,20 +101,22 @@ export class OINODbPostgresql extends OINODb {
101
101
  if (this._params.type !== "OINODbPostgresql") {
102
102
  throw new Error(OINO_ERROR_PREFIX + ": Not OINODbPostgresql-type: " + this._params.type)
103
103
  }
104
- const ssl_enabled:boolean = !(params.url == "localhost" || params.url == "127.0.0.1")
105
- this._pool = new Pool({ host: params.url, database: params.database, port: params.port, user: params.user, password: params.password, ssl: ssl_enabled })
104
+ const ssl_enabled:boolean = !(this._params.url == "localhost" || this._params.url == "127.0.0.1")
105
+ this._pool = new Pool({ host: this._params.url, database: this._params.database, port: this._params.port, user: this._params.user, password: this._params.password, ssl: ssl_enabled })
106
+ delete this._params.password
107
+
106
108
  this._pool.on("error", (err: any) => {
107
- OINOLog.error("OINODbPostgresql error", {err:err})
108
- })
109
- this._pool.on("connect", (message: any) => {
110
- // OINOLog.info("OINODbPostgresql connect")
111
- })
112
- this._pool.on("release", (message: any) => {
113
- // OINOLog.info("OINODbPostgresql notice")
114
- })
115
- this._pool.on("acquire", () => {
116
- // OINOLog.info("OINODbPostgresql end")
109
+ OINOLog.error("OINODbPostgresql error event", {err:err})
117
110
  })
111
+ // this._pool.on("connect", (message: any) => {
112
+ // OINOLog.info("OINODbPostgresql connect")
113
+ // })
114
+ // this._pool.on("release", (message: any) => {
115
+ // OINOLog.info("OINODbPostgresql notice")
116
+ // })
117
+ // this._pool.on("acquire", () => {
118
+ // OINOLog.info("OINODbPostgresql end")
119
+ // })
118
120
  }
119
121
 
120
122
  private _parseFieldLength(fieldLength:OINODataCell):number {
@@ -240,17 +242,51 @@ export class OINODbPostgresql extends OINODb {
240
242
  * Connect to database.
241
243
  *
242
244
  */
243
- async connect(): Promise<boolean> {
245
+ async connect(): Promise<OINOResult> {
246
+ let result:OINOResult = new OINOResult()
244
247
  try {
245
248
  // make sure that any items are correctly URL encoded in the connection string
246
249
  // OINOLog.debug("OINODbPostgresql.connect")
247
- // await this._pool.connect()
248
- // await this._client.connect()
249
- return Promise.resolve(true)
250
- } catch (err) {
251
- // ... error checks
252
- throw new Error(OINO_ERROR_PREFIX + ": Error connecting to Postgresql server: " + err)
250
+ await this._pool.connect()
251
+ this.isConnected = true
252
+
253
+ } catch (err:any) {
254
+ result.setError(500, "Exception connecting to database: " + err.message, "OINODbPostgresql.connect")
255
+ OINOLog.error(result.statusMessage, {error:err})
253
256
  }
257
+ return result
258
+ }
259
+
260
+ /**
261
+ * Validate connection to database is working.
262
+ *
263
+ */
264
+ async validate(): Promise<OINOResult> {
265
+ OINOBenchmark.start("OINODb", "validate")
266
+ let result:OINOResult = new OINOResult()
267
+ try {
268
+ const sql = this._getValidateSql(this._params.database)
269
+ // OINOLog.debug("OINODbPostgresql.validate", {sql:sql})
270
+ const sql_res:OINODbDataSet = await this.sqlSelect(sql)
271
+ // OINOLog.debug("OINODbPostgresql.validate", {sql_res:sql_res})
272
+ if (sql_res.isEmpty()) {
273
+ result.setError(400, "DB returned no rows for select!", "OINODbPostgresql.validate")
274
+
275
+ } else if (sql_res.getRow().length == 0) {
276
+ result.setError(400, "DB returned no values for database!", "OINODbPostgresql.validate")
277
+
278
+ } else if (sql_res.getRow()[0] == "0") {
279
+ result.setError(400, "DB returned no schema for database!", "OINODbPostgresql.validate")
280
+
281
+ } else {
282
+ this.isValidated = true
283
+ }
284
+ } catch (err:any) {
285
+ result.setError(500, "Exception validating connection: " + err.message, "OINODbPostgresql.validate")
286
+ OINOLog.error(result.statusMessage, {error:err})
287
+ }
288
+ OINOBenchmark.end("OINODb", "validate")
289
+ return result
254
290
  }
255
291
 
256
292
  /**
@@ -323,6 +359,27 @@ WHERE col.table_catalog = '${dbName}' AND col.table_name = '${tableName}'`
323
359
  return sql
324
360
  }
325
361
 
362
+ private _getValidateSql(dbName:string):string {
363
+ const sql =
364
+ `SELECT
365
+ count(col.column_name) AS column_count
366
+ FROM information_schema.columns col
367
+ LEFT JOIN LATERAL
368
+ (select kcu.column_name, STRING_AGG(tco.constraint_type,',') as constraint_type
369
+ from
370
+ information_schema.table_constraints tco,
371
+ information_schema.key_column_usage kcu
372
+ where
373
+ kcu.constraint_name = tco.constraint_name
374
+ and kcu.constraint_schema = tco.constraint_schema
375
+ and tco.table_catalog = col.table_catalog
376
+ and tco.table_name = col.table_name
377
+ and (tco.constraint_type = 'PRIMARY KEY' OR tco.constraint_type = 'FOREIGN KEY')
378
+ group by kcu.column_name) con on col.column_name = con.column_name
379
+ WHERE col.table_catalog = '${dbName}'`
380
+ return sql
381
+ }
382
+
326
383
  /**
327
384
  * Initialize a data model by getting the SQL schema and populating OINODbDataFields of
328
385
  * the model.