@oino-ts/db-mariadb 0.6.0 → 0.7.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.
@@ -78,6 +78,13 @@ class OINOMariadbData extends db_1.OINODbDataSet {
78
78
  return db_1.OINODB_EMPTY_ROW;
79
79
  }
80
80
  }
81
+ /**
82
+ * Gets all rows of data.
83
+ *
84
+ */
85
+ async getAllRows() {
86
+ return this._rows; // at the moment theres no result streaming, so we can just return the rows
87
+ }
81
88
  }
82
89
  /**
83
90
  * Implementation of MariaDb/MySql-database.
@@ -85,7 +92,8 @@ class OINOMariadbData extends db_1.OINODbDataSet {
85
92
  */
86
93
  class OINODbMariadb extends db_1.OINODb {
87
94
  static _fieldLengthRegex = /([^\(\)]+)(\s?\((\d+)\s?\,?\s?(\d*)?\))?/i;
88
- static _exceptionMessageRegex = /\(([^\)]*)\) (.*)\nsql\:(.*)?/i;
95
+ static _connectionExceptionMessageRegex = /\(([^\)]*)\) (.*)/i;
96
+ static _sqlExceptionMessageRegex = /\(([^\)]*)\) (.*)\nsql\:(.*)?/i;
89
97
  _pool;
90
98
  /**
91
99
  * Constructor of `OINODbMariadb`
@@ -97,7 +105,8 @@ class OINODbMariadb extends db_1.OINODb {
97
105
  if (this._params.type !== "OINODbMariadb") {
98
106
  throw new Error(db_1.OINO_ERROR_PREFIX + ": Not OINODbMariadb-type: " + this._params.type);
99
107
  }
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 });
108
+ 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, multipleStatements: true });
109
+ delete this._params.password; // do not store password in db object
101
110
  // this._pool.on("acquire", (conn: mariadb.Connection) => {
102
111
  // OINOLog.info("OINODbMariadb acquire", {conn:conn})
103
112
  // })
@@ -148,7 +157,7 @@ class OINODbMariadb extends db_1.OINODb {
148
157
  return Promise.resolve(result);
149
158
  }
150
159
  catch (err) {
151
- const msg_parts = err.message.match(OINODbMariadb._exceptionMessageRegex) || [];
160
+ const msg_parts = err.message.match(OINODbMariadb._sqlExceptionMessageRegex) || [];
152
161
  // 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
162
  throw new Error(msg_parts[2]);
154
163
  }
@@ -193,7 +202,7 @@ class OINODbMariadb extends db_1.OINODb {
193
202
  else if (cellValue === undefined) {
194
203
  return "UNDEFINED";
195
204
  }
196
- else if ((sqlType == "int") || (sqlType == "smallint") || (sqlType == "float")) {
205
+ else if ((sqlType == "int") || (sqlType == "smallint") || (sqlType == "float") || (sqlType == "double")) {
197
206
  return cellValue.toString();
198
207
  }
199
208
  else if ((sqlType == "longblob") || (sqlType == "binary") || (sqlType == "varbinary")) {
@@ -270,17 +279,25 @@ class OINODbMariadb extends db_1.OINODb {
270
279
  *
271
280
  */
272
281
  async connect() {
282
+ const result = new db_1.OINOResult();
283
+ let connection = null;
273
284
  try {
274
285
  // make sure that any items are correctly URL encoded in the connection string
275
286
  // OINOLog.debug("OINODbMariadb.connect")
276
- await this._pool.on;
277
- // await this._client.connect()
278
- return Promise.resolve(true);
287
+ connection = await this._pool.getConnection();
288
+ this.isConnected = true;
279
289
  }
280
290
  catch (err) {
281
- // ... error checks
282
- throw new Error(db_1.OINO_ERROR_PREFIX + ": Error connecting to OINODbMariadb server: " + err);
291
+ const msg_parts = err.message.match(OINODbMariadb._connectionExceptionMessageRegex) || [];
292
+ result.setError(500, "Error connecting to server: " + msg_parts[2], "OINODbMariadb.connect");
293
+ db_1.OINOLog.error(result.statusMessage, { error: err });
283
294
  }
295
+ finally {
296
+ if (connection) {
297
+ await connection.end();
298
+ }
299
+ }
300
+ return Promise.resolve(result);
284
301
  }
