SQL transactions not right?

I have a JSON I get from an API which I iterate through with some simple loops and then feed data to some SQL inserts to populate some tables, which are divided by models. The current method works like this:

.js:

processed_data = data.json();

if (processed_data.products) {
    for (let i = 0; i < processed_data.products.length; i++) {
        promises.push(this.product.addItemSQL(processed_data.products[i]));
    }
}
...

( I do promises.push instead of running the functions as is because I need to wait until all the inserts are done to go to the next page )
And then in the model I have the addItemSQL funciton which is just:

return this.platform.ready().then(() => {
    return this.db.query("INSERT INTO ...");
},(error) => {
});

But the result is that it’s rather slow; apparently I can use transactions to speed up these inserts, but I’m unsure how.

If you want to fastest solution you have to use transaction.

var db = window.sqlitePlugin.openDatabase(..);
db.transaction(function (tx) {
                    yourListFromJson.forEach(function (row) {
                        var data = [row.Id, row.x, row.y, row.z....];

                        tx.executeSql("INSERT INTO TABLE.....", data);
                    });
}

These insert statements will run bulk execute in a transaction.

Okay, so based on that and other threads here I’ve done this:

Instead of using:

if (processed_data.products) {
    for (let i = 0; i < processed_data.products.length; i++) {
        promises.push(this.product.addItemSQL(processed_data.products[i]));
    }
}

I’m using:

if (processed_data.products) {
    var productBulk = [];
    for (let i = 0; i < processed_data.products.length; i++) {
        productBulk.push(processed_data.products[i]);
    }
    promises.push(this.product.bulkInsert(productBulk));
}

And so, instead of using that addItemSQL function I’m using:

bulkInsert(productBulk){
    return this.platform.ready().then(() => {
        return this.db.transaction( function (tx) {
            productBulk.forEach(function (row) {
                var data = [ <all the same things, but now with row. in front of them > ];
                tx.executeSQL('INSERT INTO ...', data);
            });
        });
    });
}

But this doesn’t seem to be working.

Are you getting any error or console message?

I’ve tried doing a console.log(JSON.stringify(data)) between the var data = […] and the tx.executeSQL lines, which don’t get printed.

According to https://github.com/driftyco/ionic/issues/6594 , this is not how it’s supposed to work yet.

I’ve updated my code accordingly, but nope; the executeSQL never happens.

return this.platform.ready().then(() => {
    return this.db.query('SELECT * FROM ...').then((response) => {
        productBulk.forEach((row) => {
            var data = [ < all the same things, but now with row. in front of them > ];
            response.tx.executeSQL('INSERT INTO ... ', data);
        });
    });
});

I know the data is passing through correctly, but not that last part.