storesafe / cordova-sqlite-storage-help

Help forum for Cordova sqlite plugin projects
2 stars 1 forks source link

code design pattern #5

Open francescogiunta opened 7 years ago

francescogiunta commented 7 years ago

This is generic question about how to code cleen and beauty with your plug-in. My question is about what best coding pattern to integrate db interaction? Due to asynckronous, interactions with db, generally looks like:

function addItem(first, last, acctNum) {

db.transaction(function (tx) {

    var query = "INSERT INTO customerAccounts (firstname, lastname, acctNo) VALUES (?,?,?)";

    tx.executeSql(query, [first, last, acctNum], function(tx, res) {
        console.log("insertId: " + res.insertId + " -- probably 1");
        console.log("rowsAffected: " + res.rowsAffected + " -- should be 1");
    },
    function(tx, error) {
        console.log('INSERT error: ' + error.message);
    });
}, function(error) {
    console.log('transaction error: ' + error.message);
}, function() {
    console.log('transaction ok');
});

}

Examples often show query results in console.logs

How is possible to instantiate vars with db query results, for example to split complex task in smallest functions?

How is it possible something like:

main task function( ) { function to extract values from 1st table; function to extract values from 2nd table; do something with both previous functions; }

or

var result = function to extract data from table;

Thank you for help!

result

brodycj commented 7 years ago

Marked for community help. You may get more help from Stack Overflow. For help with your JavaScript coding please contact sales@litehelpers.net.