@oino-ts/db-mariadb 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.
@@ -85,7 +85,8 @@ class OINOMariadbData extends db_1.OINODbDataSet {
85
85
  */
86
86
  class OINODbMariadb extends db_1.OINODb {
87
87
  static _fieldLengthRegex = /([^\(\)]+)(\s?\((\d+)\s?\,?\s?(\d*)?\))?/i;
88
- static _exceptionMessageRegex = /\(([^\)]*)\) (.*)\nsql\:(.*)?/i;
88
+ static _connectionExceptionMessageRegex = /\(([^\)]*)\) (.*)/i;
89
+ static _sqlExceptionMessageRegex = /\(([^\)]*)\) (.*)\nsql\:(.*)?/i;
89
90
  _pool;
90
91
  /**
91
92
  * Constructor of `OINODbMariadb`
@@ -97,7 +98,8 @@ class OINODbMariadb extends db_1.OINODb {
97
98
  if (this._params.type !== "OINODbMariadb") {
98
99
  throw new Error(db_1.OINO_ERROR_PREFIX + ": Not OINODbMariadb-type: " + this._params.type);
99
100
  }
100
- this._pool = mariadb_1.default.createPool({ host: params.url, database: params.database, port: params.port, user: params.user, password: params.password, acquireTimeout: 2000, debug: false, rowsAsArray: true });
101
+ this._pool = mariadb_1.default.createPool({ host: this._params.url, database: this._params.database, port: this._params.port, user: this._params.user, password: this._params.password, acquireTimeout: 2000, debug: false, rowsAsArray: true });
102
+ delete this._params.password; // do not store password in db object
101
103
  // this._pool.on("acquire", (conn: mariadb.Connection) => {
102
104
  // OINOLog.info("OINODbMariadb acquire", {conn:conn})
103
105
  // })
@@ -148,7 +150,7 @@ class OINODbMariadb extends db_1.OINODb {
148
150
  return Promise.resolve(result);
149
151
  }
150
152
  catch (err) {
151
- const msg_parts = err.message.match(OINODbMariadb._exceptionMessageRegex) || [];
153
+ const msg_parts = err.message.match(OINODbMariadb._sqlExceptionMessageRegex) || [];
152
154
  // OINOLog.debug("OINODbMariadb._exec exception", {connection: msg_parts[1], message:msg_parts[2], sql:msg_parts[3]}) // print connection info just to log so tests don't break on runtime output
153
155
  throw new Error(msg_parts[2]);
154
156
  }
@@ -270,17 +272,25 @@ class OINODbMariadb extends db_1.OINODb {
270
272
  *
271
273
  */
272
274
  async connect() {
275
+ const result = new db_1.OINOResult();
276
+ let connection = null;
273
277
  try {
274
278
  // make sure that any items are correctly URL encoded in the connection string
275
279
  // OINOLog.debug("OINODbMariadb.connect")
276
- await this._pool.on;
277
- // await this._client.connect()
278
- return Promise.resolve(true);
280
+ connection = await this._pool.getConnection();
281
+ this.isConnected = true;
279
282
  }
280
283
  catch (err) {
281
- // ... error checks
282
- throw new Error(db_1.OINO_ERROR_PREFIX + ": Error connecting to OINODbMariadb server: " + err);
284
+ const msg_parts = err.message.match(OINODbMariadb._connectionExceptionMessageRegex) || [];
285
+ result.setError(500, "Error connecting to server: " + msg_parts[2], "OINODbMariadb.connect");
286
+ db_1.OINOLog.error(result.statusMessage, { error: err });
283
287
  }
288
+ finally {
289
+ if (connection) {
290
+ await connection.end();
291
+ }
292
+ }
293
+ return Promise.resolve(result);
284
294
  }
