@oino-ts/db-postgresql 0.6.0 → 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,18 @@ 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;
234
235
  }
235
236
  catch (err) {
236
- // ... error checks
237
- throw new Error(db_1.OINO_ERROR_PREFIX + ": Error connecting to Postgresql server: " + err);
237
+ result.setError(500, "Exception connecting to database: " + err.message, "OINODbPostgresql.connect");
238
+ db_1.OINOLog.error(result.statusMessage, { error: err });
238
239
  }
240
+ return result;
239
241
  }
240
242
  /**
241
243
  * Validate connection to database is working.
@@ -246,24 +248,25 @@ class OINODbPostgresql extends db_1.OINODb {
246
248
  let result = new db_1.OINOResult();
247
249
  try {
248
250
  const sql = this._getValidateSql(this._params.database);
249
- // OINOLog.debug("OINODbBunSqlite.validate", {sql:sql})
251
+ // OINOLog.debug("OINODbPostgresql.validate", {sql:sql})
250
252
  const sql_res = await this.sqlSelect(sql);
251
- // OINOLog.debug("OINODbBunSqlite.validate", {sql_res:sql_res})
253
+ // OINOLog.debug("OINODbPostgresql.validate", {sql_res:sql_res})
252
254
  if (sql_res.isEmpty()) {
253
- result.setError(400, "DB returned no rows for select!", "OINODbBunSqlite.validate");
255
+ result.setError(400, "DB returned no rows for select!", "OINODbPostgresql.validate");
254
256
  }
255
257
  else if (sql_res.getRow().length == 0) {
256
- result.setError(400, "DB returned no values for database!", "OINODbBunSqlite.validate");
258
+ result.setError(400, "DB returned no values for database!", "OINODbPostgresql.validate");
257
259
  }
258
260
  else if (sql_res.getRow()[0] == "0") {
259
- result.setError(400, "DB returned no schema for database!", "OINODbBunSqlite.validate");
261
+ result.setError(400, "DB returned no schema for database!", "OINODbPostgresql.validate");
260
262
  }
261
263
  else {
262
- // connection is working
264
+ this.isValidated = true;
263
265
  }
264
266
  }
265
- catch (e) {
266
- result.setError(500, db_1.OINO_ERROR_PREFIX + " (validate): OINODbBunSqlite.validate exception in _db.query: " + e.message, "OINODbBunSqlite.validate");
267
+ catch (err) {
268
+ result.setError(500, "Exception validating connection: " + err.message, "OINODbPostgresql.validate");
269
+ db_1.OINOLog.error(result.statusMessage, { error: err });
267
270
  }
268
271
  db_1.OINOBenchmark.end("OINODb", "validate");
269
272
  return result;
@@ -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,18 @@ 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;
231
232
  }
232
233
  catch (err) {
233
- // ... error checks
234
- throw new Error(OINO_ERROR_PREFIX + ": Error connecting to Postgresql server: " + err);
234
+ result.setError(500, "Exception connecting to database: " + err.message, "OINODbPostgresql.connect");
235
+ OINOLog.error(result.statusMessage, { error: err });
235
236
  }
237
+ return result;
236
238
  }
237
239
  /**
238
240
  * Validate connection to database is working.
@@ -243,24 +245,25 @@ export class OINODbPostgresql extends OINODb {
243
245
  let result = new OINOResult();
244
246
  try {
245
247
  const sql = this._getValidateSql(this._params.database);
246
- // OINOLog.debug("OINODbBunSqlite.validate", {sql:sql})
248
+ // OINOLog.debug("OINODbPostgresql.validate", {sql:sql})
247
249
  const sql_res = await this.sqlSelect(sql);
248
- // OINOLog.debug("OINODbBunSqlite.validate", {sql_res:sql_res})
250
+ // OINOLog.debug("OINODbPostgresql.validate", {sql_res:sql_res})
249
251
  if (sql_res.isEmpty()) {
250
- result.setError(400, "DB returned no rows for select!", "OINODbBunSqlite.validate");
252
+ result.setError(400, "DB returned no rows for select!", "OINODbPostgresql.validate");
251
253
  }
252
254
  else if (sql_res.getRow().length == 0) {
253
- result.setError(400, "DB returned no values for database!", "OINODbBunSqlite.validate");
255
+ result.setError(400, "DB returned no values for database!", "OINODbPostgresql.validate");
254
256
  }
255
257
  else if (sql_res.getRow()[0] == "0") {
256
- result.setError(400, "DB returned no schema for database!", "OINODbBunSqlite.validate");
258
+ result.setError(400, "DB returned no schema for database!", "OINODbPostgresql.validate");
257
259
  }
258
260
  else {
259
- // connection is working
261
+ this.isValidated = true;
260
262
  }
261
263
  }
262
- catch (e) {
263
- result.setError(500, OINO_ERROR_PREFIX + " (validate): OINODbBunSqlite.validate exception in _db.query: " + e.message, "OINODbBunSqlite.validate");
264
+ catch (err) {
265
+ result.setError(500, "Exception validating connection: " + err.message, "OINODbPostgresql.validate");
266
+ OINOLog.error(result.statusMessage, { error: err });
264
267
  }
265
268
  OINOBenchmark.end("OINODb", "validate");
266
269
  return result;
@@ -56,7 +56,7 @@ export declare class OINODbPostgresql extends OINODb {
56
56
  * Connect to database.
57
57
  *
58
58
  */