285
302
  /**
286
303
  * Validate connection to database is working.
@@ -293,7 +310,7 @@ class OINODbMariadb extends db_1.OINODb {
293
310
  const sql = this._getValidateSql(this._params.database);
294
311
  // OINOLog.debug("OINODbMariadb.validate", {sql:sql})
295
312
  const sql_res = await this.sqlSelect(sql);
296
- db_1.OINOLog.debug("OINODbMariadb.validate", { sql_res: sql_res });
313
+ // OINOLog.debug("OINODbMariadb.validate", {sql_res:sql_res})
297
314
  if (sql_res.isEmpty()) {
298
315
  result.setError(400, "DB returned no rows for select!", "OINODbMariadb.validate");
299
316
  }
@@ -304,11 +321,12 @@ class OINODbMariadb extends db_1.OINODb {
304
321
  result.setError(400, "DB returned no schema for database!", "OINODbMariadb.validate");
305
322
  }
306
323
  else {
307
- // connection is working
324
+ this.isValidated = true;
308
325
  }
309
326
  }
310
- catch (e) {
311
- result.setError(500, db_1.OINO_ERROR_PREFIX + " (validate): OINODbMariadb.validate exception in _db.query: " + e.message, "OINODbMariadb.validate");
327
+ catch (err) {
328
+ result.setError(500, "Exception validating connection: " + err.message, "OINODbMariadb.validate");
329
+ db_1.OINOLog.error(result.statusMessage, { error: err });
312
330
  }
313
331
  db_1.OINOBenchmark.end("OINODb", "validate");
314
332
  return result;
@@ -328,7 +346,7 @@ class OINODbMariadb extends db_1.OINODb {
328
346
  result = new OINOMariadbData(sql_res, []);
329
347
  }
330
348
  catch (e) {
331
- result = new OINOMariadbData([[]], [db_1.OINO_ERROR_PREFIX + " (sqlSelect): OINODbMariadb.sqlSelect exception in _db.query: " + e.message]);
349
+ result = new OINOMariadbData(db_1.OINODB_EMPTY_ROWS, [db_1.OINO_ERROR_PREFIX + " (sqlSelect): OINODbMariadb.sqlSelect exception in _db.query: " + e.message]);
332
350
  }
333
351
  db_1.OINOBenchmark.end("OINODb", "sqlSelect");
334
352
  return result;
@@ -348,7 +366,7 @@ class OINODbMariadb extends db_1.OINODb {
348
366
  result = new OINOMariadbData(sql_res, []);
349
367
  }
350
368
  catch (e) {
351
- result = new OINOMariadbData([[]], [db_1.OINO_ERROR_PREFIX + " (sqlExec): exception in _db.exec [" + e.message + "]"]);
369
+ result = new OINOMariadbData(db_1.OINODB_EMPTY_ROWS, [db_1.OINO_ERROR_PREFIX + " (sqlExec): exception in _db.exec [" + e.message + "]"]);
352
370
  }
353
371
  db_1.OINOBenchmark.end("OINODb", "sqlExec");
354
372
  return result;
@@ -75,6 +75,13 @@ class OINOMariadbData extends OINODbDataSet {
75
75
  return OINODB_EMPTY_ROW;
76
76
  }
77
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
+ }
78
85
  }
79
86
  /**
80
87
  * Implementation of MariaDb/MySql-database.
@@ -82,7 +89,8 @@ class OINOMariadbData extends OINODbDataSet {
82
89
  */
83
90
  export class OINODbMariadb extends OINODb {
84
91
  static _fieldLengthRegex = /([^\(\)]+)(\s?\((\d+)\s?\,?\s?(\d*)?\))?/i;
85
- static _exceptionMessageRegex = /\(([^\)]*)\) (.*)\nsql\:(.*)?/i;
92
+ static _connectionExceptionMessageRegex = /\(([^\)]*)\) (.*)/i;
93
+ static _sqlExceptionMessageRegex = /\(([^\)]*)\) (.*)\nsql\:(.*)?/i;
86
94
  _pool;