285
295
  /**
286
296
  * Validate connection to database is working.
@@ -293,7 +303,7 @@ class OINODbMariadb extends db_1.OINODb {
293
303
  const sql = this._getValidateSql(this._params.database);
294
304
  // OINOLog.debug("OINODbMariadb.validate", {sql:sql})
295
305
  const sql_res = await this.sqlSelect(sql);
296
- db_1.OINOLog.debug("OINODbMariadb.validate", { sql_res: sql_res });
306
+ // OINOLog.debug("OINODbMariadb.validate", {sql_res:sql_res})
297
307
  if (sql_res.isEmpty()) {
298
308
  result.setError(400, "DB returned no rows for select!", "OINODbMariadb.validate");
299
309
  }
@@ -304,11 +314,12 @@ class OINODbMariadb extends db_1.OINODb {
304
314
  result.setError(400, "DB returned no schema for database!", "OINODbMariadb.validate");
305
315
  }
306
316
  else {
307
- // connection is working
317
+ this.isValidated = true;
308
318
  }
309
319
  }
310
- catch (e) {
311
- result.setError(500, db_1.OINO_ERROR_PREFIX + " (validate): OINODbMariadb.validate exception in _db.query: " + e.message, "OINODbMariadb.validate");
320
+ catch (err) {
321
+ result.setError(500, "Exception validating connection: " + err.message, "OINODbMariadb.validate");
322
+ db_1.OINOLog.error(result.statusMessage, { error: err });
312
323
  }
313
324
  db_1.OINOBenchmark.end("OINODb", "validate");
314
325
  return result;
@@ -82,7 +82,8 @@ class OINOMariadbData extends OINODbDataSet {
82
82
  */
83
83
  export class OINODbMariadb extends OINODb {
84
84
  static _fieldLengthRegex = /([^\(\)]+)(\s?\((\d+)\s?\,?\s?(\d*)?\))?/i;
85
- static _exceptionMessageRegex = /\(([^\)]*)\) (.*)\nsql\:(.*)?/i;
85
+ static _connectionExceptionMessageRegex = /\(([^\)]*)\) (.*)/i;
86
+ static _sqlExceptionMessageRegex = /\(([^\)]*)\) (.*)\nsql\:(.*)?/i;
86
87
  _pool;
87
88
  /**
88
89
  * Constructor of `OINODbMariadb`
@@ -94,7 +95,8 @@ export class OINODbMariadb extends OINODb {
94
95
  if (this._params.type !== "OINODbMariadb") {
95
96
  throw new Error(OINO_ERROR_PREFIX + ": Not OINODbMariadb-type: " + this._params.type);
96
97
  }
97
- this._pool = mariadb.createPool({ host: params.url, database: params.database, port: params.port, user: params.user, password: params.password, acquireTimeout: 2000, debug: false, rowsAsArray: true });
98
+ this._pool = mariadb.createPool({ host: this._params.url, database: this._params.database, port: this._params.port, user: this._params.user, password: this._params.password, acquireTimeout: 2000, debug: false, rowsAsArray: true });
99
+ delete this._params.password; // do not store password in db object
98
100
  // this._pool.on("acquire", (conn: mariadb.Connection) => {
99
101
  // OINOLog.info("OINODbMariadb acquire", {conn:conn})
100
102
  // })
@@ -145,7 +147,7 @@ export class OINODbMariadb extends OINODb {
145
147
  return Promise.resolve(result);
146
148
  }
147
149
  catch (err) {
148
- const msg_parts = err.message.match(OINODbMariadb._exceptionMessageRegex) || [];
150
+ const msg_parts = err.message.match(OINODbMariadb._sqlExceptionMessageRegex) || [];
149
151
  // OINOLog.debug("OINODbMariadb._exec exception", {connection: msg_parts[1], message:msg_parts[2], sql:msg_parts[3]}) // print connection info just to log so tests don't break on runtime output
150
152
  throw new Error(msg_parts[2]);
151
153
  }
@@ -267,17 +269,25 @@ export class OINODbMariadb extends OINODb {
267
269
  *
268
270
  */
269
271
  async connect() {
272
+ const result = new OINOResult();
273
+ let connection = null;
270
274
  try {
271
275
  // make sure that any items are correctly URL encoded in the connection string
272
276
  // OINOLog.debug("OINODbMariadb.connect")
273
- await this._pool.on;
274
- // await this._client.connect()
275
- return Promise.resolve(true);
277
+ connection = await this._pool.getConnection();
278
+ this.isConnected = true;
276
279
  }
277
280
  catch (err) {
278
- // ... error checks
279
- throw new Error(OINO_ERROR_PREFIX + ": Error connecting to OINODbMariadb server: " + err);
281
+ const msg_parts = err.message.match(OINODbMariadb._connectionExceptionMessageRegex) || [];
282
+ result.setError(500, "Error connecting to server: " + msg_parts[2], "OINODbMariadb.connect");
283
+ OINOLog.error(result.statusMessage, { error: err });
280
284
  }
285
+ finally {
286
+ if (connection) {
287
+ await connection.end();
288
+ }
289
+ }
290
+ return Promise.resolve(result);
281
291
  }
