@oino-ts/db-postgresql 0.17.5 → 0.19.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.
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
*/
|
|
7
7
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
8
|
exports.OINODbPostgresql = void 0;
|
|
9
|
+
const common_1 = require("@oino-ts/common");
|
|
9
10
|
const db_1 = require("@oino-ts/db");
|
|
10
11
|
const pg_1 = require("pg");
|
|
11
12
|
/**
|
|
@@ -21,7 +22,7 @@ class OINOPostgresqlData extends db_1.OINODbDataSet {
|
|
|
21
22
|
constructor(data, messages = []) {
|
|
22
23
|
super(data, messages);
|
|
23
24
|
if ((data != null) && !(Array.isArray(data))) {
|
|
24
|
-
throw new Error(
|
|
25
|
+
throw new Error(common_1.OINO_ERROR_PREFIX + ": Invalid Posgresql data type!"); // TODO: maybe check all rows
|
|
25
26
|
}
|
|
26
27
|
this._rows = data;
|
|
27
28
|
if (this.isEmpty()) {
|
|
@@ -95,13 +96,13 @@ class OINODbPostgresql extends db_1.OINODb {
|
|
|
95
96
|
constructor(params) {
|
|
96
97
|
super(params);
|
|
97
98
|
if (this._params.type !== "OINODbPostgresql") {
|
|
98
|
-
throw new Error(
|
|
99
|
+
throw new Error(common_1.OINO_ERROR_PREFIX + ": Not OINODbPostgresql-type: " + this._params.type);
|
|
99
100
|
}
|
|
100
101
|
const ssl_enabled = !(this._params.url == "localhost" || this._params.url == "127.0.0.1");
|
|
101
102
|
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 });
|
|
102
103
|
delete this._params.password;
|
|
103
104
|
this._pool.on("error", (err) => {
|
|
104
|
-
|
|
105
|
+
common_1.OINOLog.error("@oino-ts/db-postgresql", "OINODbPostgresql", ".on(error)", "Error-event", { err: err });
|
|
105
106
|
});
|
|
106
107
|
}
|
|
107
108
|
_parseFieldLength(fieldLength) {
|
|
@@ -225,7 +226,7 @@ class OINODbPostgresql extends db_1.OINODb {
|
|
|
225
226
|
*
|
|
226
227
|
*/
|
|
227
228
|
async connect() {
|
|
228
|
-
let result = new
|
|
229
|
+
let result = new common_1.OINOResult();
|
|
229
230
|
try {
|
|
230
231
|
// make sure that any items are correctly URL encoded in the connection string
|
|
231
232
|
await this._pool.connect();
|
|
@@ -233,7 +234,7 @@ class OINODbPostgresql extends db_1.OINODb {
|
|
|
233
234
|
}
|
|
234
235
|
catch (e) {
|
|
235
236
|
result.setError(500, "Exception connecting to database: " + e.message, "OINODbPostgresql.connect");
|
|
236
|
-
|
|
237
|
+
common_1.OINOLog.exception("@oino-ts/db-postgresql", "OINODbPostgresql", "connect", "exception in connect", { message: e.message, stack: e.stack });
|
|
237
238
|
}
|
|
238
239
|
return result;
|
|
239
240
|
}
|
|
@@ -242,8 +243,8 @@ class OINODbPostgresql extends db_1.OINODb {
|
|
|
242
243
|
*
|
|
243
244
|
*/
|
|
244
245
|
async validate() {
|
|
245
|
-
|
|
246
|
-
let result = new
|
|
246
|
+
common_1.OINOBenchmark.startMetric("OINODb", "validate");
|
|
247
|
+
let result = new common_1.OINOResult();
|
|
247
248
|
try {
|
|
248
249
|
const sql = this._getValidateSql(this._params.database);
|
|
249
250
|
const sql_res = await this.sqlSelect(sql);
|
|
@@ -262,9 +263,9 @@ class OINODbPostgresql extends db_1.OINODb {
|
|
|
262
263
|
}
|
|
263
264
|
catch (e) {
|
|
264
265
|
result.setError(500, "Exception validating connection: " + e.message, "OINODbPostgresql.validate");
|
|
265
|
-
|
|
266
|
+
common_1.OINOLog.exception("@oino-ts/db-postgresql", "OINODbPostgresql", "validate", "exception in validate", { message: e.message, stack: e.stack });
|
|
266
267
|
}
|
|
267
|
-
|
|
268
|
+
common_1.OINOBenchmark.endMetric("OINODb", "validate");
|
|
268
269
|
return result;
|
|
269
270
|
}
|
|
270
271
|
/**
|
|
@@ -274,16 +275,16 @@ class OINODbPostgresql extends db_1.OINODb {
|
|
|
274
275
|
*
|
|
275
276
|
*/
|
|
276
277
|
async sqlSelect(sql) {
|
|
277
|
-
|
|
278
|
+
common_1.OINOBenchmark.startMetric("OINODb", "sqlSelect");
|
|
278
279
|
let result;
|
|
279
280
|
try {
|
|
280
281
|
const rows = await this._query(sql);
|
|
281
282
|
result = new OINOPostgresqlData(rows, []);
|
|
282
283
|
}
|
|
283
284
|
catch (e) {
|
|
284
|
-
result = new OINOPostgresqlData(db_1.OINODB_EMPTY_ROWS, [
|
|
285
|
+
result = new OINOPostgresqlData(db_1.OINODB_EMPTY_ROWS, [common_1.OINO_ERROR_PREFIX + " (sqlSelect): exception in _db.query [" + e.message + "]"]);
|
|
285
286
|
}
|
|
286
|
-
|
|
287
|
+
common_1.OINOBenchmark.endMetric("OINODb", "sqlSelect");
|
|
287
288
|
return result;
|
|
288
289
|
}
|
|
289
290
|
/**
|
|
@@ -293,16 +294,16 @@ class OINODbPostgresql extends db_1.OINODb {
|
|
|
293
294
|
*
|
|
294
295
|
*/
|
|
295
296
|
async sqlExec(sql) {
|
|
296
|
-
|
|
297
|
+
common_1.OINOBenchmark.startMetric("OINODb", "sqlExec");
|
|
297
298
|
let result;
|
|
298
299
|
try {
|
|
299
300
|
const rows = await this._exec(sql);
|
|
300
301
|
result = new OINOPostgresqlData(rows, []);
|
|
301
302
|
}
|
|
302
303
|
catch (e) {
|
|
303
|
-
result = new OINOPostgresqlData(db_1.OINODB_EMPTY_ROWS, [
|
|
304
|
+
result = new OINOPostgresqlData(db_1.OINODB_EMPTY_ROWS, [common_1.OINO_ERROR_PREFIX + " (sqlExec): exception in _db.exec [" + e.message + "]"]);
|
|
304
305
|
}
|
|
305
|
-
|
|
306
|
+
common_1.OINOBenchmark.endMetric("OINODb", "sqlExec");
|
|
306
307
|
return result;
|
|
307
308
|
}
|
|
308
309
|
_getSchemaSql(dbName, tableName) {
|
|
@@ -375,9 +376,9 @@ WHERE col.table_catalog = '${dbName}'`;
|
|
|
375
376
|
isAutoInc: default_val.startsWith("nextval(")
|
|
376
377
|
};
|
|
377
378
|
if (api.isFieldIncluded(field_name) == false) {
|
|
378
|
-
|
|
379
|
+
common_1.OINOLog.info("@oino-ts/db-postgresql", "OINODbPostgresql", "initializeApiDatamodel", "Field excluded in API parameters.", { field: field_name });
|
|
379
380
|
if (field_params.isPrimaryKey) {
|
|
380
|
-
throw new Error(
|
|
381
|
+
throw new Error(common_1.OINO_ERROR_PREFIX + "Primary key field excluded in API parameters: " + field_name);
|
|
381
382
|
}
|
|
382
383
|
}
|
|
383
384
|
else {
|
|
@@ -405,13 +406,13 @@ WHERE col.table_catalog = '${dbName}'`;
|
|
|
405
406
|
api.datamodel.addField(new db_1.OINOStringDataField(this, field_name, sql_type, field_params, numeric_precision + numeric_scale + 1));
|
|
406
407
|
}
|
|
407
408
|
else {
|
|
408
|
-
|
|
409
|
+
common_1.OINOLog.info("@oino-ts/db-postgresql", "OINODbPostgresql", "initializeApiDatamodel", "Unrecognized field type treated as string", { field_name: field_name, sql_type: sql_type, field_length: field_length, field_params: field_params });
|
|
409
410
|
api.datamodel.addField(new db_1.OINOStringDataField(this, field_name, sql_type, field_params, 0));
|
|
410
411
|
}
|
|
411
412
|
}
|
|
412
413
|
await schema_res.next();
|
|
413
414
|
}
|
|
414
|
-
|
|
415
|
+
common_1.OINOLog.info("@oino-ts/db-postgresql", "OINODbPostgresql", "initializeApiDatamodel", "\n" + api.datamodel.printDebug("\n"));
|
|
415
416
|
return Promise.resolve();
|
|
416
417
|
}
|
|
417
418
|
}
|
|
@@ -3,7 +3,8 @@
|
|
|
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 {
|
|
6
|
+
import { OINO_ERROR_PREFIX, OINOBenchmark, OINOLog, OINOResult } from "@oino-ts/common";
|
|
7
|
+
import { OINODb, OINODbDataSet, OINOBooleanDataField, OINONumberDataField, OINOStringDataField, OINODatetimeDataField, OINOBlobDataField, OINODB_EMPTY_ROW, OINODB_EMPTY_ROWS } from "@oino-ts/db";
|
|
7
8
|
import { Pool } from "pg";
|
|
8
9
|
/**
|
|
9
10
|
* Implmentation of OINODbDataSet for Postgresql.
|
|
@@ -230,7 +231,7 @@ export class OINODbPostgresql extends OINODb {
|
|
|
230
231
|
}
|
|
231
232
|
catch (e) {
|
|
232
233
|
result.setError(500, "Exception connecting to database: " + e.message, "OINODbPostgresql.connect");
|
|
233
|
-
OINOLog.exception("@oino-ts/db-postgresql", "
|
|
234
|
+
OINOLog.exception("@oino-ts/db-postgresql", "OINODbPostgresql", "connect", "exception in connect", { message: e.message, stack: e.stack });
|
|
234
235
|
}
|
|
235
236
|
return result;
|
|
236
237
|
}
|
|
@@ -259,7 +260,7 @@ export class OINODbPostgresql extends OINODb {
|
|
|
259
260
|
}
|
|
260
261
|
catch (e) {
|
|
261
262
|
result.setError(500, "Exception validating connection: " + e.message, "OINODbPostgresql.validate");
|
|
262
|
-
OINOLog.exception("@oino-ts/db-postgresql", "
|
|
263
|
+
OINOLog.exception("@oino-ts/db-postgresql", "OINODbPostgresql", "validate", "exception in validate", { message: e.message, stack: e.stack });
|
|
263
264
|
}
|
|
264
265
|
OINOBenchmark.endMetric("OINODb", "validate");
|
|
265
266
|
return result;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { OINOResult } from "@oino-ts/common";
|
|
2
|
+
import { OINODb, OINODbParams, OINODbDataSet, OINODbApi, OINODataCell } from "@oino-ts/db";
|
|
2
3
|
/**
|
|
3
4
|
* Implementation of Postgresql-database.
|
|
4
5
|
*
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@oino-ts/db-postgresql",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.19.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.
|
|
23
|
+
"@oino-ts/db": "0.19.0",
|
|
24
24
|
"pg": "^8.11.3"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
package/src/OINODbPostgresql.ts
CHANGED
|
@@ -4,7 +4,8 @@
|
|
|
4
4
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
import {
|
|
7
|
+
import { OINO_ERROR_PREFIX, OINOBenchmark, OINOLog, OINOResult } from "@oino-ts/common";
|
|
8
|
+
import { OINODb, OINODbParams, OINODbDataSet, OINODbApi, OINOBooleanDataField, OINONumberDataField, OINOStringDataField, OINODbDataFieldParams, OINODataRow, OINODataCell, OINODatetimeDataField, OINOBlobDataField, OINODB_EMPTY_ROW, OINODB_EMPTY_ROWS } from "@oino-ts/db";
|
|
8
9
|
|
|
9
10
|
import { Pool, QueryResult } from "pg";
|
|
10
11
|
|
|
@@ -249,7 +250,7 @@ export class OINODbPostgresql extends OINODb {
|
|
|
249
250
|
|
|
250
251
|
} catch (e:any) {
|
|
251
252
|
result.setError(500, "Exception connecting to database: " + e.message, "OINODbPostgresql.connect")
|
|
252
|
-
OINOLog.exception("@oino-ts/db-postgresql", "
|
|
253
|
+
OINOLog.exception("@oino-ts/db-postgresql", "OINODbPostgresql", "connect", "exception in connect", {message:e.message, stack:e.stack})
|
|
253
254
|
}
|
|
254
255
|
return result
|
|
255
256
|
}
|
|
@@ -278,7 +279,7 @@ export class OINODbPostgresql extends OINODb {
|
|
|
278
279
|
}
|
|
279
280
|
} catch (e:any) {
|
|
280
281
|
result.setError(500, "Exception validating connection: " + e.message, "OINODbPostgresql.validate")
|
|
281
|
-
OINOLog.exception("@oino-ts/db-postgresql", "
|
|
282
|
+
OINOLog.exception("@oino-ts/db-postgresql", "OINODbPostgresql", "validate", "exception in validate", {message:e.message, stack:e.stack})
|
|
282
283
|
}
|
|
283
284
|
OINOBenchmark.endMetric("OINODb", "validate")
|
|
284
285
|
return result
|