87
95
  /**
88
96
  * Constructor of `OINODbMariadb`
@@ -94,7 +102,8 @@ export class OINODbMariadb extends OINODb {
94
102
  if (this._params.type !== "OINODbMariadb") {
95
103
  throw new Error(OINO_ERROR_PREFIX + ": Not OINODbMariadb-type: " + this._params.type);
96
104
  }
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 });
105
+ 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, multipleStatements: true });
106
+ delete this._params.password; // do not store password in db object
98
107
  // this._pool.on("acquire", (conn: mariadb.Connection) => {
99
108
  // OINOLog.info("OINODbMariadb acquire", {conn:conn})
100
109
  // })
@@ -145,7 +154,7 @@ export class OINODbMariadb extends OINODb {
145
154
  return Promise.resolve(result);
146
155
  }
147
156
  catch (err) {
148
- const msg_parts = err.message.match(OINODbMariadb._exceptionMessageRegex) || [];
157
+ const msg_parts = err.message.match(OINODbMariadb._sqlExceptionMessageRegex) || [];
149
158
  // 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
159
  throw new Error(msg_parts[2]);
151
160
  }
@@ -190,7 +199,7 @@ export class OINODbMariadb extends OINODb {
190
199
  else if (cellValue === undefined) {
191
200
  return "UNDEFINED";
192
201
  }
193
- else if ((sqlType == "int") || (sqlType == "smallint") || (sqlType == "float")) {
202
+ else if ((sqlType == "int") || (sqlType == "smallint") || (sqlType == "float") || (sqlType == "double")) {
194
203
  return cellValue.toString();
195
204
  }
196
205
  else if ((sqlType == "longblob") || (sqlType == "binary") || (sqlType == "varbinary")) {
@@ -267,17 +276,25 @@ export class OINODbMariadb extends OINODb {
267
276
  *
268
277
  */
269
278
  async connect() {
279
+ const result = new OINOResult();
280
+ let connection = null;
270
281
  try {
271
282
  // make sure that any items are correctly URL encoded in the connection string
272
283
  // OINOLog.debug("OINODbMariadb.connect")
273
- await this._pool.on;
274
- // await this._client.connect()
275
- return Promise.resolve(true);
284
+ connection = await this._pool.getConnection();
285
+ this.isConnected = true;
276
286
  }
277
287
  catch (err) {
278
- // ... error checks
279
- throw new Error(OINO_ERROR_PREFIX + ": Error connecting to OINODbMariadb server: " + err);
288
+ const msg_parts = err.message.match(OINODbMariadb._connectionExceptionMessageRegex) || [];
289
+ result.setError(500, "Error connecting to server: " + msg_parts[2], "OINODbMariadb.connect");
290
+ OINOLog.error(result.statusMessage, { error: err });
280
291
  }
292
+ finally {
293
+ if (connection) {
294
+ await connection.end();
295
+ }
296
+ }
297
+ return Promise.resolve(result);
281
298
  }
