P
P
Power_Shield2015-12-07 19:21:07
Node.js
Power_Shield, 2015-12-07 19:21:07

How to get a selection from the database and work with it?

It is necessary to perform a selection from the database, and then on it (passing the array) to work with the data and write again. How to implement.
The best option is to get the data, work with it and write it, then move on to the next element (for a given selection)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nicholas, 2015-12-08
@ACCNCC

If mysql then https://github.com/felixge/node-mysql

A
Alexander Prozorov, 2015-12-08
@Staltec

A spherical (in a vacuum) example with mySQL and the async library . All users who are not yet "dudes" become so. Users are processed sequentially.

var async = require('async');
var mysql      = require('mysql');

var connection = mysql.createConnection({
  host     : 'localhost',
  user     : 'me',
  password : 'secret',
  database : 'my_db'
});

connection.connect();

async.auto({
    users: function (cb) {
        connection.query("SELECT `id`, `name`, `isDude` FROM `users`", function(err, rows) {
            cb(err, rows);
        });
    },
    handle: ['users', function (cb, results) {
        async.forEachOfSeries(results.users, function (user, key, mapCb) {
            if (user.isDude) return mapCb();
            connection.query("UPDATE `users` SET `isDude`=1 WHERE `id`=?", [user.id], mapCb);
        }, cb);
    }]
}, function (err) {
    if (err) {
        console.error(err);
    } else {
        console.log('All users is dudes, bro!');
    }

    connection.end();
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question