-
-
Save lbrenman/c1077d87675f1ec96c76d99bba1b10c0 to your computer and use it in GitHub Desktop.
Axway MBS Data Copier NodeJS Project assets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Time delay between API calls to post record to MBS | |
// Axway MBS imposes rate limiting of 20 API calls per second | |
exports.apiCallTimeInterval = 250; // 0.25 seconds | |
// Only MBS App Key is required to READ custom objects | |
exports.MBSFromKey = <MBS Key for FROM>; | |
// BS App Key and a User is required to write Custom Objects | |
exports.MBSToKey = <MBS Key for TO>; | |
exports.MBSToUsername = <MBS TO User username>; | |
exports.MBSToPassword = <MBS TO User password>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var MBS = require('arrowdb') | |
var config = require('./config.js') | |
var MBSFrom = new MBS(config.MBSFromKey) | |
var MBSTo = new MBS(config.MBSToKey) | |
let configErrorMsg = 'Remember to enter your MBS details in the config.js file.' | |
let apiCallTimeInterval = config.apiCallTimeInterval; | |
var MBSLogin = function (instance, username, password, callback) { | |
console.log('MBSLogin() called'); | |
instance.usersLogin({ login: username, password: password }, function (err,result) { | |
if (err) { | |
// console.error('MBSLogin() - MBS login error = ' + err.message) | |
callback({ success: false, msg: err.message+' '+configErrorMsg }) | |
} else { | |
// console.log('MBSLogin() - MBS login success') | |
callback({ success: true, msg: result.body.meta.session_id }) | |
} | |
}) | |
} | |
var writeToMBS = function (MBS, obj, classname) { | |
console.log('writeToMBS() called') | |
var newObj = {} | |
for (var prop in obj) { | |
if (obj.hasOwnProperty(prop)) { | |
if ( | |
prop != 'user_id' && | |
prop != 'id' && | |
prop != 'created_at' && | |
prop != 'updated_at' | |
) { | |
newObj[prop] = obj[prop] | |
} | |
} | |
} | |
MBS.customObjectsCreate( | |
{ classname: classname, fields: newObj }, | |
function (err, result) { | |
if (err) { | |
console.error('copytoprod - customObjectsCreate error = ' + err.message+' '+configErrorMsg) | |
} else { | |
console.error( | |
'copytoprod - customObjectsCreate success = ' + | |
JSON.stringify(result.body.response) | |
) | |
} | |
} | |
) | |
} | |
if (process.argv.length < 3) { | |
console.log('\nERROR: Pass desired custom object, e.g. node index.js employee\n') | |
process.exit(-1) | |
} else { | |
MBSLogin(MBSTo, config.MBSToUsername, config.MBSToPassword, function (e) { | |
if (e.success) { | |
console.log('MBSLogin() successful'); | |
MBSFrom.customObjectsQuery({classname: process.argv[2], limit: 1000}, function(err, result) { | |
if (err) { | |
console.error('customObjectsQuery error = '+err.message+' '+configErrorMsg); | |
} else { | |
console.error('customObjectsQuery success = '+JSON.stringify(result.body.response)); | |
// | |
// Avoid rate limiting | |
//https://stackoverflow.com/questions/14368061/nodejs-async-queue-too-fast-slowing-down-async-queue-method | |
// | |
var dataLen = result.body.response[process.argv[2]].length; | |
console.log('dataLen = '+dataLen); | |
if(dataLen>0){ | |
var counter = 0; | |
var myInterval = setInterval(function() { | |
counter++; | |
if(counter >= dataLen){ | |
clearInterval(myInterval); | |
} | |
writeToMBS(MBSTo, result.body.response[process.argv[2]][counter-1], process.argv[2]); | |
}, apiCallTimeInterval); | |
} | |
} | |
}); | |
} else { | |
console.log('MBSLogin() failed.'+' '+configErrorMsg); | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment