@oino-ts/db-postgresql 0.7.2 → 0.8.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/dist/cjs/OINODbPostgresql.js +14 -37
- package/dist/esm/OINODbPostgresql.js +14 -37
- package/package.json +2 -2
- package/src/OINODbPostgresql.ts +14 -37
|
@@ -54,7 +54,6 @@ class OINOPostgresqlData extends db_1.OINODbDataSet {
|
|
|
54
54
|
*
|
|
55
55
|
*/
|
|
56
56
|
async next() {
|
|
57
|
-
// OINOLog.debug("OINODbDataSet.next", {currentRow:this._currentRow, length:this.sqlResult.data.length})
|
|
58
57
|
if (this._currentRow < this._rows.length - 1) {
|
|
59
58
|
this._currentRow = this._currentRow + 1;
|
|
60
59
|
}
|
|
@@ -95,7 +94,6 @@ class OINODbPostgresql extends db_1.OINODb {
|
|
|
95
94
|
*/
|
|
96
95
|
constructor(params) {
|
|
97
96
|
super(params);
|
|
98
|
-
// OINOLog.debug("OINODbPostgresql.constructor", {params:params})
|
|
99
97
|
if (this._params.type !== "OINODbPostgresql") {
|
|
100
98
|
throw new Error(db_1.OINO_ERROR_PREFIX + ": Not OINODbPostgresql-type: " + this._params.type);
|
|
101
99
|
}
|
|
@@ -103,17 +101,8 @@ class OINODbPostgresql extends db_1.OINODb {
|
|
|
103
101
|
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 });
|
|
104
102
|
delete this._params.password;
|
|
105
103
|
this._pool.on("error", (err) => {
|
|
106
|
-
db_1.OINOLog.error("OINODbPostgresql error event", { err: err });
|
|
104
|
+
db_1.OINOLog.error("@oinots/db", "OINODbPostgresql", ".on(error)", "Error-event", { err: err });
|
|
107
105
|
});
|
|
108
|
-
// this._pool.on("connect", (message: any) => {
|
|
109
|
-
// OINOLog.info("OINODbPostgresql connect")
|
|
110
|
-
// })
|
|
111
|
-
// this._pool.on("release", (message: any) => {
|
|
112
|
-
// OINOLog.info("OINODbPostgresql notice")
|
|
113
|
-
// })
|
|
114
|
-
// this._pool.on("acquire", () => {
|
|
115
|
-
// OINOLog.info("OINODbPostgresql end")
|
|
116
|
-
// })
|
|
117
106
|
}
|
|
118
107
|
_parseFieldLength(fieldLength) {
|
|
119
108
|
let result = parseInt((fieldLength || "0").toString());
|
|
@@ -123,15 +112,11 @@ class OINODbPostgresql extends db_1.OINODb {
|
|
|
123
112
|
return result;
|
|
124
113
|
}
|
|
125
114
|
async _query(sql) {
|
|
126
|
-
// OINOLog.debug("OINODbPostgresql._query", {sql:sql})
|
|
127
115
|
const query_result = await this._pool.query({ rowMode: "array", text: sql });
|
|
128
|
-
// OINOLog.debug("OINODbPostgresql._query", {result:query_result})
|
|
129
116
|
return Promise.resolve(query_result.rows);
|
|
130
117
|
}
|
|
131
118
|
async _exec(sql) {
|
|
132
|
-
// OINOLog.debug("OINODbPostgresql._exec", {sql:sql})
|
|
133
119
|
const query_result = await this._pool.query({ rowMode: "array", text: sql });
|
|
134
|
-
// OINOLog.debug("OINODbPostgresql._exec", {result:query_result})
|
|
135
120
|
if (Array.isArray(query_result) == true) {
|
|
136
121
|
return Promise.resolve(query_result.flatMap((q) => q.rows));
|
|
137
122
|
}
|
|
@@ -243,13 +228,12 @@ class OINODbPostgresql extends db_1.OINODb {
|
|
|
243
228
|
let result = new db_1.OINOResult();
|
|
244
229
|
try {
|
|
245
230
|
// make sure that any items are correctly URL encoded in the connection string
|
|
246
|
-
// OINOLog.debug("OINODbPostgresql.connect")
|
|
247
231
|
await this._pool.connect();
|
|
248
232
|
this.isConnected = true;
|
|
249
233
|
}
|
|
250
|
-
catch (
|
|
251
|
-
result.setError(500, "Exception connecting to database: " +
|
|
252
|
-
db_1.OINOLog.
|
|
234
|
+
catch (e) {
|
|
235
|
+
result.setError(500, "Exception connecting to database: " + e.message, "OINODbPostgresql.connect");
|
|
236
|
+
db_1.OINOLog.exception("@oinots/db", "OINODbMsSql", "connect", "Exception", { message: e.message, stack: e.stack });
|
|
253
237
|
}
|
|
254
238
|
return result;
|
|
255
239
|
}
|
|
@@ -262,9 +246,7 @@ class OINODbPostgresql extends db_1.OINODb {
|
|
|
262
246
|
let result = new db_1.OINOResult();
|
|
263
247
|
try {
|
|
264
248
|
const sql = this._getValidateSql(this._params.database);
|
|
265
|
-
// OINOLog.debug("OINODbPostgresql.validate", {sql:sql})
|
|
266
249
|
const sql_res = await this.sqlSelect(sql);
|
|
267
|
-
// OINOLog.debug("OINODbPostgresql.validate", {sql_res:sql_res})
|
|
268
250
|
if (sql_res.isEmpty()) {
|
|
269
251
|
result.setError(400, "DB returned no rows for select!", "OINODbPostgresql.validate");
|
|
270
252
|
}
|
|
@@ -278,9 +260,9 @@ class OINODbPostgresql extends db_1.OINODb {
|
|
|
278
260
|
this.isValidated = true;
|
|
279
261
|
}
|
|
280
262
|
}
|
|
281
|
-
catch (
|
|
282
|
-
result.setError(500, "Exception validating connection: " +
|
|
283
|
-
db_1.OINOLog.
|
|
263
|
+
catch (e) {
|
|
264
|
+
result.setError(500, "Exception validating connection: " + e.message, "OINODbPostgresql.validate");
|
|
265
|
+
db_1.OINOLog.exception("@oinots/db", "OINODbMsSql", "validate", "Exception", { message: e.message, stack: e.stack });
|
|
284
266
|
}
|
|
285
267
|
db_1.OINOBenchmark.end("OINODb", "validate");
|
|
286
268
|
return result;
|
|
@@ -296,7 +278,6 @@ class OINODbPostgresql extends db_1.OINODb {
|
|
|
296
278
|
let result;
|
|
297
279
|
try {
|
|
298
280
|
const rows = await this._query(sql);
|
|
299
|
-
// OINOLog.debug("OINODbPostgresql.sqlSelect", {rows:rows})
|
|
300
281
|
result = new OINOPostgresqlData(rows, []);
|
|
301
282
|
}
|
|
302
283
|
catch (e) {
|
|
@@ -316,7 +297,6 @@ class OINODbPostgresql extends db_1.OINODb {
|
|
|
316
297
|
let result;
|
|
317
298
|
try {
|
|
318
299
|
const rows = await this._exec(sql);
|
|
319
|
-
// OINOLog.debug("OINODbPostgresql.sqlExec", {rows:rows})
|
|
320
300
|
result = new OINOPostgresqlData(rows, []);
|
|
321
301
|
}
|
|
322
302
|
catch (e) {
|
|
@@ -378,11 +358,9 @@ WHERE col.table_catalog = '${dbName}'`;
|
|
|
378
358
|
*
|
|
379
359
|
*/
|
|
380
360
|
async initializeApiDatamodel(api) {
|
|
381
|
-
const
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
const row = res.getRow();
|
|
385
|
-
// OINOLog.debug("OINODbPostgresql.initializeApiDatamodel: next row ", {row: row })
|
|
361
|
+
const schema_res = await this.sqlSelect(this._getSchemaSql(this._params.database, api.params.tableName.toLowerCase()));
|
|
362
|
+
while (!schema_res.isEof()) {
|
|
363
|
+
const row = schema_res.getRow();
|
|
386
364
|
const field_name = row[0]?.toString() || "";
|
|
387
365
|
const sql_type = row[1]?.toString() || "";
|
|
388
366
|
const field_length = this._parseFieldLength(row[2]);
|
|
@@ -397,13 +375,12 @@ WHERE col.table_catalog = '${dbName}'`;
|
|
|
397
375
|
isAutoInc: default_val.startsWith("nextval(")
|
|
398
376
|
};
|
|
399
377
|
if (api.isFieldIncluded(field_name) == false) {
|
|
400
|
-
db_1.OINOLog.info("OINODbPostgresql
|
|
378
|
+
db_1.OINOLog.info("@oinots/db", "OINODbPostgresql", "initializeApiDatamodel", "Field excluded in API parameters.", { field: field_name });
|
|
401
379
|
if (field_params.isPrimaryKey) {
|
|
402
380
|
throw new Error(db_1.OINO_ERROR_PREFIX + "Primary key field excluded in API parameters: " + field_name);
|
|
403
381
|
}
|
|
404
382
|
}
|
|
405
383
|
else {
|
|
406
|
-
// OINOLog.debug("OINODbPostgresql.initializeApiDatamodel: next field ", {field_name: field_name, sql_type:sql_type, field_length:field_length, field_params:field_params })
|
|
407
384
|
if ((sql_type == "integer") || (sql_type == "smallint") || (sql_type == "real")) {
|
|
408
385
|
api.datamodel.addField(new db_1.OINONumberDataField(this, field_name, sql_type, field_params));
|
|
409
386
|
}
|
|
@@ -428,13 +405,13 @@ WHERE col.table_catalog = '${dbName}'`;
|
|
|
428
405
|
api.datamodel.addField(new db_1.OINOStringDataField(this, field_name, sql_type, field_params, numeric_precision + numeric_scale + 1));
|
|
429
406
|
}
|
|
430
407
|
else {
|
|
431
|
-
db_1.OINOLog.info("OINODbPostgresql
|
|
408
|
+
db_1.OINOLog.info("@oinots/db", "OINODbPostgresql", "initializeApiDatamodel", "Unrecognized field type treated as string", { field_name: field_name, sql_type: sql_type, field_length: field_length, field_params: field_params });
|
|
432
409
|
api.datamodel.addField(new db_1.OINOStringDataField(this, field_name, sql_type, field_params, 0));
|
|
433
410
|
}
|
|
434
411
|
}
|
|
435
|
-
await
|
|
412
|
+
await schema_res.next();
|
|
436
413
|
}
|
|
437
|
-
db_1.OINOLog.
|
|
414
|
+
db_1.OINOLog.info("@oinots/db", "OINODbPostgresql", "initializeApiDatamodel", "\n" + api.datamodel.printDebug("\n"));
|
|
438
415
|
return Promise.resolve();
|
|
439
416
|
}
|
|
440
417
|
}
|
|
@@ -51,7 +51,6 @@ class OINOPostgresqlData extends OINODbDataSet {
|
|
|
51
51
|
*
|
|
52
52
|
*/
|
|
53
53
|
async next() {
|
|
54
|
-
// OINOLog.debug("OINODbDataSet.next", {currentRow:this._currentRow, length:this.sqlResult.data.length})
|
|
55
54
|
if (this._currentRow < this._rows.length - 1) {
|
|
56
55
|
this._currentRow = this._currentRow + 1;
|
|
57
56
|
}
|
|
@@ -92,7 +91,6 @@ export class OINODbPostgresql extends OINODb {
|
|
|
92
91
|
*/
|
|
93
92
|
constructor(params) {
|
|
94
93
|
super(params);
|
|
95
|
-
// OINOLog.debug("OINODbPostgresql.constructor", {params:params})
|
|
96
94
|
if (this._params.type !== "OINODbPostgresql") {
|
|
97
95
|
throw new Error(OINO_ERROR_PREFIX + ": Not OINODbPostgresql-type: " + this._params.type);
|
|
98
96
|
}
|
|
@@ -100,17 +98,8 @@ export class OINODbPostgresql extends OINODb {
|
|
|
100
98
|
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 });
|
|
101
99
|
delete this._params.password;
|
|
102
100
|
this._pool.on("error", (err) => {
|
|
103
|
-
OINOLog.error("OINODbPostgresql error event", { err: err });
|
|
101
|
+
OINOLog.error("@oinots/db", "OINODbPostgresql", ".on(error)", "Error-event", { err: err });
|
|
104
102
|
});
|
|
105
|
-
// this._pool.on("connect", (message: any) => {
|
|
106
|
-
// OINOLog.info("OINODbPostgresql connect")
|
|
107
|
-
// })
|
|
108
|
-
// this._pool.on("release", (message: any) => {
|
|
109
|
-
// OINOLog.info("OINODbPostgresql notice")
|
|
110
|
-
// })
|
|
111
|
-
// this._pool.on("acquire", () => {
|
|
112
|
-
// OINOLog.info("OINODbPostgresql end")
|
|
113
|
-
// })
|
|
114
103
|
}
|
|
115
104
|
_parseFieldLength(fieldLength) {
|
|
116
105
|
let result = parseInt((fieldLength || "0").toString());
|
|
@@ -120,15 +109,11 @@ export class OINODbPostgresql extends OINODb {
|
|
|
120
109
|
return result;
|
|
121
110
|
}
|
|
122
111
|
async _query(sql) {
|
|
123
|
-
// OINOLog.debug("OINODbPostgresql._query", {sql:sql})
|
|
124
112
|
const query_result = await this._pool.query({ rowMode: "array", text: sql });
|
|
125
|
-
// OINOLog.debug("OINODbPostgresql._query", {result:query_result})
|
|
126
113
|
return Promise.resolve(query_result.rows);
|
|
127
114
|
}
|
|
128
115
|
async _exec(sql) {
|
|
129
|
-
// OINOLog.debug("OINODbPostgresql._exec", {sql:sql})
|
|
130
116
|
const query_result = await this._pool.query({ rowMode: "array", text: sql });
|
|
131
|
-
// OINOLog.debug("OINODbPostgresql._exec", {result:query_result})
|
|
132
117
|
if (Array.isArray(query_result) == true) {
|
|
133
118
|
return Promise.resolve(query_result.flatMap((q) => q.rows));
|
|
134
119
|
}
|
|
@@ -240,13 +225,12 @@ export class OINODbPostgresql extends OINODb {
|
|
|
240
225
|
let result = new OINOResult();
|
|
241
226
|
try {
|
|
242
227
|
// make sure that any items are correctly URL encoded in the connection string
|
|
243
|
-
// OINOLog.debug("OINODbPostgresql.connect")
|
|
244
228
|
await this._pool.connect();
|
|
245
229
|
this.isConnected = true;
|
|
246
230
|
}
|
|
247
|
-
catch (
|
|
248
|
-
result.setError(500, "Exception connecting to database: " +
|
|
249
|
-
OINOLog.
|
|
231
|
+
catch (e) {
|
|
232
|
+
result.setError(500, "Exception connecting to database: " + e.message, "OINODbPostgresql.connect");
|
|
233
|
+
OINOLog.exception("@oinots/db", "OINODbMsSql", "connect", "Exception", { message: e.message, stack: e.stack });
|
|
250
234
|
}
|
|
251
235
|
return result;
|
|
252
236
|
}
|
|
@@ -259,9 +243,7 @@ export class OINODbPostgresql extends OINODb {
|
|
|
259
243
|
let result = new OINOResult();
|
|
260
244
|
try {
|
|
261
245
|
const sql = this._getValidateSql(this._params.database);
|
|
262
|
-
// OINOLog.debug("OINODbPostgresql.validate", {sql:sql})
|
|
263
246
|
const sql_res = await this.sqlSelect(sql);
|
|
264
|
-
// OINOLog.debug("OINODbPostgresql.validate", {sql_res:sql_res})
|
|
265
247
|
if (sql_res.isEmpty()) {
|
|
266
248
|
result.setError(400, "DB returned no rows for select!", "OINODbPostgresql.validate");
|
|
267
249
|
}
|
|
@@ -275,9 +257,9 @@ export class OINODbPostgresql extends OINODb {
|
|
|
275
257
|
this.isValidated = true;
|
|
276
258
|
}
|
|
277
259
|
}
|
|
278
|
-
catch (
|
|
279
|
-
result.setError(500, "Exception validating connection: " +
|
|
280
|
-
OINOLog.
|
|
260
|
+
catch (e) {
|
|
261
|
+
result.setError(500, "Exception validating connection: " + e.message, "OINODbPostgresql.validate");
|
|
262
|
+
OINOLog.exception("@oinots/db", "OINODbMsSql", "validate", "Exception", { message: e.message, stack: e.stack });
|
|
281
263
|
}
|
|
282
264
|
OINOBenchmark.end("OINODb", "validate");
|
|
283
265
|
return result;
|
|
@@ -293,7 +275,6 @@ export class OINODbPostgresql extends OINODb {
|
|
|
293
275
|
let result;
|
|
294
276
|
try {
|
|
295
277
|
const rows = await this._query(sql);
|
|
296
|
-
// OINOLog.debug("OINODbPostgresql.sqlSelect", {rows:rows})
|
|
297
278
|
result = new OINOPostgresqlData(rows, []);
|
|
298
279
|
}
|
|
299
280
|
catch (e) {
|
|
@@ -313,7 +294,6 @@ export class OINODbPostgresql extends OINODb {
|
|
|
313
294
|
let result;
|
|
314
295
|
try {
|
|
315
296
|
const rows = await this._exec(sql);
|
|
316
|
-
// OINOLog.debug("OINODbPostgresql.sqlExec", {rows:rows})
|
|
317
297
|
result = new OINOPostgresqlData(rows, []);
|
|
318
298
|
}
|
|
319
299
|
catch (e) {
|
|
@@ -375,11 +355,9 @@ WHERE col.table_catalog = '${dbName}'`;
|
|
|
375
355
|
*
|
|
376
356
|
*/
|
|
377
357
|
async initializeApiDatamodel(api) {
|
|
378
|
-
const
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
const row = res.getRow();
|
|
382
|
-
// OINOLog.debug("OINODbPostgresql.initializeApiDatamodel: next row ", {row: row })
|
|
358
|
+
const schema_res = await this.sqlSelect(this._getSchemaSql(this._params.database, api.params.tableName.toLowerCase()));
|
|
359
|
+
while (!schema_res.isEof()) {
|
|
360
|
+
const row = schema_res.getRow();
|
|
383
361
|
const field_name = row[0]?.toString() || "";
|
|
384
362
|
const sql_type = row[1]?.toString() || "";
|
|
385
363
|
const field_length = this._parseFieldLength(row[2]);
|
|
@@ -394,13 +372,12 @@ WHERE col.table_catalog = '${dbName}'`;
|
|
|
394
372
|
isAutoInc: default_val.startsWith("nextval(")
|
|
395
373
|
};
|
|
396
374
|
if (api.isFieldIncluded(field_name) == false) {
|
|
397
|
-
OINOLog.info("OINODbPostgresql
|
|
375
|
+
OINOLog.info("@oinots/db", "OINODbPostgresql", "initializeApiDatamodel", "Field excluded in API parameters.", { field: field_name });
|
|
398
376
|
if (field_params.isPrimaryKey) {
|
|
399
377
|
throw new Error(OINO_ERROR_PREFIX + "Primary key field excluded in API parameters: " + field_name);
|
|
400
378
|
}
|
|
401
379
|
}
|
|
402
380
|
else {
|
|
403
|
-
// OINOLog.debug("OINODbPostgresql.initializeApiDatamodel: next field ", {field_name: field_name, sql_type:sql_type, field_length:field_length, field_params:field_params })
|
|
404
381
|
if ((sql_type == "integer") || (sql_type == "smallint") || (sql_type == "real")) {
|
|
405
382
|
api.datamodel.addField(new OINONumberDataField(this, field_name, sql_type, field_params));
|
|
406
383
|
}
|
|
@@ -425,13 +402,13 @@ WHERE col.table_catalog = '${dbName}'`;
|
|
|
425
402
|
api.datamodel.addField(new OINOStringDataField(this, field_name, sql_type, field_params, numeric_precision + numeric_scale + 1));
|
|
426
403
|
}
|
|
427
404
|
else {
|
|
428
|
-
OINOLog.info("OINODbPostgresql
|
|
405
|
+
OINOLog.info("@oinots/db", "OINODbPostgresql", "initializeApiDatamodel", "Unrecognized field type treated as string", { field_name: field_name, sql_type: sql_type, field_length: field_length, field_params: field_params });
|
|
429
406
|
api.datamodel.addField(new OINOStringDataField(this, field_name, sql_type, field_params, 0));
|
|
430
407
|
}
|
|
431
408
|
}
|
|
432
|
-
await
|
|
409
|
+
await schema_res.next();
|
|
433
410
|
}
|
|
434
|
-
OINOLog.
|
|
411
|
+
OINOLog.info("@oinots/db", "OINODbPostgresql", "initializeApiDatamodel", "\n" + api.datamodel.printDebug("\n"));
|
|
435
412
|
return Promise.resolve();
|
|
436
413
|
}
|
|
437
414
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@oino-ts/db-postgresql",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.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.8.0",
|
|
24
24
|
"pg": "^8.11.3"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
package/src/OINODbPostgresql.ts
CHANGED
|
@@ -59,7 +59,6 @@ class OINOPostgresqlData extends OINODbDataSet {
|
|
|
59
59
|
*
|
|
60
60
|
*/
|
|
61
61
|
async next():Promise<boolean> {
|
|
62
|
-
// OINOLog.debug("OINODbDataSet.next", {currentRow:this._currentRow, length:this.sqlResult.data.length})
|
|
63
62
|
if (this._currentRow < this._rows.length-1) {
|
|
64
63
|
this._currentRow = this._currentRow + 1
|
|
65
64
|
} else {
|
|
@@ -104,7 +103,6 @@ export class OINODbPostgresql extends OINODb {
|
|
|
104
103
|
constructor(params:OINODbParams) {
|
|
105
104
|
super(params)
|
|
106
105
|
|
|
107
|
-
// OINOLog.debug("OINODbPostgresql.constructor", {params:params})
|
|
108
106
|
if (this._params.type !== "OINODbPostgresql") {
|
|
109
107
|
throw new Error(OINO_ERROR_PREFIX + ": Not OINODbPostgresql-type: " + this._params.type)
|
|
110
108
|
}
|
|
@@ -113,17 +111,8 @@ export class OINODbPostgresql extends OINODb {
|
|
|
113
111
|
delete this._params.password
|
|
114
112
|
|
|
115
113
|
this._pool.on("error", (err: any) => {
|
|
116
|
-
OINOLog.error("OINODbPostgresql error event", {err:err})
|
|
114
|
+
OINOLog.error("@oinots/db-postgresql", "OINODbPostgresql", ".on(error)", "Error-event", {err:err})
|
|
117
115
|
})
|
|
118
|
-
// this._pool.on("connect", (message: any) => {
|
|
119
|
-
// OINOLog.info("OINODbPostgresql connect")
|
|
120
|
-
// })
|
|
121
|
-
// this._pool.on("release", (message: any) => {
|
|
122
|
-
// OINOLog.info("OINODbPostgresql notice")
|
|
123
|
-
// })
|
|
124
|
-
// this._pool.on("acquire", () => {
|
|
125
|
-
// OINOLog.info("OINODbPostgresql end")
|
|
126
|
-
// })
|
|
127
116
|
}
|
|
128
117
|
|
|
129
118
|
private _parseFieldLength(fieldLength:OINODataCell):number {
|
|
@@ -135,16 +124,12 @@ export class OINODbPostgresql extends OINODb {
|
|
|
135
124
|
}
|
|
136
125
|
|
|
137
126
|
private async _query(sql:string):Promise<OINODataRow[]> {
|
|
138
|
-
// OINOLog.debug("OINODbPostgresql._query", {sql:sql})
|
|
139
127
|
const query_result:QueryResult = await this._pool.query({rowMode: "array", text: sql})
|
|
140
|
-
// OINOLog.debug("OINODbPostgresql._query", {result:query_result})
|
|
141
128
|
return Promise.resolve(query_result.rows)
|
|
142
129
|
}
|
|
143
130
|
|
|
144
131
|
private async _exec(sql:string):Promise<OINODataRow[]> {
|
|
145
|
-
// OINOLog.debug("OINODbPostgresql._exec", {sql:sql})
|
|
146
132
|
const query_result:QueryResult = await this._pool.query({rowMode: "array", text: sql})
|
|
147
|
-
// OINOLog.debug("OINODbPostgresql._exec", {result:query_result})
|
|
148
133
|
if (Array.isArray(query_result) == true) {
|
|
149
134
|
return Promise.resolve(query_result.flatMap((q) => q.rows))
|
|
150
135
|
} else if (query_result.rows) {
|
|
@@ -259,13 +244,12 @@ export class OINODbPostgresql extends OINODb {
|
|
|
259
244
|
let result:OINOResult = new OINOResult()
|
|
260
245
|
try {
|
|
261
246
|
// make sure that any items are correctly URL encoded in the connection string
|
|
262
|
-
// OINOLog.debug("OINODbPostgresql.connect")
|
|
263
247
|
await this._pool.connect()
|
|
264
248
|
this.isConnected = true
|
|
265
249
|
|
|
266
|
-
} catch (
|
|
267
|
-
result.setError(500, "Exception connecting to database: " +
|
|
268
|
-
OINOLog.
|
|
250
|
+
} catch (e:any) {
|
|
251
|
+
result.setError(500, "Exception connecting to database: " + e.message, "OINODbPostgresql.connect")
|
|
252
|
+
OINOLog.exception("@oinots/db-postgresql", "OINODbMsSql", "connect", "Exception", {message:e.message, stack:e.stack})
|
|
269
253
|
}
|
|
270
254
|
return result
|
|
271
255
|
}
|
|
@@ -279,9 +263,7 @@ export class OINODbPostgresql extends OINODb {
|
|
|
279
263
|
let result:OINOResult = new OINOResult()
|
|
280
264
|
try {
|
|
281
265
|
const sql = this._getValidateSql(this._params.database)
|
|
282
|
-
// OINOLog.debug("OINODbPostgresql.validate", {sql:sql})
|
|
283
266
|
const sql_res:OINODbDataSet = await this.sqlSelect(sql)
|
|
284
|
-
// OINOLog.debug("OINODbPostgresql.validate", {sql_res:sql_res})
|
|
285
267
|
if (sql_res.isEmpty()) {
|
|
286
268
|
result.setError(400, "DB returned no rows for select!", "OINODbPostgresql.validate")
|
|
287
269
|
|
|
@@ -294,9 +276,9 @@ export class OINODbPostgresql extends OINODb {
|
|
|
294
276
|
} else {
|
|
295
277
|
this.isValidated = true
|
|
296
278
|
}
|
|
297
|
-
} catch (
|
|
298
|
-
result.setError(500, "Exception validating connection: " +
|
|
299
|
-
OINOLog.
|
|
279
|
+
} catch (e:any) {
|
|
280
|
+
result.setError(500, "Exception validating connection: " + e.message, "OINODbPostgresql.validate")
|
|
281
|
+
OINOLog.exception("@oinots/db-postgresql", "OINODbMsSql", "validate", "Exception", {message:e.message, stack:e.stack})
|
|
300
282
|
}
|
|
301
283
|
OINOBenchmark.end("OINODb", "validate")
|
|
302
284
|
return result
|
|
@@ -313,7 +295,6 @@ export class OINODbPostgresql extends OINODb {
|
|
|
313
295
|
let result:OINODbDataSet
|
|
314
296
|
try {
|
|
315
297
|
const rows:OINODataRow[] = await this._query(sql)
|
|
316
|
-
// OINOLog.debug("OINODbPostgresql.sqlSelect", {rows:rows})
|
|
317
298
|
result = new OINOPostgresqlData(rows, [])
|
|
318
299
|
|
|
319
300
|
} catch (e:any) {
|
|
@@ -334,7 +315,6 @@ export class OINODbPostgresql extends OINODb {
|
|
|
334
315
|
let result:OINODbDataSet
|
|
335
316
|
try {
|
|
336
317
|
const rows:OINODataRow[] = await this._exec(sql)
|
|
337
|
-
// OINOLog.debug("OINODbPostgresql.sqlExec", {rows:rows})
|
|
338
318
|
result = new OINOPostgresqlData(rows, [])
|
|
339
319
|
|
|
340
320
|
} catch (e:any) {
|
|
@@ -402,11 +382,9 @@ WHERE col.table_catalog = '${dbName}'`
|
|
|
402
382
|
*/
|
|
403
383
|
async initializeApiDatamodel(api:OINODbApi): Promise<void> {
|
|
404
384
|
|
|
405
|
-
const
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
const row:OINODataRow = res.getRow()
|
|
409
|
-
// OINOLog.debug("OINODbPostgresql.initializeApiDatamodel: next row ", {row: row })
|
|
385
|
+
const schema_res:OINODbDataSet = await this.sqlSelect(this._getSchemaSql(this._params.database, api.params.tableName.toLowerCase()))
|
|
386
|
+
while (!schema_res.isEof()) {
|
|
387
|
+
const row:OINODataRow = schema_res.getRow()
|
|
410
388
|
const field_name:string = row[0]?.toString() || ""
|
|
411
389
|
const sql_type:string = row[1]?.toString() || ""
|
|
412
390
|
const field_length:number = this._parseFieldLength(row[2])
|
|
@@ -421,13 +399,12 @@ WHERE col.table_catalog = '${dbName}'`
|
|
|
421
399
|
isAutoInc: default_val.startsWith("nextval(")
|
|
422
400
|
}
|
|
423
401
|
if (api.isFieldIncluded(field_name) == false) {
|
|
424
|
-
OINOLog.info("OINODbPostgresql
|
|
402
|
+
OINOLog.info("@oinots/db-postgresql", "OINODbPostgresql", "initializeApiDatamodel", "Field excluded in API parameters.", {field:field_name})
|
|
425
403
|
if (field_params.isPrimaryKey) {
|
|
426
404
|
throw new Error(OINO_ERROR_PREFIX + "Primary key field excluded in API parameters: " + field_name)
|
|
427
405
|
}
|
|
428
406
|
|
|
429
407
|
} else {
|
|
430
|
-
// OINOLog.debug("OINODbPostgresql.initializeApiDatamodel: next field ", {field_name: field_name, sql_type:sql_type, field_length:field_length, field_params:field_params })
|
|
431
408
|
if ((sql_type == "integer") || (sql_type == "smallint") || (sql_type == "real")) {
|
|
432
409
|
api.datamodel.addField(new OINONumberDataField(this, field_name, sql_type, field_params ))
|
|
433
410
|
|
|
@@ -451,13 +428,13 @@ WHERE col.table_catalog = '${dbName}'`
|
|
|
451
428
|
api.datamodel.addField(new OINOStringDataField(this, field_name, sql_type, field_params, numeric_precision + numeric_scale + 1))
|
|
452
429
|
|
|
453
430
|
} else {
|
|
454
|
-
OINOLog.info("OINODbPostgresql
|
|
431
|
+
OINOLog.info("@oinots/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 })
|
|
455
432
|
api.datamodel.addField(new OINOStringDataField(this, field_name, sql_type, field_params, 0))
|
|
456
433
|
}
|
|
457
434
|
}
|
|
458
|
-
await
|
|
435
|
+
await schema_res.next()
|
|
459
436
|
}
|
|
460
|
-
OINOLog.
|
|
437
|
+
OINOLog.info("@oinots/db-postgresql", "OINODbPostgresql", "initializeApiDatamodel", "\n" + api.datamodel.printDebug("\n"))
|
|
461
438
|
return Promise.resolve()
|
|
462
439
|
}
|
|
463
440
|
}
|