282
299
  /**
283
300
  * Validate connection to database is working.
@@ -290,7 +307,7 @@ export class OINODbMariadb extends OINODb {
290
307
  const sql = this._getValidateSql(this._params.database);
291
308
  // OINOLog.debug("OINODbMariadb.validate", {sql:sql})
292
309
  const sql_res = await this.sqlSelect(sql);
293
- OINOLog.debug("OINODbMariadb.validate", { sql_res: sql_res });
310
+ // OINOLog.debug("OINODbMariadb.validate", {sql_res:sql_res})
294
311
  if (sql_res.isEmpty()) {
295
312
  result.setError(400, "DB returned no rows for select!", "OINODbMariadb.validate");
296
313
  }
@@ -301,11 +318,12 @@ export class OINODbMariadb extends OINODb {
301
318
  result.setError(400, "DB returned no schema for database!", "OINODbMariadb.validate");
302
319
  }
303
320
  else {
304
- // connection is working
321
+ this.isValidated = true;
305
322
  }
306
323
  }
307
- catch (e) {
308
- result.setError(500, OINO_ERROR_PREFIX + " (validate): OINODbMariadb.validate exception in _db.query: " + e.message, "OINODbMariadb.validate");
324
+ catch (err) {
325
+ result.setError(500, "Exception validating connection: " + err.message, "OINODbMariadb.validate");
326
+ OINOLog.error(result.statusMessage, { error: err });
309
327
  }
310
328
  OINOBenchmark.end("OINODb", "validate");
311
329
  return result;
@@ -325,7 +343,7 @@ export class OINODbMariadb extends OINODb {
325
343
  result = new OINOMariadbData(sql_res, []);
326
344
  }
327
345
  catch (e) {
328
- result = new OINOMariadbData([[]], [OINO_ERROR_PREFIX + " (sqlSelect): OINODbMariadb.sqlSelect exception in _db.query: " + e.message]);
346
+ result = new OINOMariadbData(OINODB_EMPTY_ROWS, [OINO_ERROR_PREFIX + " (sqlSelect): OINODbMariadb.sqlSelect exception in _db.query: " + e.message]);
329
347
  }
330
348
  OINOBenchmark.end("OINODb", "sqlSelect");
331
349
  return result;
@@ -345,7 +363,7 @@ export class OINODbMariadb extends OINODb {
345
363
  result = new OINOMariadbData(sql_res, []);
346
364
  }
347
365
  catch (e) {
348
- result = new OINOMariadbData([[]], [OINO_ERROR_PREFIX + " (sqlExec): exception in _db.exec [" + e.message + "]"]);
366
+ result = new OINOMariadbData(OINODB_EMPTY_ROWS, [OINO_ERROR_PREFIX + " (sqlExec): exception in _db.exec [" + e.message + "]"]);
349
367
  }
350
368
  OINOBenchmark.end("OINODb", "sqlExec");
351
369
  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.7.0",
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.7.0",
25
25
  "mariadb": "^3.2.3"
26
26
  },
27
27
  "devDependencies": {
@@ -82,6 +82,14 @@ class OINOMariadbData extends OINODbDataSet {
82
82
  return OINODB_EMPTY_ROW
83
83
  }
84
84
  }
85
+
86
+ /**
87
+ * Gets all rows of data.
88
+ *
89
+ */
90
+ async getAllRows(): Promise<OINODataRow[]> {
91
+ return this._rows // at the moment theres no result streaming, so we can just return the rows
92
+ }
85
93
  }
86
94
 
87
95
  /**
@@ -91,7 +99,8 @@ class OINOMariadbData extends OINODbDataSet {
91
99
  export class OINODbMariadb extends OINODb {
92
100
 
93
101
  private static _fieldLengthRegex = /([^\(\)]+)(\s?\((\d+)\s?\,?\s?(\d*)?\))?/i
94
- private static _exceptionMessageRegex = /\(([^\)]*)\) (.*)\nsql\:(.*)?/i
102
+ private static _connectionExceptionMessageRegex = /\(([^\)]*)\) (.*)/i
103
+ private static _sqlExceptionMessageRegex = /\(([^\)]*)\) (.*)\nsql\:(.*)?/i
95
104
 
96
105
  private _pool:mariadb.Pool
97
106
 
@@ -106,8 +115,9 @@ export class OINODbMariadb extends OINODb {
106
115
  if (this._params.type !== "OINODbMariadb") {
107
116
  throw new Error(OINO_ERROR_PREFIX + ": Not OINODbMariadb-type: " + this._params.type)
108
117
  }
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
-
118
+ 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, multipleStatements: true })
119
+ delete this._params.password // do not store password in db object
120
+
111
121
  // this._pool.on("acquire", (conn: mariadb.Connection) => {
112
122
  // OINOLog.info("OINODbMariadb acquire", {conn:conn})
113
123
  // })
@@ -160,7 +170,7 @@ export class OINODbMariadb extends OINODb {
160
170
  return Promise.resolve(result)
161
171
 
162
172
  } catch (err) {
163
- const msg_parts = (err as Error).message.match(OINODbMariadb._exceptionMessageRegex) || []
173
+ const msg_parts = (err as Error).message.match(OINODbMariadb._sqlExceptionMessageRegex) || []
164
174
  // 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
175
  throw new Error(msg_parts[2]);
166
176
  } finally {
@@ -208,7 +218,7 @@ export class OINODbMariadb extends OINODb {
208
218
  } else if (cellValue === undefined) {
209
219
  return "UNDEFINED"
210
220
 
211
- } else if ((sqlType == "int") || (sqlType == "smallint") || (sqlType == "float")) {
221
+ } else if ((sqlType == "int") || (sqlType == "smallint") || (sqlType == "float") || (sqlType == "double")) {
212
222
  return cellValue.toString()
213
223
 
214
224
  } else if ((sqlType == "longblob") || (sqlType == "binary") || (sqlType == "varbinary")) {
@@ -285,17 +295,26 @@ export class OINODbMariadb extends OINODb {
285
295
  * Connect to database.
286
296
  *
287
297
  */
