@oino-ts/db-postgresql 0.6.1 → 0.7.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.
@@ -8,7 +8,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
8
8
  exports.OINODbPostgresql = void 0;
9
9
  const db_1 = require("@oino-ts/db");
10
10
  const pg_1 = require("pg");
11
- const EMPTY_ROW = [];
12
11
  /**
13
12
  * Implmentation of OINODbDataSet for Postgresql.
14
13
  *
@@ -73,9 +72,16 @@ class OINOPostgresqlData extends db_1.OINODbDataSet {
73
72
  return this._rows[this._currentRow];
74
73
  }
75
74
  else {
76
- return EMPTY_ROW;
75
+ return db_1.OINODB_EMPTY_ROW;
77
76
  }
78
77
  }
78
+ /**
79
+ * Gets all rows of data.
80
+ *
81
+ */
82
+ async getAllRows() {
83
+ return this._rows; // at the moment theres no result streaming, so we can just return the rows
84
+ }
79
85
  }
80
86
  /**
81
87
  * Implementation of Postgresql-database.
@@ -123,10 +129,18 @@ class OINODbPostgresql extends db_1.OINODb {
123
129
  return Promise.resolve(query_result.rows);
124
130
  }
125
131
  async _exec(sql) {
126
- // OINOLog.debug("OINODbPostgresql._query", {sql:sql})
132
+ // OINOLog.debug("OINODbPostgresql._exec", {sql:sql})
127
133
  const query_result = await this._pool.query({ rowMode: "array", text: sql });
128
- // OINOLog.debug("OINODbPostgresql._query", {result:query_result})
129
- return Promise.resolve(query_result.rows);
134
+ // OINOLog.debug("OINODbPostgresql._exec", {result:query_result})
135
+ if (Array.isArray(query_result) == true) {
136
+ return Promise.resolve(query_result.flatMap((q) => q.rows));
137
+ }
138
+ else if (query_result.rows) {
139
+ return Promise.resolve(query_result.rows);
140
+ }
141
+ else {
142
+ return Promise.resolve(db_1.OINODB_EMPTY_ROWS); // return empty row if no rows returned
143
+ }
130
144
  }
131
145
  /**
132
146
  * Print a table name using database specific SQL escaping.
@@ -286,7 +300,7 @@ class OINODbPostgresql extends db_1.OINODb {
286
300
  result = new OINOPostgresqlData(rows, []);
287
301
  }
288
302
  catch (e) {
289
- result = new OINOPostgresqlData([[]], [db_1.OINO_ERROR_PREFIX + " (sqlSelect): exception in _db.query [" + e.message + "]"]);
303
+ result = new OINOPostgresqlData(db_1.OINODB_EMPTY_ROWS, [db_1.OINO_ERROR_PREFIX + " (sqlSelect): exception in _db.query [" + e.message + "]"]);
290
304
  }
291
305
  db_1.OINOBenchmark.end("OINODb", "sqlSelect");
292
306
  return result;
@@ -306,7 +320,7 @@ class OINODbPostgresql extends db_1.OINODb {
306
320
  result = new OINOPostgresqlData(rows, []);
307
321
  }
308
322
  catch (e) {
309
- result = new OINOPostgresqlData([[]], [db_1.OINO_ERROR_PREFIX + " (sqlExec): exception in _db.exec [" + e.message + "]"]);
323
+ result = new OINOPostgresqlData(db_1.OINODB_EMPTY_ROWS, [db_1.OINO_ERROR_PREFIX + " (sqlExec): exception in _db.exec [" + e.message + "]"]);
310
324
  }
311
325
  db_1.OINOBenchmark.end("OINODb", "sqlExec");
312
326
  return result;
@@ -3,9 +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 { OINODb, OINODbDataSet, OINOBooleanDataField, OINONumberDataField, OINOStringDataField, OINO_ERROR_PREFIX, OINOBenchmark, OINODatetimeDataField, OINOBlobDataField, OINOLog, OINOResult } from "@oino-ts/db";
6
+ import { OINODb, OINODbDataSet, OINOBooleanDataField, OINONumberDataField, OINOStringDataField, OINO_ERROR_PREFIX, OINOBenchmark, OINODatetimeDataField, OINOBlobDataField, OINOLog, OINOResult, OINODB_EMPTY_ROW, OINODB_EMPTY_ROWS } from "@oino-ts/db";
7
7
  import { Pool } from "pg";
8
- const EMPTY_ROW = [];
9
8
  /**
10
9
  * Implmentation of OINODbDataSet for Postgresql.
11
10
  *
@@ -70,9 +69,16 @@ class OINOPostgresqlData extends OINODbDataSet {
70
69
  return this._rows[this._currentRow];
71
70
  }
72
71
  else {
73
- return EMPTY_ROW;
72
+ return OINODB_EMPTY_ROW;
74
73
  }
75
74
  }
75
+ /**
76
+ * Gets all rows of data.
77
+ *
78
+ */
79
+ async getAllRows() {
80
+ return this._rows; // at the moment theres no result streaming, so we can just return the rows
81
+ }
76
82
  }
