1

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.

1

2 Answers 2

1

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);
    });
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.