I have an application that runs in the Browser and communicates with a REST service. The rest service can has to write data to an Oracle DB lets say it has to write an Entity like this:
var entity = {
"id": 0,
"date": new Date(),
"comment": 'test'
};
And I have a Statement, that looks like this:
ar statement = 'INSERT INTO MY_TABLE ( '
+ ' THE_ID '
+ ' , THE_DATE '
+ ' , THE_COMMENT '
+ ' ) VALUES ( '
+ ' :id, '
+ " :date, "
+ ' :comment'
+ ')';
The array of entities is called:
var entities = [];
Now lets say i have an array of entity with the length of around 30. I tried recursive:
var i = 0;
// Function that Batch inserts an Array.
var insertArray = function (statement, entities, i, cb) {
if (i >= entities.length) {
console.log("exit insertArray.");
return cb(null, true);
}
insert(statement, entities[i], function (err, val) {
if (err) {
console.log('error insertArray: ', err);
return cb(err, null);
}
insertArray(statement, entities, ++i, cb);
});
};
This is the function that inserts an entity into the DB
var insert = function (statement, entity, cb) {
orawrap.execute(
statement,
{
"id": { val: entity.id, dir: oracledb.BIND_INOUT, type: oracledb.NUMBER },
"date": { val: entity.date, dir: oracledb.BIND_INOUT, type: oracledb.DATE },
"comment": { val: entity.comment, dir: oracledb.BIND_INOUT, type: oracledb.STRING }
},
cb);
};
This code stops inserting after around 3 recursive calls. i tried in a for loop as well. But since its nicer to use callbacks in an asynchronous environment, the recursive idea seems nicer to me. Anyhow the iterative approach did not batch insert the values as well.
Do you have an idea how to solve this?