-
Notifications
You must be signed in to change notification settings - Fork 3
/
diy.js
611 lines (562 loc) · 24.2 KB
/
diy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
'use strict'
var util = require('util');
var mysql = require('mysql');
var logger = require('winston');
var _ = require('lodash');
var async = require('async');
var wrappers = {
/*--------------------------------------------------------------------------
** Function Name : pingMySQLConnection
** Description : Helper function to test the db connection
**------------------------------------------------------------------------*/
pingMySQLConnection: function (options, callback) {
// getMySQLConnectionHelper will implicitly test the ability to connect,
// and return an error if connect fails.
getMySQLConnectionHelper(options, function (err, connection) {
if (err) {
return callback(null, {
statusCode: 401,
errors: [{code: err.code, message: util.inspect(err, {depth: null})}]
});
}
connection.end();
return callback(null, {statusCode: 200});
});
},
/*--------------------------------------------------------------------------
** Function Name : processMySQLExport
** Description : Export the rows using one of the export type (All,
** once, delta, test). Use the below configuration sample
**
** {
** "table" : "TABLE_NAME",
** "size" : 2
** }
**
** Field is the auto incremented col signifying row count
** Size is the number of rows to be exported at a time
**------------------------------------------------------------------------*/
processMySQLExport: function (options, callback) {
if (!options.configuration) {
return callback(new Error('!options.configuration'));
}
if(_.isEmpty(options.configuration.table)){
return callback(new Error('_.isEmpty(options.configuration.table)'));
}
var connection;
async.waterfall([
function (cb){
getMySQLConnectionHelper(options, function (err, conn){
if (err) {
return cb(err);
}
connection=conn;
return cb(null);
});
},
function (cb){
getPropertyField(connection, options.configuration.table, 'auto_increment', function(err, results){
if(err){
return cb(err);
}
return cb(null,results);
})
},
function (keyField, cb){
var args=[];
var type = _.get(options,'type','all');
var size = _.get(options,'configuration.query.size',100);
var startIndex = _.get(options,'state.startIndex',0);
var typeQuery = {
'all': ' select * from ?? where ?? > ? ',
'once': ' and ?? = 0 ',
'delta': ' and ?? > ? ',
'test': ' limit 1 ',
'suffix': ' order by ?? limit ? ',
'update': 'update ?? set ??=1 where ?? in ? '
}
var query=typeQuery.all;
args.push(options.configuration.table);
args.push(keyField);
args.push(startIndex);
switch (type) {
case 'all':
query += typeQuery.suffix;
args.push(keyField);
args.push(size);
break;
case 'once':
if(_.isEmpty(options.once.booleanField)){
return cb(new Error('_.isEmpty(options.once.booleanField)'));
}
query += typeQuery.once;
query += typeQuery.suffix;
args.push(options.once.booleanField);
args.push(keyField);
args.push(size);
break;
case 'delta':
if(_.isEmpty(options.delta.dateField)){
return cb(new Error('_.isEmpty(options.delta.dateField)'));
}
query += typeQuery.delta;
query += typeQuery.suffix;
args.push(options.delta.dateField);
args.push(new Date(options.delta.lastExecutionTime));
args.push(keyField);
args.push(size);
break;
case 'test':
query += typeQuery.test;
break;
default:
return cb(new Error('default'));
}
query = connection.format(query,args);
connection.query(query, function (err, rows) {
logger.info(query);
if (err) {
logger.error('connection.query', util.inspect(err, {depth: null}));
return cb(err);
}
var data = JSON.parse(JSON.stringify(rows));
var ids = [];
_.forEach(data,function(value){
ids.push(_.get(value,keyField));
});
if(_.isEqual(type,'test') || data.length < 1){
return cb(null,{data: data, lastPage: true});
}
if(_.isEqual(type,'delta') || _.isEqual(type,'all')){
var lastPage = data.length < size ? true : false;
var state = {
startIndex : _.last(ids)
}
return cb(null,{data: data, lastPage: lastPage, state: state });
}
query= connection.format(typeQuery.update, [options.configuration.table, options.once.booleanField, keyField, [ids]]);
connection.query(query, function(err,rows){
logger.info(query);
if (err) {
logger.error('connection.query', util.inspect(err, {depth: null}));
return cb(err);
}
if(rows.changedRows !== ids.length){
return cb(new Error('rows.changedRows !== ids.length'));
}
var lastPage = ids.length < size ? true : false;
var state = {
startIndex : _.last(ids)
}
return cb(null,{data: data, lastPage: lastPage, state: state });
});
});
}],function(err,results){
if(err){
if(connection){
connection.destroy();
}
logger.error('processMySQLExport', util.inspect(err, {depth: null}));
return callback(err);
}
connection.end();
return callback(null,results);
}
);
},
/*--------------------------------------------------------------------------
** Function Name : processMySQLImport
** Description : Import the rows to the db. The supported operations
** are ADD, UPDATE, ADDUPDATE. Below is the sample
** configuration
**
** {
** "importType" : "add",
** "table" : "TABLE_NAME",
** "ignoreExistingRecords" : "false",
** "ignoreMissingRecords" : "false",
** "existingRecCondition" : {
** "condition" : " COL_A = ? and COL_B = ? ",
** "colField" : ["COL_C","COL_D"]
** }
** }
**
** TABLE_NAME is the table on which the import is performed
**
** ignoreExistingRecords(for ADD) when set to false will try to
** insert the rows without checking the existing records.
**
** ignoreMissingRecords(for UPDATE) when set to false will not throw
** any error when the record to be updated is missing.
**
** existingRecCondition should be configured for all kind of operations
** except that of pure INSERT i.e. importType is "ADD" and
** ignoreExistingRecords is "true"
**
** condition is used for checking existing records and updating
** records. The condition needs to be written in MySql syntax
** where COL_A, COL_B represent table columns of import table.
**
** colField COL_C, COL_D are the table columns against which COL_A, COL_B
** needs to be checked.
**
** NOTE: Make sure that for all the operations of type ADD, UPDATE,
** ADDUPDATE, EXTERNAL_ID of source table is mapped to the
** auto_increment key field of destination table.
**
**------------------------------------------------------------------------*/
processMySQLImport: function (options, callback) {
if (!options.configuration || !options.configuration.importType) {
return callback(new Error('!options.configuration || !options.configuration.importType'));
}
if(!options.configuration.table){
return callback(new Error('!options.configuration.table'));
}
if (!Array.isArray(options.postMapData) || options.postMapData.length < 1) {
return callback(new Error('!Array.isArray(options.postMapData) || options.postMapData.length < 1'));
}
var importType = options.configuration.importType;
if(!(importType==='add' || importType==='update' || importType==='addUpdate')){
return callback(new Error('Invalid import type'));
}
var connection;
async.waterfall([
function(cb){
getMySQLConnectionHelper(options, function (err, conn) {
if (err) {
return cb(err);
}
connection=conn;
return cb(null);
});
},
function (cb){
getPropertyField(connection, options.configuration.table, 'auto_increment', function(err, results){
if(err){
return cb(err);
}
if(!_.includes(Object.keys(options.postMapData[0]),results)){
return cb(new Error('Key field missing in mapping'));
}
options.configuration.keyField= results;
return cb(null);
});
},
function (cb){
executeImportType(options, connection, cb);
}
],function(err, results){
if(err){
if(connection){
connection.destroy();
}
return callback(err);
}
connection.end();
logger.info(results);
callback(null,results);
});
}
}
/*--------------------------------------------------------------------------
** Function Name : executeImportType
** Description : Identify the type of import and call the relative
** functions.
**------------------------------------------------------------------------*/
function executeImportType(options,connection,callback){
if(options.configuration.importType === 'add'){
importTypeAdd(options,connection,function(err,results){
if(err){
return callback(err);
}
return callback(null,results);
});
}
else if (options.configuration.importType === 'update') {
importTypeUpdate(options,connection,function(err,results){
if(err){
return callback(err);
}
return callback(null,results);
});
}
else if (options.configuration.importType === 'addUpdate'){
importTypeAddUpdate(options,connection,function(err,results){
if(err){
return callback(err);
}
return callback(null,results);
});
}
}
/*--------------------------------------------------------------------------
** Function Name : importTypeAdd
** Description : Funtion to insert rows. When ignoreExistingRecords is
** set to false, it checks for existing row else it
** inserts the row into table.
**
**------------------------------------------------------------------------*/
function importTypeAdd(options,connection,callback){
if(!options.configuration.ignoreExistingRecords){
return callback(new Error('!options.configuration.ignoreExistingRecords'));
}
if(options.configuration.ignoreExistingRecords === 'true'){
if(!options.configuration.existingRecCondition || !options.configuration.existingRecCondition.condition || !options.configuration.existingRecCondition.colField){
return callback(new Error('!options.configuration.existingRecCondition || !options.configuration.existingRecCondition.condition || !options.configuration.existingRecCondition.colField'));
}
executeSelect(options,connection,function(err,results){
if(err){
return callback(err);
}
return callback(null,results);
});
}
else if(options.configuration.ignoreExistingRecords === 'false'){
executeInsert(options,connection,options.postMapData,function(err,results){
if(err){
return callback(err);
}
return callback(null,results);
});
}
else{
return callback(new Error('options.configuration.ignoreExistingRecords'));
}
}
/*--------------------------------------------------------------------------
** Function Name : importTypeUpdate
** Description : Funtion to update rows.
**
**------------------------------------------------------------------------*/
function importTypeUpdate(options,connection,callback){
if(!options.configuration.ignoreMissingRecords){
return callback(new Error('!options.configuration.ignoreMissingRecords'));
}
if(!options.configuration.existingRecCondition || !options.configuration.existingRecCondition.condition || !options.configuration.existingRecCondition.colField){
return callback(new Error('!options.configuration.existingRecCondition || !options.configuration.existingRecCondition.condition || !options.configuration.existingRecCondition.colField'));
}
executeUpdate(options,connection,function(err,results){
if(err){
return callback(err);
}
return callback(null,results);
});
}
/*--------------------------------------------------------------------------
** Function Name : importTypeAddUpdate
** Description : Funtion to add and update rows.
**
**------------------------------------------------------------------------*/
function importTypeAddUpdate(options,connection,callback){
if(!options.configuration.existingRecCondition || !options.configuration.existingRecCondition.condition || !options.configuration.existingRecCondition.colField){
return callback(new Error('!options.configuration.existingRecCondition || !options.configuration.existingRecCondition.condition || !options.configuration.existingRecCondition.colField'));
}
executeUpdate(options,connection,function(err,results){
if(err){
return callback(err);
}
return callback(null,results);
});
}
/*--------------------------------------------------------------------------
** Function Name : executeSelect
** Description : Funtion to identify existing rows. If the row does
** not exist then insert the row.
**
**------------------------------------------------------------------------*/
function executeSelect(options,connection,callback){
var toReturn = [];
var query = 'select * from ?? where ';
query += options.configuration.existingRecCondition.condition;
async.eachOf(options.postMapData, function(dataValue,index,cb){
var args = [];
args.push(options.configuration.table);
_.forEach(options.configuration.existingRecCondition.colField,function(condValue){
args.push(dataValue[condValue]);
});
var tempQuery = connection.format(query,args);
connection.query(tempQuery,function(err,results) {
logger.info(tempQuery);
if(err){
logger.error('executeSelect', util.inspect(err, {depth: null}));
toReturn[index] = {statusCode:422 , error:[{code:err.errno, message:err.message}]};
return cb();
}
else if(results.length < 1){
executeInsert(options,connection,[dataValue],function(err,results){
if(err){
logger.error('executeInsert', util.inspect(err, {depth: null}));
}
toReturn[index]=results[0];
return cb();
});
}
else{
toReturn[index] = {statusCode:200, id:results[0][options.configuration.keyField], ignored:true};
return cb();
}
});
}, function(){
return callback(null,toReturn);
});
}
/*--------------------------------------------------------------------------
** Function Name : executeUpdate
** Description : Funtion to update the rows. If the row does
** not exist then insert the row or throw error based
** on the importType and ignoreMissingRecords property.
**
**------------------------------------------------------------------------*/
function executeUpdate(options,connection,callback){
var ignoreMissingRecords = options.configuration.ignoreMissingRecords;
if((options.configuration.importType === 'update') && (!(ignoreMissingRecords ==='true' || ignoreMissingRecords === 'false'))){
return callback(new Error('Invalid value for ignoreMissingRecords'));
}
var toReturn=[];
var keys = Object.keys(options.postMapData[0]);
var query = 'update '+ options.configuration.table + ' set ';
_.forEach(keys, function(value){
query+=value + '=?, ';
});
query=_.trimEnd(query,' ,') + ' where ' + options.configuration.existingRecCondition.condition;
async.eachOf(options.postMapData,function(dataValue,index,cb){
var args = [];
_.forEach(keys, function(colValue){
args.push(dataValue[colValue]);
});
_.forEach(options.configuration.existingRecCondition.colField,function(colValue){
args.push(dataValue[colValue]);
});
var tempQuery = connection.format(query,args);
connection.query(tempQuery,function(err,results){
logger.info(tempQuery);
if(err){
logger.error('executeUpdate', util.inspect(err, {depth: null}));
toReturn[index] = {statusCode:422 , error:[{code:err.errno, message:err.message}]};
return cb();
}
if(results.affectedRows === 0){
if(options.configuration.importType === 'update' && options.configuration.ignoreMissingRecords === 'true'){
logger.error('Missing record: '+tempQuery);
toReturn[index] = {statusCode:422 , errors: [{code:1032, message:'Row not found: '+tempQuery }]};
return cb();
}
else if(options.configuration.importType === 'addUpdate'){
executeInsert(options,connection,[dataValue],function(err,results){
if(err){
logger.error('executeInsert', util.inspect(err, {depth: null}));
toReturn[index] = {statusCode:422 , error:[{code:err.errno, message:err.message}]};
return cb();
}
toReturn[index]=results[0];
return cb();
});
}
}
else{
toReturn[index]={statusCode: 200, id: options.preMapData[index][options.configuration.keyField]};
cb();
}
});
},function (){
return callback(null,toReturn);
});
}
/*--------------------------------------------------------------------------
** Function Name : executeInsert
** Description : Funtion to insert rows.
**
**------------------------------------------------------------------------*/
function executeInsert(options,connection,insertData,callback){
var data = [];
var toReturn = [];
var query = ' insert into ?? ( ?? ) values ? ';
var keys = Object.keys(options.postMapData[0]);
_.forEach(insertData, function(dataValue){
var temp=[];
_.forEach(keys, function(colValue){
temp.push(dataValue[colValue]);
});
data.push(temp);
});
async.eachOf(data,function(record, index, cb){
var args = [];
args.push(options.configuration.table);
args.push(keys);
args.push([record]);
var tempQuery = connection.format(query,args);
connection.query(tempQuery,function(err,results) {
logger.info(tempQuery);
if(err){
toReturn[index] = {statusCode:422 , error:[{code:err.errno, message:err.message}]};
return cb();
}
else {
toReturn[index]={statusCode: 200, id: results.insertId};
return cb();
}
});
}, function(){
return callback(null,toReturn);
});
}
/*--------------------------------------------------------------------------
** Function Name : getMySQLConnectionHelper
** Description : Helper function to craete the connection based on
** the configuration. Use the below sample to configure
**
** unEncrypted:{
** port: xxxx,
** database: 'xxxxxxxxxx',
** user: 'xxxxxxxxxx',
** host: 'xxxxxxxxxx'
** },
** encrypted: {
** password: 'xxxxxxxxxx'
** }
**
**------------------------------------------------------------------------*/
function getMySQLConnectionHelper (options, callback) {
if (!options.connection || !options.connection.unencrypted || !options.connection.encrypted) {
return callback(new Error('!options.connection || !options.connection.unencrypted || !options.connection.encrypted'));
}
var dbconfig = {
host : options.connection.unencrypted.host,
user : options.connection.unencrypted.user,
database : options.connection.unencrypted.database,
port : options.connection.unencrypted.port,
password : options.connection.encrypted.password
}
var connection = mysql.createConnection(dbconfig);
connection.connect(function (err) {
if (err) {
logger.error('connection.connect', util.inspect(err, {depth: null}));
return callback(err);
}
return callback(null, connection);
});
}
/*--------------------------------------------------------------------------
** Function Name : getPropertyField
** Description : Funtion to find columns of table based on some property.
**
**------------------------------------------------------------------------*/
function getPropertyField(connection, table, property, callback){
var query = 'select column_name from information_schema.columns where table_schema=database() and table_name=? and extra=?';
var args=[];
args.push(table);
args.push(property);
var query = connection.format(query,args);
connection.query(query, function(err,rows){
if(err){
return callback(err);
}
else if(rows.length==1){
return callback(null, rows[0]['column_name']);
}
else{
return callback(new Error(rows.length > 1 ? 'Too many `'+property+'` fields in table' : 'Missing `'+property+'` field in table'));
}
});
}
exports.wrappers = wrappers