77
83
  /**
78
84
  * Implementation of Postgresql-database.
@@ -120,10 +126,18 @@ export class OINODbPostgresql extends OINODb {
120
126
  return Promise.resolve(query_result.rows);
121
127
  }
122
128
  async _exec(sql) {
123
- // OINOLog.debug("OINODbPostgresql._query", {sql:sql})
129
+ // OINOLog.debug("OINODbPostgresql._exec", {sql:sql})
124
130
  const query_result = await this._pool.query({ rowMode: "array", text: sql });
125
- // OINOLog.debug("OINODbPostgresql._query", {result:query_result})
126
- return Promise.resolve(query_result.rows);
131
+ // OINOLog.debug("OINODbPostgresql._exec", {result:query_result})
132
+ if (Array.isArray(query_result) == true) {
133
+ return Promise.resolve(query_result.flatMap((q) => q.rows));
134
+ }
135
+ else if (query_result.rows) {
136
+ return Promise.resolve(query_result.rows);
137
+ }
138
+ else {
139
+ return Promise.resolve(OINODB_EMPTY_ROWS); // return empty row if no rows returned
140
+ }
127
141
  }
128
142
  /**
129
143
  * Print a table name using database specific SQL escaping.
@@ -283,7 +297,7 @@ export class OINODbPostgresql extends OINODb {
283
297
  result = new OINOPostgresqlData(rows, []);
284
298
  }
285
299
  catch (e) {
286
- result = new OINOPostgresqlData([[]], [OINO_ERROR_PREFIX + " (sqlSelect): exception in _db.query [" + e.message + "]"]);
300
+ result = new OINOPostgresqlData(OINODB_EMPTY_ROWS, [OINO_ERROR_PREFIX + " (sqlSelect): exception in _db.query [" + e.message + "]"]);
287
301
  }
288
302
  OINOBenchmark.end("OINODb", "sqlSelect");
289
303
  return result;
@@ -303,7 +317,7 @@ export class OINODbPostgresql extends OINODb {
303
317
  result = new OINOPostgresqlData(rows, []);
304
318
  }
305
319
  catch (e) {
306
- result = new OINOPostgresqlData([[]], [OINO_ERROR_PREFIX + " (sqlExec): exception in _db.exec [" + e.message + "]"]);
320
+ result = new OINOPostgresqlData(OINODB_EMPTY_ROWS, [OINO_ERROR_PREFIX + " (sqlExec): exception in _db.exec [" + e.message + "]"]);
307
321
  }
308
322
  OINOBenchmark.end("OINODb", "sqlExec");
309
323
  return result;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oino-ts/db-postgresql",
3
- "version": "0.6.1",
3
+ "version": "0.7.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.6.1",
23
+ "@oino-ts/db": "0.7.1",
24
24
  "pg": "^8.11.3"
25
25
  },
26
26
  "devDependencies": {
@@ -4,11 +4,10 @@
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, OINOResult } from "@oino-ts/db";
7
+ import { OINODb, OINODbParams, OINODbDataSet, OINODbApi, OINOBooleanDataField, OINONumberDataField, OINOStringDataField, OINODbDataFieldParams, OINO_ERROR_PREFIX, OINODataRow, OINODataCell, OINOBenchmark, OINODatetimeDataField, OINOBlobDataField, OINOLog, OINOResult, OINODB_EMPTY_ROW, OINODB_EMPTY_ROWS } from "@oino-ts/db";
8
8
 
9
9
  import { Pool, QueryResult } from "pg";
10
10
 
11
- const EMPTY_ROW:string[] = []
12
11
 
13
12
  /**
14
13
  * Implmentation of OINODbDataSet for Postgresql.
@@ -77,9 +76,17 @@ class OINOPostgresqlData extends OINODbDataSet {
77
76
  if ((this._currentRow >=0) && (this._currentRow < this._rows.length)) {
78
77
  return this._rows[this._currentRow]
79
78
  } else {
80
- return EMPTY_ROW
79
+ return OINODB_EMPTY_ROW
81
80
  }
82
81
  }
82
+
83
+ /**
84
+ * Gets all rows of data.
85
+ *
86
+ */
87
+ async getAllRows(): Promise<OINODataRow[]> {
88
+ return this._rows // at the moment theres no result streaming, so we can just return the rows
89
+ }
83
90
  }
