Stephan
Stephan

Reputation: 23

Batch inserting array values into Database Node.js, Oracle, orawrap

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?

Upvotes: 1

Views: 988

Answers (1)

Dan McGhan
Dan McGhan

Reputation: 4659

The easiest way to do these kinds of tasks in Node.js is with the async library. Checkout the eachSeries method of the async library: https://github.com/caolan/async#each

That link points to each, but eachSeries is just a variation on each that runs things in series (one after the other) rather than in parallel. You just pass your array, the function that should be run for each element in the array, and another function to be invoked when the iteration is all done.

Upvotes: 2

Related Questions