282
292
  /**
283
293
  * Validate connection to database is working.
@@ -290,7 +300,7 @@ export class OINODbMariadb extends OINODb {
290
300
  const sql = this._getValidateSql(this._params.database);
291
301
  // OINOLog.debug("OINODbMariadb.validate", {sql:sql})
292
302
  const sql_res = await this.sqlSelect(sql);
293
- OINOLog.debug("OINODbMariadb.validate", { sql_res: sql_res });
303
+ // OINOLog.debug("OINODbMariadb.validate", {sql_res:sql_res})
294
304
  if (sql_res.isEmpty()) {
295
305
  result.setError(400, "DB returned no rows for select!", "OINODbMariadb.validate");
296
306
  }
@@ -301,11 +311,12 @@ export class OINODbMariadb extends OINODb {
301
311
  result.setError(400, "DB returned no schema for database!", "OINODbMariadb.validate");
302
312
  }
303
313
  else {
304
- // connection is working
314
+ this.isValidated = true;
305
315
  }
306
316
  }
307
- catch (e) {
308
- result.setError(500, OINO_ERROR_PREFIX + " (validate): OINODbMariadb.validate exception in _db.query: " + e.message, "OINODbMariadb.validate");
317
+ catch (err) {
318
+ result.setError(500, "Exception validating connection: " + err.message, "OINODbMariadb.validate");
319
+ OINOLog.error(result.statusMessage, { error: err });
309
320
  }
310
321
  OINOBenchmark.end("OINODb", "validate");
311
322
  return result;
@@ -5,7 +5,8 @@ import { OINODb, OINODbParams, OINODbDataSet, OINODbApi, OINODataCell, OINOResul
5
5
  */