59
- connect(): Promise<boolean>;
59
+ connect(): Promise<OINOResult>;
60
60
  /**
61
61
  * Validate connection to database is working.
62
62
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oino-ts/db-postgresql",
3
- "version": "0.6.0",
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.6.0",
23
+ "@oino-ts/db": "0.6.1",
24
24
  "pg": "^8.11.3"
25
25
  },
26
26
  "devDependencies": {
@@ -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,19 @@ 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
254
258
  }
255
259
 
256
260
  /**
@@ -262,23 +266,24 @@ export class OINODbPostgresql extends OINODb {
262
266
  let result:OINOResult = new OINOResult()
263
267
  try {
264
268
  const sql = this._getValidateSql(this._params.database)
265
- // OINOLog.debug("OINODbBunSqlite.validate", {sql:sql})
269
+ // OINOLog.debug("OINODbPostgresql.validate", {sql:sql})
266
270
  const sql_res:OINODbDataSet = await this.sqlSelect(sql)
267
- // OINOLog.debug("OINODbBunSqlite.validate", {sql_res:sql_res})
271
+ // OINOLog.debug("OINODbPostgresql.validate", {sql_res:sql_res})
268
272
  if (sql_res.isEmpty()) {
269
- result.setError(400, "DB returned no rows for select!", "OINODbBunSqlite.validate")
273
+ result.setError(400, "DB returned no rows for select!", "OINODbPostgresql.validate")
270
274
 
271
275
  } else if (sql_res.getRow().length == 0) {
272
- result.setError(400, "DB returned no values for database!", "OINODbBunSqlite.validate")
276
+ result.setError(400, "DB returned no values for database!", "OINODbPostgresql.validate")
273
277
 
274
278
  } else if (sql_res.getRow()[0] == "0") {
275
- result.setError(400, "DB returned no schema for database!", "OINODbBunSqlite.validate")
279
+ result.setError(400, "DB returned no schema for database!", "OINODbPostgresql.validate")
276
280
 
277
281
  } else {
278
- // connection is working
282
+ this.isValidated = true
279
283
  }
280
- } catch (e:any) {
281
- result.setError(500, OINO_ERROR_PREFIX + " (validate): OINODbBunSqlite.validate exception in _db.query: " + e.message, "OINODbBunSqlite.validate")
284
+ } catch (err:any) {
285
+ result.setError(500, "Exception validating connection: " + err.message, "OINODbPostgresql.validate")
286
+ OINOLog.error(result.statusMessage, {error:err})
282
287
  }
283
288
  OINOBenchmark.end("OINODb", "validate")
284
289
  return result