Nico
Nico

Reputation: 340

Node.js, multiple MySQL insertions

i want to do multiple MySQL insertions in a specefic ordner for my server-client-application.

var con = createDatabaseConnection();

function createDatabaseConnection() {
return (mysql.createConnection({
        host: // hostIp,
        user: // user,
        password: //pw,
        database: //databasename
    });
}

function writeFoo(fooObject) {
   var sql = // prepare sql insert for fooObject;
   con.query(sql, function (err) {
      // ? Is inserted data ready to use ?
   }
});

function writeBar(barObject) {
   var sql = // prepare sql insert for barObject;
   con.query(sql, function (err) {
      // ? Is inserted data ready to use ?
   });
});

// Both arrays containing an unpredictable amount of objects
var fooArray = [];
var barArray = [];

The crucial point is that I have to be absolutely sure, that the fooObjects in the fooArray are inserted before the barArray gets inserted. Because the SQL INSERT statement which is used in writeBar() contains a SELECT statement which has to find the data inserted in writeFoo(). And at any moment an event could happen (client sends data that needs to be inserted), that would need this functionality too.

The real situation is a bit more complex, but i think that's the main problem at the moment.

Upvotes: 1

Views: 108

Answers (2)

Mihai Iorga
Mihai Iorga

Reputation: 39704

You can chain them in a promise, this way you will make sure every call is in order, ex:

var con;
function createDatabaseConnection() {
    return new Promise(function (resolve, reject) {
        mysql.createConnection({
            host: hostIp,
            user: user,
            password: pw,
            database: databasename
        }, function(err, res){
            if(err) reject(err);
            else resolve(res);
        });
    });
}

function writeFoo(fooObject) {
    let sql = fooObject;
    return new Promise(function (resolve, reject) {
        con.query(sql, function (err, res) {
            if(err) reject(err);
            else resolve(res);
        });
    });
}

function writeBar(barObject) {
    let sql = barObject;
    return new Promise(function (resolve, reject) {
        con.query(sql, function (err, res) {
            if(err) reject(err);
            else resolve(res);
        });
    });
}

createDatabaseConnection()
    .catch(function (error) {
        // connection failed
        // log info
        console.error(error);
        process.exit(0);
    })
    .then(function (res) {
        con = res;
        return writeFoo(fooObject);
    })
    .catch(function (error) {
        // writeFoo(fooObject) failed
        // log info
        console.error(error);
        process.exit(0);
    })
    .then(function () {
        return writeBar(barObject);
    })
    .catch(function (error) {
        // writeBar(barObject) failed
        // log info
        console.error(error);
        process.exit(0);
    });

Upvotes: 1

Christian4423
Christian4423

Reputation: 1796

Not going to say ORM's are always best but http://docs.sequelizejs.com/ works well with node. There is a bulk insert option right in there.

Upvotes: 0

Related Questions