84
91
 
85
92
  /**
@@ -135,10 +142,16 @@ export class OINODbPostgresql extends OINODb {
135
142
  }
136
143
 
137
144
  private async _exec(sql:string):Promise<OINODataRow[]> {
138
- // OINOLog.debug("OINODbPostgresql._query", {sql:sql})
145
+ // OINOLog.debug("OINODbPostgresql._exec", {sql:sql})
139
146
  const query_result:QueryResult = await this._pool.query({rowMode: "array", text: sql})
140
- // OINOLog.debug("OINODbPostgresql._query", {result:query_result})
141
- return Promise.resolve(query_result.rows)
147
+ // OINOLog.debug("OINODbPostgresql._exec", {result:query_result})
148
+ if (Array.isArray(query_result) == true) {
149
+ return Promise.resolve(query_result.flatMap((q) => q.rows))
150
+ } else if (query_result.rows) {
151
+ return Promise.resolve(query_result.rows)
152
+ } else {
153
+ return Promise.resolve(OINODB_EMPTY_ROWS) // return empty row if no rows returned
154
+ }
142
155
  }
143
156
 
144
157
  /**
@@ -304,7 +317,7 @@ export class OINODbPostgresql extends OINODb {
304
317
  result = new OINOPostgresqlData(rows, [])
305
318
 
306
319
  } catch (e:any) {
307
- result = new OINOPostgresqlData([[]], [OINO_ERROR_PREFIX + " (sqlSelect): exception in _db.query [" + e.message + "]"])
320
+ result = new OINOPostgresqlData(OINODB_EMPTY_ROWS, [OINO_ERROR_PREFIX + " (sqlSelect): exception in _db.query [" + e.message + "]"])
308
321
  }
309
322
  OINOBenchmark.end("OINODb", "sqlSelect")
310
323
  return result
@@ -325,7 +338,7 @@ export class OINODbPostgresql extends OINODb {
325
338
  result = new OINOPostgresqlData(rows, [])
326
339
 
327
340
  } catch (e:any) {
328
- result = new OINOPostgresqlData([[]], [OINO_ERROR_PREFIX + " (sqlExec): exception in _db.exec [" + e.message + "]"])
341
+ result = new OINOPostgresqlData(OINODB_EMPTY_ROWS, [OINO_ERROR_PREFIX + " (sqlExec): exception in _db.exec [" + e.message + "]"])
329
342
  }
330
343
  OINOBenchmark.end("OINODb", "sqlExec")
331
344
  return result