@blazedpath/commons 0.0.11 → 0.1.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.
@@ -18,26 +18,24 @@ process.on('exit', async function () {
18
18
  });
19
19
 
20
20
  module.exports = {
21
-
22
- syntaxis: require("./syntaxis.json"),
23
- executeProcedure,
21
+ syntaxis: require("./syntaxis.json"),
22
+ executeProcedure,
24
23
  executeSql: async function (connection, sql, parameters, options = {}) {
25
- if (sql.indexOf("call") != -1) {
24
+ if(sql.indexOf("call") != -1 ) {
26
25
  return await executeProcedure(connection, sql, parameters);
27
- } else if (_.has(options, "bulkInsert")) {
28
- return await executeBulkInsert(connection, sql, parameters, options);
29
- } else if (_.has(options, "bulkMerge")) {
30
- return await executeBulkMerge(connection, sql, parameters, options);
31
- } else if (_.has(options, "queryOne") || _.has(options, "query")) {
26
+ } else if(_.has(options, "bulkInsert")) {
27
+ return await executeBulkInsert(connection, sql, parameters, options);
28
+ } else if(_.has(options, "bulkMerge")) {
29
+ return await executeBulkMerge(connection, sql, parameters, options);
30
+ } else if(_.has(options, "queryOne") || _.has(options, "query")) {
32
31
  return await executeQuery(connection, sql, parameters, options);
33
32
  } else {
34
33
  return await executeNonQuery(connection, sql, parameters, options);
35
34
  }
36
35
  },
37
- beginTransaction,
36
+ beginTransaction,
38
37
  close,
39
38
  createRdsConnection,
40
39
  commitTransaction,
41
- rollbackTransaction
42
-
40
+ rollbackTransaction,
43
41
  }
@@ -31,8 +31,8 @@ const getPool = async function (connection) {
31
31
  connection = _.omit(connection, "dataSource", "providerName");
32
32
  connection.sessionCallback = makeInitSessionCallback({ schema: connection.schema })
33
33
  // Enable instant client mode in Node.js
34
- if (process.env.LD_LIBRARY_PATH) {
35
- oracledb.initOracleClient({ libDir: process.env.LD_LIBRARY_PATH, configDir: '', thin: false });
34
+ if (process.env.LD_LIBRARY_PATH){
35
+ oracledb.initOracleClient( { libDir: process.env.LD_LIBRARY_PATH, configDir: '', thin: false} );
36
36
  }
37
37
  pool = await oracledb.createPool(connection);
38
38
  pool.connectionName = connection.name;
@@ -68,13 +68,13 @@ const tryDatetime = function (value) {
68
68
  if (match) {
69
69
  const probableDate = new Date(value);
70
70
  if (probableDate.toString() !== 'Invalid Date') {
71
- return probableDate;
71
+ return probableDate;
72
72
  }
73
73
  }
74
74
  }
75
75
  return null;
76
76
  };
77
- const convertMilliseconds = function (strMilliseconds) {
77
+ const convertMilliseconds = function(strMilliseconds) {
78
78
  if (strMilliseconds) {
79
79
  if (strMilliseconds.length < 3)
80
80
  strMilliseconds = strMilliseconds.padEnd(3, '0');
@@ -127,15 +127,15 @@ const getParameterType = function (type) {
127
127
  case 'VARCHAR2':
128
128
  return oracledb.STRING;
129
129
  default:
130
- return oracledb.STRING
130
+ return oracledb.STRING
131
131
  }
132
132
  }
133
133
 
134
- const getOraParameters = function (parameters, options) {
134
+ const getOraParameters = function (parameters,options) {
135
135
  let oraParameters = {};
136
136
  if (parameters)
137
137
  for (let i = 0; i < parameters.length; i++) {
138
- let parameter = parameters[i];
138
+ let parameter = parameters[i];
139
139
  let oraParameter = {};
140
140
  // checks if i need to convert nodejs to oracle representation (bool -> 0,1) etcs
141
141
  if (parameter.direction === 'in' || parameter.direction === 'in/out') {
@@ -175,7 +175,7 @@ const getOraParameters = function (parameters, options) {
175
175
  if (parameter.isResultSet)
176
176
  oraParameter.type = oracledb.CURSOR;
177
177
  else {
178
- if (parameter.dbTypes) {
178
+ if (parameter.dbTypes){
179
179
  const oracleEntry = parameter.dbTypes.find(entry => entry.providerName === 'Oracle');
180
180
  parameter.dbType = getParameterType(oracleEntry.dbType);
181
181
  } else if (parameter.type) {
@@ -193,7 +193,7 @@ const getOraParameters = function (parameters, options) {
193
193
  return oraParameters
194
194
  }
195
195
 
196
- const getOraOptions = function (options, bindDefs) {
196
+ const getOraOptions = function (options , bindDefs) {
197
197
  let oraOptions = { autoCommit: !options.isTransactionRequest };
198
198
  if (options && (options.query || options.queryOne)) {
199
199
  oraOptions.resultSet = true;
@@ -210,29 +210,29 @@ const convertValueFromType = async function (value, type) {
210
210
  return BlzBase.convertToString(value);
211
211
  }
212
212
  case 'integer': {
213
- return BlzBase.convertToInteger(value);
213
+ return BlzBase.convertToInteger(value);
214
214
  }
215
215
  case 'decimal': {
216
- return BlzBase.convertToDecimal(value);
216
+ return BlzBase.convertToDecimal(value);
217
217
  }
218
218
  case 'boolean': {
219
- return BlzBase.convertToBoolean(value);
219
+ return BlzBase.convertToBoolean(value);
220
220
  }
221
221
  case 'datetime': {
222
- return BlzBase.convertToDatetime(value);
222
+ return BlzBase.convertToDatetime(value);
223
223
  }
224
224
  case 'date': {
225
- return BlzBase.convertToDate(value);
225
+ return BlzBase.convertToDate(value);
226
226
  }
227
227
  case 'time': {
228
- return BlzBase.convertToTime(value);
228
+ return BlzBase.convertToTime(value);
229
229
  }
230
230
  case 'binary': {
231
- return BlzBase.convertToBinary(value);
231
+ return BlzBase.convertToBinary(value);
232
232
  }
233
- default: {
233
+ default:{
234
234
  // If i have a clob, or special type, the case might not be straightforward
235
- if (value._type.name === 'DB_TYPE_CLOB') {
235
+ if (value._type.name === 'DB_TYPE_CLOB' ) {
236
236
  return await oraResult.outBinds[parameter.name].getData();
237
237
  }
238
238
  }
@@ -288,19 +288,19 @@ process.on('exit', async function () {
288
288
  });
289
289
 
290
290
  const getValue = function (source, type) {
291
- const _type = type ? type.toLowerCase() : typeof source
291
+ const _type = type ? type.toLowerCase() : typeof source
292
292
  switch (_type.toLowerCase()) {
293
- case 'boolean':
294
- return source ? 1 : 0
295
- case 'string':
296
- return typeof source === 'string' || source === null || source === undefined ? source : source.toString()
297
- case 'datetime':
298
- case 'date':
299
- case 'time':
300
- return source ? new Date(source) : null
301
- default:
302
- return source
303
- }
293
+ case 'boolean':
294
+ return source ? 1 : 0
295
+ case 'string':
296
+ return typeof source === 'string' || source === null || source === undefined ? source : source.toString()
297
+ case 'datetime':
298
+ case 'date':
299
+ case 'time':
300
+ return source ? new Date(source) : null
301
+ default:
302
+ return source
303
+ }
304
304
  }
305
305
 
306
306
  /**
@@ -343,10 +343,10 @@ const rowsToArray = function (columns, rows) {
343
343
  const columnsCount = columns.length
344
344
  for (const row of rows) {
345
345
  const item = {}
346
- for (let i = 0; i < columnsCount; i++) {
347
- const column = columns[i]
346
+ for(let i=0 ; i < columnsCount ; i++) {
347
+ const column = columns[i]
348
348
  const value = row[i]
349
- item[column.name] = getValue(value, column.type)
349
+ item[column.name] = getValue(value,column.type)
350
350
  }
351
351
  array.push(item)
352
352
  }
@@ -354,12 +354,11 @@ const rowsToArray = function (columns, rows) {
354
354
  }
355
355
 
356
356
  module.exports = {
357
-
358
357
  syntaxis: require('./syntaxis.json'),
359
358
  createRdsConnection: async function (connection) {
360
359
  try {
361
360
  let oraPool = await getPool(connection);
362
- let oraConnection = await oraPool.getConnection();
361
+ let oraConnection = await oraPool.getConnection();
363
362
  return oraConnection;
364
363
  }
365
364
  catch (err) {
@@ -367,17 +366,17 @@ module.exports = {
367
366
  }
368
367
  },
369
368
  executeSql: async function (connection, sql, parameters, options) {
370
- if (_.has(options, "bulkInsert")) {
371
- return await this._executeBulkInsert(connection, sql, parameters, options);
372
- } else if (_.has(options, "bulkMerge")) {
373
- return await this._executeBulkMerge(connection, sql, parameters, options);
369
+ if(_.has(options, "bulkInsert")) {
370
+ return await this._executeBulkInsert(connection, sql, parameters, options);
371
+ } else if(_.has(options, "bulkMerge")) {
372
+ return await this._executeBulkMerge(connection, sql, parameters, options);
374
373
  } else {
375
374
  return await this._executeSql(connection, sql, parameters, options);
376
375
  }
377
376
  },
378
377
  _executeSql: async function (connection, sql, parameters, options) {
379
- try {
380
- let oraParameters = getOraParameters(parameters, options);
378
+ try{
379
+ let oraParameters = getOraParameters(parameters,options);
381
380
  let oraOptions = getOraOptions(options)
382
381
  let oraResult = await connection.execute(sql, oraParameters, oraOptions);
383
382
  const result = await getResult(parameters, oraResult, options);
@@ -388,43 +387,42 @@ module.exports = {
388
387
  },
389
388
  _executeBulkInsert: async function (connection, sql, rows, options) {
390
389
  let autoincremental = null
391
- if (options.autoincrementalData && options.autoincrementalData.pkColumnName) {
392
- autoincremental = {
393
- name: options.autoincrementalData.pkColumnName,
394
- alias: options.autoincrementalData.pkColumnAlias,
395
- sequence: options.autoincrementalData.sequenceName,
396
- type: options.autoincrementalData.pkColumnType
397
- }
390
+ if (options.autoincrementalData && options.autoincrementalData.pkColumnName) {
391
+ autoincremental = { name: options.autoincrementalData.pkColumnName ,
392
+ alias: options.autoincrementalData.pkColumnAlias,
393
+ sequence: options.autoincrementalData.sequenceName,
394
+ type: options.autoincrementalData.pkColumnType
395
+ }
398
396
  }
399
- const _sql = autoincremental ?
400
- 'INSERT INTO ' + options.rdsTable.tableName +
401
- ' (' + autoincremental.name + ',' + options.setsFields.join(', ') +
402
- ') VALUES ( ' + autoincremental.sequence + '.NEXTVAL, ' + options.setsFields.map((field) => ':' + field).join(', ') + ' ) ' +
403
- ' RETURNING ' + autoincremental.name + ' INTO :' + autoincremental.name
404
- : sql + '(' + options.setsFields.map((field) => ':' + field).join(', ') + ' ) '
405
-
397
+ const _sql = autoincremental ?
398
+ 'INSERT INTO ' + options.rdsTable.tableName +
399
+ ' (' + autoincremental.name + ',' + options.setsFields.join(', ') +
400
+ ') VALUES ( ' + autoincremental.sequence + '.NEXTVAL, ' + options.setsFields.map((field) => ':' + field).join(', ') + ' ) ' +
401
+ ' RETURNING ' + autoincremental.name + ' INTO :' + autoincremental.name
402
+ : sql + '(' +options.setsFields.map((field) => ':' + field).join(', ') + ' ) '
403
+
406
404
  // solve max size for string fields
407
- const columnsCount = options.columns.length
408
- for (let i = 0; i < columnsCount; i++) {
405
+ const columnsCount = options.columns.length
406
+ for(let i=0 ; i < columnsCount ; i++) {
409
407
  const column = options.columns[i]
410
408
  if (column.type === 'string') {
411
- column.size = rows.reduce((max, row) => { return !row[i] ? 0 : row[i].length > max ? row[i].length : max }, 0)
409
+ column.size = rows.reduce((max, row) => { return !row[i]?0:row[i].length > max ? row[i].length : max }, 0)
412
410
  }
413
411
  }
414
412
  const bindDefs = {}
415
- for (const column of options.columns) {
416
- const bindDef = { type: getDbType(column.type) }
413
+ for (const column of options.columns) {
414
+ const bindDef = { type:getDbType(column.type) }
417
415
  if (column.type === 'string') {
418
416
  bindDef.maxSize = column.size
419
417
  }
420
- bindDefs[column.name] = bindDef
421
- }
418
+ bindDefs[column.name] = bindDef
419
+ }
422
420
  if (autoincremental) {
423
- bindDefs[autoincremental.name] = { type: getDbType(autoincremental.type), dir: oracledb.BIND_OUT }
424
- }
425
- const oraOptions = getOraOptions(options, bindDefs)
426
- const data = rowsToArray(options.columns, rows)
427
- try {
421
+ bindDefs[autoincremental.name] = { type: getDbType(autoincremental.type), dir: oracledb.BIND_OUT}
422
+ }
423
+ const oraOptions = getOraOptions(options,bindDefs)
424
+ const data = rowsToArray(options.columns,rows)
425
+ try {
428
426
  const result = await connection.executeMany(_sql, data, oraOptions)
429
427
  if (autoincremental) {
430
428
  const ids = []
@@ -438,94 +436,93 @@ module.exports = {
438
436
  } catch (err) {
439
437
  console.log(err)
440
438
  throw err
441
- }
439
+ }
442
440
  },
443
441
  _executeBulkMerge: async function (connection, sql, rows, options) {
444
442
  // example: https://stackoverflow.com/questions/67881781/how-to-execute-multiple-merge-into-query-on-oracle-db-from-node-js
445
443
  let autoincremental = null
446
- if (options.autoincrementalData && options.autoincrementalData.pkColumnName) {
447
- autoincremental = {
448
- name: options.autoincrementalData.pkColumnName,
449
- alias: options.autoincrementalData.pkColumnAlias,
450
- sequence: options.autoincrementalData.sequenceName,
451
- type: options.autoincrementalData.pkColumnType
452
- }
444
+ if (options.autoincrementalData && options.autoincrementalData.pkColumnName) {
445
+ autoincremental = { name: options.autoincrementalData.pkColumnName ,
446
+ alias: options.autoincrementalData.pkColumnAlias,
447
+ sequence: options.autoincrementalData.sequenceName,
448
+ type: options.autoincrementalData.pkColumnType
449
+ }
453
450
  }
454
451
  const _sql = []
455
- _sql.push('MERGE INTO ' + options.rdsTable.tableName + ' t USING (')
452
+ _sql.push('MERGE INTO ' + options.rdsTable.tableName + ' t USING (')
456
453
  _sql.push('SELECT ')
457
454
  let first = true
458
455
  for (const column of options.columns) {
459
- _sql.push((first ? '' : ', ') + ':' + column.name + ' AS ' + column.name)
456
+ _sql.push((first?'': ', ') + ':' + column.name + ' AS ' + column.name)
460
457
  first = false
461
458
  }
462
459
  _sql.push('FROM DUAL')
463
460
  _sql.push(') s ON (')
464
- first = true
465
- for (const field of options.mergeFields) {
466
- _sql.push((first ? '' : ' AND ') + 't.' + field + ' = s.' + field)
461
+ first = true
462
+ for(const field of options.mergeFields) {
463
+ _sql.push((first?'': ' AND ') + 't.' + field + ' = s.' + field )
467
464
  first = false
468
465
  }
469
466
  _sql.push(')')
470
467
  _sql.push('WHEN MATCHED THEN UPDATE SET ')
471
468
  first = true
472
- for (const column of options.columns) {
473
- if (options.mergeFields.includes(column.name)) continue
474
- _sql.push((first ? '' : ',') + 't.' + column.name + ' = s.' + column.name)
469
+ for(const column of options.columns) {
470
+ if(options.mergeFields.includes(column.name)) continue
471
+ _sql.push((first?'': ',') + 't.' + column.name + ' = s.' + column.name)
475
472
  first = false
476
473
  }
477
474
  _sql.push('WHEN NOT MATCHED THEN INSERT (')
478
- if (autoincremental) {
475
+ if(autoincremental) {
479
476
  _sql.push(autoincremental.name)
480
- for (const column of options.columns) {
477
+ for(const column of options.columns) {
481
478
  _sql.push(',' + column.name)
482
479
  }
483
480
  _sql.push(') VALUES (')
484
481
  _sql.push(autoincremental.sequence + '.NEXTVAL')
485
- for (const column of options.columns) {
482
+ for(const column of options.columns) {
486
483
  _sql.push(',s.' + column.name)
487
484
  }
488
485
  } else {
489
486
  first = true
490
- for (const column of options.columns) {
491
- _sql.push((first ? '' : ',') + column.name)
487
+ for(const column of options.columns) {
488
+ _sql.push((first?'': ',') + column.name)
492
489
  first = false
493
490
  }
494
491
  _sql.push(') VALUES (')
495
492
  first = true
496
- for (const column of options.columns) {
497
- _sql.push((first ? '' : ',') + 's.' + column.name)
493
+ for(const column of options.columns) {
494
+ _sql.push((first?'': ',') + 's.' + column.name)
498
495
  first = false
499
496
  }
500
497
  }
501
498
  _sql.push(')')
502
499
  const mergeSql = _sql.join(' ')
503
-
500
+
504
501
  // solve max size for string fields
505
- const columnsCount = options.columns.length
506
- for (let i = 0; i < columnsCount; i++) {
502
+ const columnsCount = options.columns.length
503
+ for(let i=0 ; i < columnsCount ; i++) {
507
504
  const column = options.columns[i]
508
505
  if (column.type === 'string') {
509
- column.size = rows.reduce((max, row) => { return !row[i] ? 0 : row[i].length > max ? row[i].length : max }, 0)
506
+ column.size = rows.reduce((max, row) => { return !row[i]?0:row[i].length > max ? row[i].length : max }, 0)
510
507
  }
511
508
  }
512
509
  const bindDefs = {}
513
- for (const column of options.columns) {
514
- const bindDef = { type: getDbType(column.type) }
510
+ for (const column of options.columns) {
511
+ const bindDef = { type:getDbType(column.type) }
515
512
  if (column.type === 'string') {
516
513
  bindDef.maxSize = column.size
517
514
  }
518
- bindDefs[column.name] = bindDef
519
- }
520
- const oraOptions = getOraOptions(options, bindDefs)
521
- const data = rowsToArray(options.columns, rows)
522
- try {
515
+ bindDefs[column.name] = bindDef
516
+ }
517
+ const oraOptions = getOraOptions(options,bindDefs)
518
+ const data = rowsToArray(options.columns,rows)
519
+ try {
523
520
  const result = await connection.executeMany(mergeSql, data, oraOptions)
524
521
  return { rowsAffected: result.rowsAffected }
525
522
  } catch (err) {
526
523
  console.log(err)
527
524
  throw err
528
- }
525
+ }
529
526
  },
530
527
  close(nativeConnection) {
531
528
  return nativeConnection.close();
@@ -539,6 +536,4 @@ module.exports = {
539
536
  rollbackTransaction(nativeConnection) {
540
537
  return nativeConnection.rollback();
541
538
  }
542
-
543
-
544
539
  };
@@ -18,26 +18,24 @@ process.on('exit', async function () {
18
18
  });
19
19
 
20
20
  module.exports = {
21
-
22
- syntaxis: require('./syntaxis.json'),
23
- executeProcedure,
21
+ syntaxis: require('./syntaxis.json'),
22
+ executeProcedure,
24
23
  executeSql: async function (connection, sql, parameters, options = {}) {
25
- if (sql.indexOf("call") != -1) {
24
+ if(sql.indexOf("call") != -1 ) {
26
25
  return await executeProcedure(connection, sql, parameters);
27
- } else if (_.has(options, "bulkInsert")) {
28
- return await executeBulkInsert(connection, sql, parameters, options);
29
- } else if (_.has(options, "bulkMerge")) {
30
- return await executeBulkMerge(connection, sql, parameters, options);
31
- } else if (_.has(options, "queryOne") || _.has(options, "query")) {
26
+ } else if(_.has(options, "bulkInsert")) {
27
+ return await executeBulkInsert(connection, sql, parameters, options);
28
+ } else if(_.has(options, "bulkMerge")) {
29
+ return await executeBulkMerge(connection, sql, parameters, options);
30
+ } else if(_.has(options, "queryOne") || _.has(options, "query")) {
32
31
  return await executeQuery(connection, sql, parameters, options);
33
32
  } else {
34
- return await executeNonQuery(connection, sql, parameters, options);
33
+ return await executeNonQuery(connection, sql, parameters, options );
35
34
  }
36
35
  },
37
- beginTransaction,
36
+ beginTransaction,
38
37
  close,
39
38
  createRdsConnection,
40
39
  commitTransaction,
41
- rollbackTransaction
42
-
40
+ rollbackTransaction,
43
41
  }