How to perform bulk operations with help of SQL Agent?

Fri Aug 18 2017 09:53:24 GMT+0200 (CEST), Peter Širka

How to perform bulk operations in SQL Agent?

// .limit() paginates Array by 50 items
some_array.limit(50, function(items, next) {

    var sql = DB();
    var builder = [];

    // Creates raw SQL queries
    for (var i = 0, length = items.length; i < length; i++) {
        var item = items[i];
        builder.push('INSERT INTO tbl_products (id, name, price) VALUES({0},{1},{2})'.format(sql.escape(item.id), sql.escape(item.name), sql.escape(item.price)));
    }

    // Runs one big query
    sql.query(builder.join('\n'));
    sql.exec(function(err) {
        err && console.error(err);

        // Process next 50 pieces
        next();
    });

}, () => console.log('DONE'));

Tags

Follow us

Latest blogs
How do I transform ErrorBuilder output?
Mon Jan 29 2018 09:22:49 GMT+0100 (CET)
Flow: REST API components
Mon Jan 22 2018 10:11:14 GMT+0100 (CET)
New Total.js Eshop v11
Fri Jan 19 2018 09:55:09 GMT+0100 (CET)
New Total.js CMS v11
Fri Jan 19 2018 00:24:05 GMT+0100 (CET)
Flow 4.1.0
Thu Jan 18 2018 23:58:37 GMT+0100 (CET)

Latest comments
Total.js is amazing! ;)
Leonardo Hessel
Tue Dec 19 2017 19:51:15 GMT+0100 (CET)
It's easy, just set e.g. `res.status = 404`.
Peter
Thu Nov 16 2017 07:54:11 GMT+0100 (CET)
Yes, it's valid.
Peter
Thu Nov 16 2017 07:53:06 GMT+0100 (CET)
How do we set the status code in custome middleware?
Nabeel
Mon Oct 02 2017 10:31:40 GMT+0200 (CEST)
Node... Awesome applications.
oscar
Thu Aug 31 2017 02:21:59 GMT+0200 (CEST)
Awesome!
Tema
Wed May 10 2017 06:33:13 GMT+0200 (CEST)
Awesome, thanks!
Tema
Tue May 02 2017 14:13:03 GMT+0200 (CEST)