@oino-ts/db-postgresql 0.5.2 → 0.6.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.
@@ -237,6 +237,37 @@ class OINODbPostgresql extends db_1.OINODb {
237
237
  throw new Error(db_1.OINO_ERROR_PREFIX + ": Error connecting to Postgresql server: " + err);
238
238
  }
239
239
  }
240
+ /**
241
+ * Validate connection to database is working.
242
+ *
243
+ */
244
+ async validate() {
245
+ db_1.OINOBenchmark.start("OINODb", "validate");
246
+ let result = new db_1.OINOResult();
247
+ try {
248
+ const sql = this._getValidateSql(this._params.database);
249
+ // OINOLog.debug("OINODbBunSqlite.validate", {sql:sql})
250
+ const sql_res = await this.sqlSelect(sql);
251
+ // OINOLog.debug("OINODbBunSqlite.validate", {sql_res:sql_res})
252
+ if (sql_res.isEmpty()) {
253
+ result.setError(400, "DB returned no rows for select!", "OINODbBunSqlite.validate");
254
+ }
255
+ else if (sql_res.getRow().length == 0) {
256
+ result.setError(400, "DB returned no values for database!", "OINODbBunSqlite.validate");
257
+ }
258
+ else if (sql_res.getRow()[0] == "0") {
259
+ result.setError(400, "DB returned no schema for database!", "OINODbBunSqlite.validate");
260
+ }
261
+ else {
262
+ // connection is working
263
+ }
264
+ }
265
+ catch (e) {
266
+ result.setError(500, db_1.OINO_ERROR_PREFIX + " (validate): OINODbBunSqlite.validate exception in _db.query: " + e.message, "OINODbBunSqlite.validate");
267
+ }
268
+ db_1.OINOBenchmark.end("OINODb", "validate");
269
+ return result;
270
+ }
240
271
  /**
241
272
  * Execute a select operation.
242
273
  *
@@ -303,6 +334,25 @@ LEFT JOIN LATERAL
303
334
  WHERE col.table_catalog = '${dbName}' AND col.table_name = '${tableName}'`;
304
335
  return sql;
305
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
+ }
306
356
  /**
307
357
  * Initialize a data model by getting the SQL schema and populating OINODbDataFields of
308
358
  * 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
  /**
@@ -234,6 +234,37 @@ export class OINODbPostgresql extends OINODb {
234
234
  throw new Error(OINO_ERROR_PREFIX + ": Error connecting to Postgresql server: " + err);
235
235
  }
236
236
  }
237
+ /**
238
+ * Validate connection to database is working.
239
+ *
240
+ */
241
+ async validate() {
242
+ OINOBenchmark.start("OINODb", "validate");
243
+ let result = new OINOResult();
244
+ try {
245
+ const sql = this._getValidateSql(this._params.database);
246
+ // OINOLog.debug("OINODbBunSqlite.validate", {sql:sql})
247
+ const sql_res = await this.sqlSelect(sql);
248
+ // OINOLog.debug("OINODbBunSqlite.validate", {sql_res:sql_res})
249
+ if (sql_res.isEmpty()) {
250
+ result.setError(400, "DB returned no rows for select!", "OINODbBunSqlite.validate");
251
+ }
252
+ else if (sql_res.getRow().length == 0) {
253
+ result.setError(400, "DB returned no values for database!", "OINODbBunSqlite.validate");
254
+ }
255
+ else if (sql_res.getRow()[0] == "0") {
256
+ result.setError(400, "DB returned no schema for database!", "OINODbBunSqlite.validate");
257
+ }
258
+ else {
259
+ // connection is working
260
+ }
261
+ }
262
+ catch (e) {
263
+ result.setError(500, OINO_ERROR_PREFIX + " (validate): OINODbBunSqlite.validate exception in _db.query: " + e.message, "OINODbBunSqlite.validate");
264
+ }
265
+ OINOBenchmark.end("OINODb", "validate");
266
+ return result;
267
+ }
237
268
  /**
238
269
  * Execute a select operation.
239
270
  *
@@ -300,6 +331,25 @@ LEFT JOIN LATERAL
300
331
  WHERE col.table_catalog = '${dbName}' AND col.table_name = '${tableName}'`;
301
332
  return sql;
302
333
  }
334
+ _getValidateSql(dbName) {
335
+ const sql = `SELECT
336
+ count(col.column_name) AS column_count
337
+ FROM information_schema.columns col
338
+ LEFT JOIN LATERAL
339
+ (select kcu.column_name, STRING_AGG(tco.constraint_type,',') as constraint_type
340
+ from
341
+ information_schema.table_constraints tco,
342
+ information_schema.key_column_usage kcu
343
+ where
344
+ kcu.constraint_name = tco.constraint_name
345
+ and kcu.constraint_schema = tco.constraint_schema
346
+ and tco.table_catalog = col.table_catalog
347
+ and tco.table_name = col.table_name
348
+ and (tco.constraint_type = 'PRIMARY KEY' OR tco.constraint_type = 'FOREIGN KEY')
349
+ group by kcu.column_name) con on col.column_name = con.column_name
350
+ WHERE col.table_catalog = '${dbName}'`;
351
+ return sql;
352
+ }
303
353
  /**
304
354
  * Initialize a data model by getting the SQL schema and populating OINODbDataFields of
305
355
  * 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
  *
@@ -57,6 +57,11 @@ export declare class OINODbPostgresql extends OINODb {
57
57
  *
58
58
  */