288
- async connect(): Promise<boolean> {
298
+ async connect(): Promise<OINOResult> {
299
+ const result:OINOResult = new OINOResult()
300
+ let connection:mariadb.Connection|null = null
289
301
  try {
290
302
  // make sure that any items are correctly URL encoded in the connection string
291
303
  // OINOLog.debug("OINODbMariadb.connect")
292
- await this._pool.on
293
- // await this._client.connect()
294
- return Promise.resolve(true)
304
+ connection = await this._pool.getConnection()
305
+ this.isConnected = true
306
+
295
307
  } catch (err) {
296
- // ... error checks
297
- throw new Error(OINO_ERROR_PREFIX + ": Error connecting to OINODbMariadb server: " + err)
298
- }
308
+ const msg_parts = (err as Error).message.match(OINODbMariadb._connectionExceptionMessageRegex) || []
309
+ result.setError(500, "Error connecting to server: " + msg_parts[2], "OINODbMariadb.connect")
310
+ OINOLog.error(result.statusMessage, {error:err})
311
+ } finally {
312
+ if (connection) {
313
+ await connection.end()
314
+ }
315
+ }
316
+
317
+ return Promise.resolve(result)
299
318
  }
300
319
 
301
320
  /**
@@ -309,7 +328,7 @@ export class OINODbMariadb extends OINODb {
309
328
  const sql = this._getValidateSql(this._params.database)
310
329
  // OINOLog.debug("OINODbMariadb.validate", {sql:sql})
311
330
  const sql_res:OINODbDataSet = await this.sqlSelect(sql)
312
- OINOLog.debug("OINODbMariadb.validate", {sql_res:sql_res})
331
+ // OINOLog.debug("OINODbMariadb.validate", {sql_res:sql_res})
313
332
  if (sql_res.isEmpty()) {
314
333
  result.setError(400, "DB returned no rows for select!", "OINODbMariadb.validate")
315
334
 
@@ -320,10 +339,11 @@ export class OINODbMariadb extends OINODb {
320
339
  result.setError(400, "DB returned no schema for database!", "OINODbMariadb.validate")
321
340
 
322
341
  } else {
323
- // connection is working
342
+ this.isValidated = true
324
343
  }
325
- } catch (e:any) {
326
- result.setError(500, OINO_ERROR_PREFIX + " (validate): OINODbMariadb.validate exception in _db.query: " + e.message, "OINODbMariadb.validate")
344
+ } catch (err:any) {
345
+ result.setError(500, "Exception validating connection: " + err.message, "OINODbMariadb.validate")
346
+ OINOLog.error(result.statusMessage, {error:err})
327
347
  }
328
348
  OINOBenchmark.end("OINODb", "validate")
329
349
  return result
@@ -344,7 +364,7 @@ export class OINODbMariadb extends OINODb {
344
364
  result = new OINOMariadbData(sql_res, [])
345
365
 
346
366
  } catch (e:any) {
347
- result = new OINOMariadbData([[]], [OINO_ERROR_PREFIX + " (sqlSelect): OINODbMariadb.sqlSelect exception in _db.query: " + e.message])
367
+ result = new OINOMariadbData(OINODB_EMPTY_ROWS, [OINO_ERROR_PREFIX + " (sqlSelect): OINODbMariadb.sqlSelect exception in _db.query: " + e.message])
348
368
  }
349
369
  OINOBenchmark.end("OINODb", "sqlSelect")
350
370
  return result
@@ -365,7 +385,7 @@ export class OINODbMariadb extends OINODb {
365
385
  result = new OINOMariadbData(sql_res, [])
366
386
 
367
387
  } catch (e:any) {
368
- result = new OINOMariadbData([[]], [OINO_ERROR_PREFIX + " (sqlExec): exception in _db.exec [" + e.message + "]"])
388
+ result = new OINOMariadbData(OINODB_EMPTY_ROWS, [OINO_ERROR_PREFIX + " (sqlExec): exception in _db.exec [" + e.message + "]"])
369
389
  }
370
390
  OINOBenchmark.end("OINODb", "sqlExec")
371
391
  return result