6
6
  export declare class OINODbMariadb extends OINODb {
7
7
  private static _fieldLengthRegex;
8
- private static _exceptionMessageRegex;
8
+ private static _connectionExceptionMessageRegex;
9
+ private static _sqlExceptionMessageRegex;
9
10
  private _pool;
10
11
  /**
11
12
  * Constructor of `OINODbMariadb`
@@ -58,7 +59,7 @@ export declare class OINODbMariadb extends OINODb {
58
59
  * Connect to database.
59
60
  *
60
61
  */
61
- connect(): Promise<boolean>;
62
+ connect(): Promise<OINOResult>;
62
63
  /**
63
64
  * Validate connection to database is working.
64
65
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oino-ts/db-mariadb",
3
- "version": "0.6.0",
3
+ "version": "0.6.1",
4
4
  "description": "OINO TS package for using Mariadb databases.",
5
5
  "author": "Matias Kiviniemi (pragmatta)",
6
6
  "license": "MPL-2.0",
@@ -21,7 +21,7 @@
21
21
  "module": "./dist/esm/index.js",
22
22
  "types": "./dist/types/index.d.ts",
23
23
  "dependencies": {
24
- "@oino-ts/db": "^0.6.0",
24
+ "@oino-ts/db": "^0.6.1",
25
25
  "mariadb": "^3.2.3"
26
26
  },
27
27
  "devDependencies": {
@@ -91,7 +91,8 @@ class OINOMariadbData extends OINODbDataSet {
91
91
  export class OINODbMariadb extends OINODb {
92
92
 
93
93
  private static _fieldLengthRegex = /([^\(\)]+)(\s?\((\d+)\s?\,?\s?(\d*)?\))?/i
94
- private static _exceptionMessageRegex = /\(([^\)]*)\) (.*)\nsql\:(.*)?/i
94
+ private static _connectionExceptionMessageRegex = /\(([^\)]*)\) (.*)/i
95
+ private static _sqlExceptionMessageRegex = /\(([^\)]*)\) (.*)\nsql\:(.*)?/i
95
96
 
96
97
  private _pool:mariadb.Pool
97
98
 
@@ -106,8 +107,9 @@ export class OINODbMariadb extends OINODb {
106
107
  if (this._params.type !== "OINODbMariadb") {
107
108
  throw new Error(OINO_ERROR_PREFIX + ": Not OINODbMariadb-type: " + this._params.type)
108
109
  }
109
- this._pool = mariadb.createPool({ host: params.url, database: params.database, port: params.port, user: params.user, password: params.password, acquireTimeout: 2000, debug:false, rowsAsArray: true })
110
-
110
+ this._pool = mariadb.createPool({ host: this._params.url, database: this._params.database, port: this._params.port, user: this._params.user, password: this._params.password, acquireTimeout: 2000, debug:false, rowsAsArray: true })
111
+ delete this._params.password // do not store password in db object
112
+
111
113
  // this._pool.on("acquire", (conn: mariadb.Connection) => {
112
114
  // OINOLog.info("OINODbMariadb acquire", {conn:conn})
113
115
  // })
@@ -160,7 +162,7 @@ export class OINODbMariadb extends OINODb {
160
162
  return Promise.resolve(result)
161
163
 
162
164
  } catch (err) {
163
- const msg_parts = (err as Error).message.match(OINODbMariadb._exceptionMessageRegex) || []
165
+ const msg_parts = (err as Error).message.match(OINODbMariadb._sqlExceptionMessageRegex) || []
164
166
  // OINOLog.debug("OINODbMariadb._exec exception", {connection: msg_parts[1], message:msg_parts[2], sql:msg_parts[3]}) // print connection info just to log so tests don't break on runtime output
165
167
  throw new Error(msg_parts[2]);
166
168
  } finally {
@@ -285,17 +287,26 @@ export class OINODbMariadb extends OINODb {
285
287
  * Connect to database.
286
288
  *
287
289
  */
288
- async connect(): Promise<boolean> {
290
+ async connect(): Promise<OINOResult> {
291
+ const result:OINOResult = new OINOResult()
292
+ let connection:mariadb.Connection|null = null
289
293
  try {
290
294
  // make sure that any items are correctly URL encoded in the connection string
291
295
  // OINOLog.debug("OINODbMariadb.connect")
292
- await this._pool.on
293
- // await this._client.connect()
294
- return Promise.resolve(true)
296
+ connection = await this._pool.getConnection()
297
+ this.isConnected = true
298
+
295
299
  } catch (err) {
296
- // ... error checks
297
- throw new Error(OINO_ERROR_PREFIX + ": Error connecting to OINODbMariadb server: " + err)
298
- }
300
+ const msg_parts = (err as Error).message.match(OINODbMariadb._connectionExceptionMessageRegex) || []
301
+ result.setError(500, "Error connecting to server: " + msg_parts[2], "OINODbMariadb.connect")
302
+ OINOLog.error(result.statusMessage, {error:err})
303
+ } finally {
304
+ if (connection) {
305
+ await connection.end()
306
+ }
307
+ }
308
+
309
+ return Promise.resolve(result)
299
310
  }
300
311
 
301
312
  /**
@@ -309,7 +320,7 @@ export class OINODbMariadb extends OINODb {
309
320
  const sql = this._getValidateSql(this._params.database)
310
321
  // OINOLog.debug("OINODbMariadb.validate", {sql:sql})
311
322
  const sql_res:OINODbDataSet = await this.sqlSelect(sql)
312
- OINOLog.debug("OINODbMariadb.validate", {sql_res:sql_res})
323
+ // OINOLog.debug("OINODbMariadb.validate", {sql_res:sql_res})
313
324
  if (sql_res.isEmpty()) {
314
325
  result.setError(400, "DB returned no rows for select!", "OINODbMariadb.validate")
315
326
 
@@ -320,10 +331,11 @@ export class OINODbMariadb extends OINODb {
320
331
  result.setError(400, "DB returned no schema for database!", "OINODbMariadb.validate")
321
332
 
322
333
  } else {
323
- // connection is working
334
+ this.isValidated = true
324
335
  }
325
- } catch (e:any) {
326
- result.setError(500, OINO_ERROR_PREFIX + " (validate): OINODbMariadb.validate exception in _db.query: " + e.message, "OINODbMariadb.validate")
336
+ } catch (err:any) {
337
+ result.setError(500, "Exception validating connection: " + err.message, "OINODbMariadb.validate")
338
+ OINOLog.error(result.statusMessage, {error:err})
327
339
  }
328
340
  OINOBenchmark.end("OINODb", "validate")
329
341
  return result