59
59
  connect(): Promise<boolean>;
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.0",
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.0",
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
 
@@ -253,6 +253,37 @@ export class OINODbPostgresql extends OINODb {
253
253
  }
254
254
  }
255
255
 
256
+ /**
257
+ * Validate connection to database is working.
258
+ *
259
+ */
260
+ async validate(): Promise<OINOResult> {
261
+ OINOBenchmark.start("OINODb", "validate")
262
+ let result:OINOResult = new OINOResult()
263
+ try {
264
+ const sql = this._getValidateSql(this._params.database)
265
+ // OINOLog.debug("OINODbBunSqlite.validate", {sql:sql})
266
+ const sql_res:OINODbDataSet = await this.sqlSelect(sql)
267
+ // OINOLog.debug("OINODbBunSqlite.validate", {sql_res:sql_res})
268
+ if (sql_res.isEmpty()) {
269
+ result.setError(400, "DB returned no rows for select!", "OINODbBunSqlite.validate")
270
+
271
+ } else if (sql_res.getRow().length == 0) {
272
+ result.setError(400, "DB returned no values for database!", "OINODbBunSqlite.validate")
273
+
274
+ } else if (sql_res.getRow()[0] == "0") {
275
+ result.setError(400, "DB returned no schema for database!", "OINODbBunSqlite.validate")
276
+
277
+ } else {
278
+ // connection is working
279
+ }
280
+ } catch (e:any) {
281
+ result.setError(500, OINO_ERROR_PREFIX + " (validate): OINODbBunSqlite.validate exception in _db.query: " + e.message, "OINODbBunSqlite.validate")
282
+ }
283
+ OINOBenchmark.end("OINODb", "validate")
284
+ return result
285
+ }
286
+
256
287
  /**
257
288
  * Execute a select operation.
258
289
  *
@@ -323,6 +354,27 @@ WHERE col.table_catalog = '${dbName}' AND col.table_name = '${tableName}'`
323
354
  return sql
324
355
  }
325
356
 
357
+ private _getValidateSql(dbName:string):string {
358
+ const sql =
359
+ `SELECT
360
+ count(col.column_name) AS column_count
361
+ FROM information_schema.columns col
362
+ LEFT JOIN LATERAL
363
+ (select kcu.column_name, STRING_AGG(tco.constraint_type,',') as constraint_type
364
+ from
365
+ information_schema.table_constraints tco,
366
+ information_schema.key_column_usage kcu
367
+ where
368
+ kcu.constraint_name = tco.constraint_name
369
+ and kcu.constraint_schema = tco.constraint_schema
370
+ and tco.table_catalog = col.table_catalog
371
+ and tco.table_name = col.table_name
372
+ and (tco.constraint_type = 'PRIMARY KEY' OR tco.constraint_type = 'FOREIGN KEY')
373
+ group by kcu.column_name) con on col.column_name = con.column_name
374
+ WHERE col.table_catalog = '${dbName}'`
375
+ return sql
376
+ }
377
+
326
378
  /**
327
379
  * Initialize a data model by getting the SQL schema and populating OINODbDataFields of
328
380
  * the model.